如何更改打印到pdf的ggplot中的高度/重量? [英] how to change the height/weight in a ggplot printed to pdf?
本文介绍了如何更改打印到pdf的ggplot中的高度/重量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑此 ggplots
的列表
:
mydata <- data_frame(group = c('a', 'a', 'a', 'b', 'b', 'b'),
x = c(1,2,3,5,6,7),
y = c(3,5,6,4,3,2)) %>%
group_by(group) %>%
nest() %>%
mutate(myplot = map(data, ~ggplot(data = .x, aes(x = x, y = x)) + geom_point()))
# A tibble: 2 x 3
group data myplot
<chr> <list> <list>
1 a <tibble [3 x 2]> <S3: gg>
2 b <tibble [3 x 2]> <S3: gg>
我可以使用以下简单方法打印 myplot
中包含的图:
I can print the plots contained in myplot
using the simple:
pdf('P://mychart.pdf')
do.call("grid.arrange", c(mydata $myplot))
dev.off()
但是,所有图表都包含在一个正方形中,从而浪费了很多空白.如何在这里控制边距,使图表更宽?
However, the charts are all contained in a square, wasting a lot of space on the margin. How can I get control over the margins here so that the charts are all wider?
谢谢!
推荐答案
您可以使用 ggsave
:
ggsave("P://mychart.pdf", do.call("arrangeGrob", mydata$myplot),
width = 9, height = 6, units = "in")
如上所示,该图是一个 .png
,但是 ggsave
与 .pdf
完美配合.
The plot as seen above is a .png
, but ggsave
works perfectly with .pdf
.
如果您想将 myplot
列表中的每个项目都绘制为单个 .pdf
文件,则可以使用 mapply
:
If you instead want to plot every item in the myplot
list as single .pdf
files, you could use mapply
:
mapply(function(x, y){
ggsave(paste0("P://mychart", x, ".pdf"), y, width = 9, height = 6, units = "in")
},1:length(mydata$myplot), mydata$myplot)
这将生成图 mychart1.pdf
和 mychart2.pdf
.
这篇关于如何更改打印到pdf的ggplot中的高度/重量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文