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!
当使用R中的pdf()函数在外部文件中保存绘图时,我们可以指定宽度和/或高度来调整绘图的大小。但是,有些情况下我们获得多个图(比如使用par(mfrow = c(2,4)))。在这种情况下,要确定PDF文件的最佳宽度和高度是多么困难,以便正确显示所有图形。有没有办法让R自动“适应PDF文件中的图”?我搜索了pdf()中的参数并尝试了一些,但结果并不令人满意。非常感谢你!
1 个解决方案
#1
2
How about something using ggplot?
用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")
EDIT: Should you need to print over multiple pages, see this question.
编辑:如果您需要打印多个页面,请参阅此问题。
#1
2
How about something using ggplot?
用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")
EDIT: Should you need to print over multiple pages, see this question.
编辑:如果您需要打印多个页面,请参阅此问题。