在PDF输出中自动调整R绘图大小 [英] automatically adjust R plot size in a PDF output
问题描述
在R中使用pdf()
函数将图保存到外部文件中时,我们可以指定width
和/或height
来调整图的大小.但是,在某些情况下,我们会获得多个绘图(例如使用par(mfrow=c(2,4))
).在这种情况下,要正确显示所有图,很难确定PDF文件的最佳width
和height
是什么.有没有办法让R自动拟合图"在PDF文件中?我在pdf()
中搜索了参数并尝试了一些,但结果并不令人满意.非常感谢你!
When using the pdf()
function in R for saving a plot in an external file, we can specify width
and/or height
to adjust the size of the plot. However, there are situations when we obtain multiple plot (say using par(mfrow=c(2,4))
). In this situation, it's kind of difficult to determine what is the best width
and height
for the PDF file in order to have all plots displayed properly. Is there a way to let R automatically "fit the plots" in the PDF file? I searched the arguments in pdf()
and tried some, but the results are not satisfactory. Thank you very much!
推荐答案
使用ggplot怎么样?
How about something using ggplot?
require(ggplot2)
# Bogus data
x <- rnorm(10000)
y <- as.factor(round(rnorm(10000, mean=10, sd=2), 0))
df <- data.frame(vals=x, factors=y)
myplot <- ggplot(data=df, aes(x=vals)) +
geom_density() +
facet_wrap(~ factors)
ggsave(filename="~/foo.pdf", plot=myplot, width=8, height=10, units="in")
Should you need to print over multiple pages, see this question.
这篇关于在PDF输出中自动调整R绘图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!