将导出为pdf文件的绘图的R主标题中的文本换行

时间:2013-03-15 01:05:46

标签: r pdf plot rstudio

当我使用长“主”标题在RStudio中生成绘图时,标题似乎自动环绕,当它们由RStudio生成时,通常会保留在可见页面边距内。但是当我使用R的pdf()函数将图表导出到文件时,文本换行消失会超出页边距并且看起来很糟糕。有没有办法让用PDF输出的图表看起来和他们在RStudio看起来一样好?我知道我可以手动将\n个字符插入到标题字符串中,或​​者(我还没有尝试过)我可以使用strwrap,但这似乎应该出现的那种功能如果我只是知道什么开关翻转的话。有一种简单的方法,或者只是繁琐的,一次性的方式吗?

# Demo code for long plot title
plot(density(rnorm(10000,0,1))
     ,main="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor"
)

2 个答案:

答案 0 :(得分:1)

绘图查看窗口和文件输出设备(如pdf()

)之间的差异

有时在绘图窗口中绘图与文件输出设备中的绘图不同。根据我的经验,这是因为它们具有不同的几何形状。试试这些......

pdf("Desktop/testSmall.pdf",width=4,height=4)
plot(1:10,1:10,main="This is a fairly long plot title with no line breaks")
dev.off()

和更大的第二个情节

pdf("Desktop/testLarge.pdf",width=8,height=8)
plot(1:10,1:10,main="This is a fairly long plot title with no line breaks")
dev.off()

换行

以下链接表明这不是一个需要解决的简单问题(但并非不可能):

https://stat.ethz.ch/pipermail/r-help/2008-June/164458.html

我不知道你在RStudio中看到什么“好”意味着什么,但是这会分割主标题中的行并使它们居中。

pdf("test.pdf")
plot(1:10,1:10,main=paste("Line one","Line two",sep="\n"))
dev.off()

答案 1 :(得分:0)

在我的系统下,我需要做的就是确保方向设置为portrait(而横向是默认设置)

当您导出为pdf时,RStudio将根据Rstudio设备的大小给出适当的pdf高度和宽度。