我正在尝试添加两个一起使用 facet_grid 的 ggplots 并收集下面的着色指南。它似乎只是从第二个情节中收集指南,即使我尝试在第一个情节中关闭指南,它仍然显示为副本
使用此代码
library(tidyverse)
library(patchwork)
n <- 100
data <- data.frame(
plot_label = c(rep("A", n), rep("B", n)),
facet_x = sample(c("p", "q", "r"), 2*n, replace = TRUE),
facet_y = sample(1:5, 2*n, replace = TRUE),
colour = sample(c("x", "y", "z"), 2*n, replace = TRUE),
random_x = runif(2*n),
random_y = rnorm(2*n)
)
p1 <- data %>%
filter(plot_label == "A") %>%
ggplot(aes(x = random_x, y = random_y)) +
geom_point(aes(colour = colour)) +
scale_colour_manual(values = c("purple", "skyblue", "green"), name = "myscale") +
facet_grid(facet_x~facet_y)
p2 <- data %>%
filter(plot_label == "B") %>%
ggplot(aes(x = random_x, y = random_y)) +
geom_point(aes(colour = colour)) +
scale_colour_manual(values = c("purple", "skyblue", "green"), name = "myscale") +
facet_grid(facet_x~facet_y)
plot <- (p1 + p2) &
plot_layout(guides = "collect") &
theme(
legend.position = "bottom",
legend.key.size = unit(0.2, "cm"),
legend.title = element_text(size = 16),
legend.text = element_text(size = 12)
)
plot
如有必要,我可能还可以通过拆分变量(“A”和“B”)进行分面,但出于与现实世界用例有关的原因,我非常不希望这样做。
我只是在为这件作品拼凑情节时才使用拼凑而成,所以我很有可能错过了一些基本的东西,但我一生都无法解决它