繁体   English   中英

在 R 中使用 ggplot2 facet_wrap() 的多条形图的尺寸

[英]Dimension of the multi barplot using ggplot2 facet_wrap() in R

请我想发送一个 pdf 文件,其中包含我使用ggplot2 / geom_bar()facet_wrap()生成的多条形图。 要求每个条形图的尺寸必须为 2 cm * 2 cm (W*H)。 不幸的是,我没有找到一种方法来做到这一点,例如,如果我运行命令行示例(上图),它会在 A4 纸中变大。

我在问是否有一个函数/包/解决方案允许在我的未来文件中定义每个条形图的确切大小,任何帮助将不胜感激。

pdf("result.pdf" , width = 8.27 , height = 11.69) 

ggplot(mtcars, aes(x=gear,  y=mpg, fill=vs)) +  
  geom_bar(position="dodge", stat="identity") +
  facet_wrap(~ carb, ncol=2)

dev.off()

您需要theme()功能。

pdf("result1.pdf" , width = 8.27 , height = 11.69) 

ggplot(mtcars, aes(x=gear, y=mpg, fill=vs)) +
  geom_bar(position="dodge", stat="identity") + facet_wrap(~ carb, ncol=2) +
  theme(panel.margin = unit(c(2, 2, 2, 2), "cm"))

dev.off()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM