R 调整「;“保证金”;ggplot2中轴文本的空间
R 调整「;“保证金”;ggplot2中轴文本的空间,r,ggplot2,gridextra,grob,R,Ggplot2,Gridextra,Grob,ggplot中的哪个属性(如果有)控制 轴文本的宽度(或空白量) 在下面的示例中,我的最终目标是“推入”顶部图形的左侧,使其与底部图形对齐 我尝试了主题(plot.margin=…)但这会影响整个情节的边距。 facet'ing也没有帮助,因为y轴上的刻度不同 作为最后的手段,我意识到我可以修改轴文本本身,但是我还需要计算每个图形的切割 可复制示例: 库(ggplot2) 图书馆(比例尺) 这里有一个解决方案 这个想法是借用自" 定义函数 align_plots1 <- functio
ggplot
中的哪个属性(如果有)控制轴文本的宽度(或空白量)
在下面的示例中,我的最终目标是“推入”顶部图形的左侧,使其与底部图形对齐 我尝试了
主题(plot.margin=…)
但这会影响整个情节的边距。facet
'ing也没有帮助,因为y轴上的刻度不同
作为最后的手段,我意识到我可以修改轴文本本身,但是我还需要计算每个图形的切割
可复制示例:
库(ggplot2)
图书馆(比例尺)
这里有一个解决方案
这个想法是借用自"
定义函数
align_plots1 <- function (...) {
pl <- list(...)
stopifnot(do.call(all, lapply(pl, inherits, "gg")))
gl <- lapply(pl, ggplotGrob)
bind2 <- function(x, y) gtable:::rbind_gtable(x, y, "first")
combined <- Reduce(bind2, gl[-1], gl[[1]])
wl <- lapply(gl, "[[", "widths")
combined$widths <- do.call(grid::unit.pmax, wl)
grid::grid.newpage()
grid::grid.draw(combined)
}
short <- P.base + geom_bar(aes(y=y1), stat="identity", width=.5)
long <- P.base + geom_bar(aes(y=y2), stat="identity", width=.5)
#Now, align the plots
align_plots1(short, long)
align_plots1当你从其他地方借用解决方案时,链接到原始问题是很有用的,也是很好的礼仪。谢谢提醒。我忘了在找到解决方案的地方附加链接。我添加了链接。grid.arrange
不擅长对齐图,你应该始终使用GTTable。谢谢@baptiste,这似乎是这正是我想要的。请随意发布您自己的答案。另外,我很好奇为什么rbindgtable
不是一个导出函数?rbind.gtable作为一个方法导出,但它用Reduce调用这个函数来表示多个gtable。或者是这样,或者哈德利对下划线有弱点。
align_plots1 <- function (...) {
pl <- list(...)
stopifnot(do.call(all, lapply(pl, inherits, "gg")))
gl <- lapply(pl, ggplotGrob)
bind2 <- function(x, y) gtable:::rbind_gtable(x, y, "first")
combined <- Reduce(bind2, gl[-1], gl[[1]])
wl <- lapply(gl, "[[", "widths")
combined$widths <- do.call(grid::unit.pmax, wl)
grid::grid.newpage()
grid::grid.draw(combined)
}
short <- P.base + geom_bar(aes(y=y1), stat="identity", width=.5)
long <- P.base + geom_bar(aes(y=y2), stat="identity", width=.5)
#Now, align the plots
align_plots1(short, long)