我希望将不同功能生成的多个图形组合到单个显示中.具体来说,我只想在包装中保留第一个图表的传奇.
以下是该问题的最小版本:
library(ggplot2)
library(patchwork)
plot_histogram <- function() {
plot_data <- ggplot(mtcars, aes(x = mpg, fill = factor(am))) +
geom_histogram(binwidth = 2, color = "black") +
labs(title = "Histo",
x = "Miles/gallon",
y = "nb cars",
fill = "Transmission") +
scale_fill_manual(values = c("skyblue", "salmon"),
labels = c("Auto", "Man")) +
theme_minimal() +
theme(legend.position = "bottom")
return(plot_data)
}
plot_scatter <- function() {
plot_data <- ggplot(mtcars, aes(x = mpg, y = wt, color = factor(cyl))) +
geom_point(size = 3) +
labs(title = "Scatterplot",
x = "Miles/gallon",
y = "weight",
color = "Cyl") +
theme_minimal() +
theme(legend.position = "bottom")
return(plot_data)
}
scatter_plot_left <- plot_scatter()
scatter_plot_right <- plot_scatter()
histogram_plot <- plot_histogram()
combined_plots <- wrap_plots(scatter_plot_left + scatter_plot_right,
histogram_plot,
nrow = 1,
widths = c(3, 1)) +
plot_layout(guides = 'collect') &
theme(legend.position = "bottom")
combined_plots
有没有办法只保留散布情节的传说?
从现在开始,我找到的最佳"解决方案"是用guides = 'collect'
包裹前两个图,然后用另一个图包裹它,而不保留插图.
这并不是一个真正的解决方案,因为传奇没有居中,并且每次都必须调整图形的大小,这在自动生产过程中并不好.