Ich kann drei Plots kombinieren, die keine Legenden haben, aber wenn ich Legenden zu zwei der Plots hinzufügen, kann ich sie nicht kombinieren. Wie kann ich einige Plots mit Legenden mit einem Plot ohne Legende kombinieren?Kombinieren Sie Plots mit einer Legende mit einer, die nicht
Hier ist ein Beispiel dafür, wie ich Parzellen ohne Legenden bin Kombination:
library(ggplot2)
map_data <- data.frame(lon = c(-177.5, -172.5, -167.5, -162.5, -157.5, -152.5),
lat = c(87.5, 87.5, 87.5, 87.5, 87.5, 87.5),
ssta = c(-2.78999996, 0.09999999, 0.19999999, 0.06000000, 1.65999997, -0.41000000))
# one map
p.1 <- ggplot() +
borders("world",
colour="black",
fill="white") +
geom_tile(data = map_data,
aes(lon,
lat)) + # no fill value so no legend
coord_fixed() +
scale_fill_viridis(na.value = "transparent") +
theme_minimal()
# a second map, the same general shape
p.2 <- p.1
# a line plot
library(lubridate)
time_sequence <- seq(dmy("01-01-1800"),
dmy("01-01-1960"),
by = 'month')
long_plot_data <- data.frame(variable = seq(dmy("01-01-1800"),
dmy("01-01-1960"),
by = 'month'),
value = runif(length(time_sequence)),
col = runif(length(time_sequence)))
p.3 <- ggplot(long_plot_data,
aes(variable,
value)) +
geom_line() +
theme_minimal()
# combine plots
library(grid)
grid.newpage()
grid.draw(rbind(ggplotGrob(p.1),
ggplotGrob(p.2),
ggplotGrob(p.3),
size = "last"))
Und jetzt, wenn ich einen Füllwerts auf die Karte Plots hinzufügen, erhalte ich eine Legende und ein Fehler wenn ich versuche, auf die gleiche Art und Weise zu kombinieren:
Hier ist, wie das aussieht:
# a second map, the same general shape
p.2 <- p.1
# a line plot
library(lubridate)
time_sequence <- seq(dmy("01-01-1800"),
dmy("01-01-1960"),
by = 'month')
long_plot_data <- data.frame(variable = seq(dmy("01-01-1800"),
dmy("01-01-1960"),
by = 'month'),
value = runif(length(time_sequence)),
col = runif(length(time_sequence)))
p.3 <- ggplot(long_plot_data,
aes(variable,
value)) +
geom_line() +
theme_minimal()
Jetzt versuche ich die beiden Karten mit Legenden zu verbinden und die Liniendiagramm ohne eine Legende:
# combine plots
library(grid)
grid.newpage()
grid.draw(rbind(ggplotGrob(p.1),
ggplotGrob(p.2),
ggplotGrob(p.3),
size = "last"))
Der Ausgang Error: ncol(x) == ncol(y) is not TRUE
ist aber, wenn ich hinzufügen, eine Legende das Liniendiagramm, so dass alle drei Plots Legenden haben, kann ich sie kombinieren:
Wie kann ich zwei Diagramme mit Legenden mit einem ohne Legende kombinieren?
Möglicherweise müssen Sie die Legende zu einem separaten grob bewegen und dann mit Gitter positioniere es auf der linken Seite mit zwei leeren Groschen darunter (super super hacky), ähnlich wie https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs – Andrew
Habe a schau dir @baptistes Ei-Paket an, [hier verfügbar] (https: //github.c Om/Baptiste/Ei) auf gtihub. –