我正在努力生成一个图,该图包含 2 个类别、9 个子类别,并且应该适合 A4 页面的一半。
我尝试过使用两个方面标签,但是子图的排列成为一个问题,并且并不是每个子图都需要标记。我尝试过facet、grid.arrange、ggarrange和cowplot,但我没有设法删除子图之间的空白并控制标签。
任何关于如何解决这个问题的建议都很好;我真的想避免将其放入绘画程序中并手动完成(不可重现)。
我正在尝试生成一个看起来像这样的图:
但我正在使用这个:
inter_plots <- function(data, x_id, y_id, xlab, ylab, xmin, xmax){
ggplot(data, aes_string(x_id, y_id, fill = "probs")) +
geom_tile(color = "white", lwd = 0, linetype = 1) +
scale_x_continuous(expand = c(0, 0), breaks = c(-0.5, 0, 1, 2)) +
scale_y_continuous(expand = c(0, 0),
breaks = c(min(data$sqrt_ud),8.867e-07,1.755e-06),
labels = my_labels) +
labs(x = "", y = "", fill = "Selection \nprobability") +
scale_fill_gradientn("Selection \nprobability", colors = colfunc(135)) +
theme_classic() +
theme(axis.text = element_text(color = "black", size = 15),
axis.line = element_line(linewidth = 1.2),
text = element_text(size = 20),
legend.text = element_text(size = 15),
legend.position = "none",
aspect.ratio = 1,
axis.title.x=element_text(color = "white"),
axis.text.x=element_text(color = "white"),
axis.ticks.x=element_blank(),
plot.margin=unit(c(-1,1,-1,0), "cm"))
}
生成单独的图并将它们排列在gridExtra中:
gridExtra::grid.arrange(fall_plots[[1]], spring_plots[[1]], fall_plots[[2]], spring_plots[[2]], fall_plots[[3]], spring_plots[[3]],
fall_plots[[4]], spring_plots[[4]], fall_plots[[5]], spring_plots[[5]], fall_plots[[6]], spring_plots[[6]], fall_plots[[7]], spring_plots[[7]],
fall_plots[[8]], spring_plots[[8]], fall_plots[[9]],
ncol = 2, nrow = 9, left = yleft, bottom = xbottom,
vp = viewport(width=0.9, height=0.9))
谢谢!
您的问题很简单,就是您的绘图窗口的纵横比太宽。因为您已将绘图的纵横比设置为 1,所以您需要确保输出设备与您想要的布局完全匹配。
显然,我们没有您的数据,但让我们生成 18 个具有固定纵横比的随机图:
plot_list <- lapply(1:18, function(i) {
n <- sample(5, 2)
ggplot(setNames(iris[n], c("x", "y")), aes(x, y)) +
geom_point() +
theme(aspect.ratio = 1)
})
gridExtra::grid.arrange(plot_list[[1]] , plot_list[[2]], plot_list[[3]],
plot_list[[4]] , plot_list[[5]], plot_list[[6]],
plot_list[[7]] , plot_list[[8]], plot_list[[9]],
plot_list[[10]] , plot_list[[11]], plot_list[[12]],
plot_list[[13]] , plot_list[[14]], plot_list[[15]],
plot_list[[16]] , plot_list[[17]], plot_list[[18]],
ncol = 2, nrow = 9,
vp = viewport(width = 0.9, height = 0.9))
现在,如果我将绘图窗口设置为 1488 x 1880 像素,由于绘图的宽高比固定,因此 has 会留下空白:
但是如果我将绘图尺寸更改为 500 x 1880 像素,我会丢失大部分多余的空白:
完全删除空格的最佳方法是从 ggplot 代码中删除
aspect.ratio = 1
。这将导致所有空白被删除。然后,您只需调整绘图窗口的尺寸即可使各个面板显示为正方形。