[英]ggplot2 align top of two facetted plots
我需要安排两个刻面图,如下:
d = data.frame(Index = LETTERS[1:5],x=1:5,y=1:5)
A = ggplot(subset(d,Index == 'A'),aes(x,y)) +
theme_bw() +
theme(axis.title.x = element_blank()) +
geom_point() + facet_wrap(~Index) + labs(title = "Title, The Title",
subtitle = "Subtitle, The Subtitle",
y = "Side Axes")
B = ggplot(subset(d,Index != 'A'),aes(x,y)) +
theme_bw() +
theme(axis.title.x = element_blank(), axis.title.y = element_blank()) +
geom_point() + facet_wrap(~Index) + labs(title = "", subtitle = "")
g = gridExtra::arrangeGrob(A,B,ncol=2,bottom="Bottom Axes")
grid.arrange(g)
产生以下内容:
从上面可以看出,绘图区域的顶部边缘之间存在轻微的不对齐。 这是由标题和副标题中的“逗号”引起的。
有谁知道如何强制顶边对齐? 我需要左侧情节的标题和副标题,右侧有一个(空)标题,副标题。
@ CephBirk的解决方案是一个聪明而简单的方法。 对于像这样的黑客不起作用的情况,您可以从图中删除标题和子标题,而是使用grid.arrange
和arrangeGrob
为您创建单独的grid.arrange
以及arrangeGrob
。 在下面的代码中,我还添加了一个nullGrob()
作为图A
和B
之间的间隔符,因此左图中的右x标签( 1.50
)不会被截断。
library(gridExtra)
A = ggplot(subset(d,Index == 'A'),aes(x,y)) +
theme_bw() +
theme(axis.title = element_blank()) +
geom_point() + facet_wrap(~Index)
B = ggplot(subset(d,Index != 'A'),aes(x,y)) +
theme_bw() +
theme(axis.title.x = element_blank(), axis.title.y = element_blank()) +
geom_point() + facet_wrap(~Index)
grid.arrange(
arrangeGrob(
arrangeGrob(textGrob("Title, The Title", hjust=0),
textGrob("Subtitle, The Subtitle", hjust=0, gp=gpar(cex=0.8))),
nullGrob(), ncol=2, widths=c(1,4)),
arrangeGrob(A, nullGrob(), B, ncol=3, widths=c(8,0.1,8),
left="Side Axes", bottom="Bottom Axes"),
heights=c(1,12))
它有点hackish,但你可以在左边有相同的标题和副标题,但是用白色字体打印。 :)你说它是由于逗号,所以修复它。
不是最满意的,但它完成了工作。
B = ggplot(subset(d,Index != 'A'),aes(x,y)) +
theme_bw() +
theme(axis.title.x = element_blank(), axis.title.y = element_blank(), title = element_text(color = 'white')) +
geom_point() + facet_wrap(~Index) +
labs(title = "Title, The Title", subtitle = "Subtitle, The Subtitle")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.