在ggplot2中调整/设置页边/图形区域的宽度 [英] Aligning / setting width of margin/figure region in ggplot2
问题描述
我需要将几块地块放置在彼此的下方,我希望边缘和图形区域的宽度相同,以便整齐排列。请注意,我需要两个单独的地块,而不是一个联合地块。我想将每一个保存在一个单独的PNG文件中。
library(ggplot2)
$ b我只是希望它们的结构(边距,图形区域大小) $ bd< - data.frame(label = c(一些很长的标签有很多文字,
另一个带有大量文字的长标签),
short,
也短,
短,
也短),
x = 1:6)
ggplot(d [1:2,], aes(label,x))+ geom_bar(stat =identity)+ coord_flip()
ggplot(d [3:4,],aes(label,x))+ geom_bar(stat =identity) + coord_flip()
我想要的是plot 2具有与图中相同的左边距宽度1,或多或少像下面,当然不会增加额外的空白;)
在基本图形中,我会相应地设置 par(mar)
。
如何在ggplot中实现此功能? div> 根据 Plot 2 I need to position several plots below each other and I want the widths of the margin and figure regions to be identical, so they align neatly. Note, that I need two single plots, not one joint plot. I want to save each one in a separate PNG file. I just want their structure (margin, figure region size) to be identical.
What I want is plot 2 to have the same left margin width as in plot 1, more or less like below, of course without adding extra blanks ;) In base graphics I would just set How can I achieve this in ggplot? Based on the answer from the last link from the comment above,
you can equate the widths of the plots. The only difference from that answer is that they are not combined. So for your plots Equate widths of plot Plot Which produces: Plot 1 Plot 2 这篇关于在ggplot2中调整/设置页边/图形区域的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
library(ggplot2)
d <- data.frame(label=c("some very longe label with lots of text",
"another long label with lots of text",
"short",
"also short",
" short",
" also short"),
x = 1:6)
ggplot(d[1:2, ], aes(label, x)) + geom_bar(stat = "identity") + coord_flip()
ggplot(d[3:4, ], aes(label, x)) + geom_bar(stat = "identity") + coord_flip()
par("mar")
accordingly. library(ggplot2)
p1 <- ggplot(d[1:2, ], aes(label, x)) + geom_bar(stat = "identity") + coord_flip()
p2 <- ggplot(d[3:4, ], aes(label, x)) + geom_bar(stat = "identity") + coord_flip()
library(grid)
gl <- lapply(list(p1,p2), ggplotGrob)
wd <- do.call(unit.pmax, lapply(gl, "[[", 'widths'))
gl <- lapply(gl, function(x) {
x[['widths']] = wd
x})
grid.newpage(); grid.draw(gl[[1]])
grid.newpage(); grid.draw(gl[[2]])