我尝试创建一个具有共享轴标签的绘图。问题是,我希望我的图例位于我的地块下,因为否则我的地块会太压缩。我没能用 Eggpackage 创建一个好的图,因为我的 x 轴标签在我的图例下。
所以我想我可以尝试一下解决办法,并询问了 ChatGPT。问题是,下面的代码切断了我的 x 轴标签的最后一部分。我是 R Studio 的新手,不知道我是否犯了一个明显的错误。我不知道我用最小的、可重现的例子是否做对了。 感谢您的帮助!
编辑:我更新了我的 MRE,我无法将它提供的 3 个图合并到一个图中,因为这将包括 12 条线和我的原始数据。
set.seed(123)
spy <- data.frame(
UG_100 = runif(10, 0, 100),
UG_500 = runif(10, 0, 100),
UG_1000 = runif(10, 0, 100),
SK_0_disap = rbinom(10, 1, 0.5),
SK_Z_disap = rbinom(10, 1, 0.5),
SB_Z_disap = rbinom(10, 1, 0.5),
SB_0_disap = rbinom(10, 1, 0.5)
)
# packages
library(ggplot2)
library(viridis)
library(patchwork)
library(cowplot)
# Plots
combined_plot1 <- ggplot(spy, aes(x = UG_100)) +
geom_point(aes(y = SK_0_disap, color = "SK_0_disap")) +
geom_smooth(aes(y = SK_0_disap, color = "SK_0_disap", fill = "SK_0_disap"), method = "glm", se = TRUE, method.args = list(family = binomial), alpha = 0.2) +
theme_classic() +
labs(x = NULL, y = "Disappearance") +
scale_color_viridis_d(name = "Food Type", labels = c("sunflower seeds \nwithout cinnamon")) +
scale_fill_viridis_d(name = "Food Type", labels = c("SK_0_disap"), guide = FALSE)
combined_plot_500_1 <- ggplot(spy, aes(x = UG_500)) +
geom_point(aes(y = SK_0_disap, color = "SK_0_disap")) +
geom_smooth(aes(y = SK_0_disap, color = "SK_0_disap", fill = "SK_0_disap"), method = "glm", se = TRUE, method.args = list(family = binomial), alpha = 0.2) +
theme_classic() +
labs(x = "Percentage of impervious surface cover", y = NULL) +
theme(axis.title.x = element_text(margin = margin(t = 20))) +
scale_color_viridis_d(name = "Food Type", labels = c("sunflower seeds \nwithout cinnamon")) +
scale_fill_viridis_d(name = "Food Type", labels = c("SK_0_disap"), guide = FALSE)
combined_plot_1000 <- ggplot(spy, aes(x = UG_1000)) +
geom_point(aes(y = SK_0_disap, color = "SK_0_disap")) +
geom_smooth(aes(y = SK_0_disap, color = "SK_0_disap", fill = "SK_0_disap"), method = "glm", se = TRUE, method.args = list(family = binomial), alpha = 0.2) +
theme_classic() +
labs(x = NULL, y = NULL) +
scale_color_viridis_d(name = "Food Type", labels = c("sunflower seeds \nwithout cinnamon")) +
scale_fill_viridis_d(name = "Food Type", labels = c("SK_0_disap"), guide = FALSE)
# Plot patchwork
combined_plot_all <- (combined_plot1 | combined_plot_500_1 | combined_plot_1000) +
plot_layout(guides = "collect") &
theme(legend.position = "bottom")
final_plot <- combined_plot_all +
plot_annotation(
tag_levels = 'a',
theme = theme(plot.tag = element_text(size = 14, face = "bold"))
) &
plot_annotation(
title = NULL,
subtitle = NULL,
caption = NULL,
tag_levels = c('a', 'b', 'c'),
theme = theme(plot.tag = element_text(size = 14, face = "bold"))
)
print(final_plot)
这里有两个选项可以实现您想要的结果。首先,正如我在评论中已经提到的,您可能会考虑使用分面,这需要将数据重塑很长,但允许创建一个由
UG
分面的图。
library(tidyverse)
spy_long <- spy |>
tidyr::pivot_longer(starts_with("UG"), names_to = "ug", values_to = "value") |>
mutate(ug = factor(ug, paste0("UG_", c(100, 500, 1000))))
ggplot(spy_long, aes(x = value, y = SK_0_disap)) +
geom_point(aes(color = "SK_0_disap")) +
geom_smooth(
aes(color = "SK_0_disap", fill = "SK_0_disap"),
method = "glm", se = TRUE, method.args = list(family = binomial), alpha = 0.2
) +
scale_color_viridis_d(
name = "Food Type", labels = c("sunflower seeds \nwithout cinnamon"),
aesthetics = c("color", "fill")
) +
facet_wrap(~ug) +
theme_classic() +
theme(legend.position = "bottom") +
labs(x = "Percentage of impervious surface cover", y = "Disappearance")
#> `geom_smooth()` using formula = 'y ~ x'
其次,如果你想坚持使用单独的图,例如因为您想要有标签,那么您仍然可以使用
axes="collect"
来获取共享轴,其工作方式与 guides="collect"
类似。但与后者一样,收集仅在轴相同时才有效,这意味着轴应该具有相同的名称、标签、中断和限制,即您必须为每个图添加相同的轴标题,并且重要的是确保所有 x 轴共享相同的 limits=
。此外,我避免为每个图重复代码,而是指定整个补丁的大部分图层,这需要使用 patchwork
的 &
运算符而不是 ggplot2
的 +
添加图层。
注意:如果您只需要共享 x 轴,也可以使用
axes="collect_x"
仅收集 x 轴。
combined_plot_100 <- ggplot(spy, aes(x = UG_100, y = SK_0_disap))
combined_plot_500 <- ggplot(spy, aes(x = UG_500, y = SK_0_disap))
combined_plot_1000 <- ggplot(spy, aes(x = UG_1000, y = SK_0_disap))
library(patchwork)
(combined_plot_100 | combined_plot_500 | combined_plot_1000) +
plot_layout(guides = "collect", axes = "collect") +
plot_annotation(
tag_levels = "a",
theme = theme(plot.tag = element_text(size = 14, face = "bold"))
) &
geom_point(aes(color = "SK_0_disap")) &
geom_smooth(aes(color = "SK_0_disap", fill = "SK_0_disap"),
method = "glm", se = TRUE, method.args = list(family = binomial), alpha = 0.2
) &
scale_x_continuous(limits = c(0, 100)) &
scale_color_viridis_d(
name = "Food Type", labels = c("sunflower seeds \nwithout cinnamon"),
aesthetics = c("color", "fill")
) &
theme_classic() &
theme(
axis.title.x = element_text(margin = margin(t = 20)),
legend.position = "bottom"
) &
labs(x = "Percentage of impervious surface cover", y = "Disappearance")
#> `geom_smooth()` using formula = 'y ~ x'
#> `geom_smooth()` using formula = 'y ~ x'
#> `geom_smooth()` using formula = 'y ~ x'