简体   繁体   English

R缩放设置宽度和高度的PDF中的绘图元素

[英]R Scale plot elements within PDF of set width and height

Though R plots sent to a PDF can be rescaled at will in an illustration or page layout software, scientific journals often insist that the plots provided have specific dimensions. 虽然发送到PDF的R图可以在插图或页面布局软件中随意重新调整,但科学期刊通常坚持提供的图表具有特定的尺寸。

Can the size of all plotting elements be scaled within a given PDF size directly in R? 所有绘图元素的大小可以直接在R中在给定的PDF大小内缩放吗?

require(ggplot2)

p <- qplot(data=iris,
           x=Petal.Width,
           y=Petal.Length,
           colour=Species)

pdf("./test_plot_default.pdf")
print(p)
graphics.off()

Produces adequate scaling of plot elements 生成适当的绘图元素缩放

测试图,默认为pdf大小

However, changing PDF size elements does not cause the plot elements to scale. 但是,更改PDF大小元素不会导致绘图元素缩放。 With smaller PDFs, the plotting elements are overly enlarged compared to the plotting space. 对于较小的PDF,绘图元素与绘图空间相比过度放大。

pdf("./test_plot_dimentionsions required by journal.pdf", width=3, height=3)
print(p)
graphics.off()

在此输入图像描述

Using @Rosen Matev suggestion: 使用@Rosen Matev建议:

update_geom_default("point", list(size=1))
theme_set(theme_grey(base_size=6))
pdf("./test_plot_dimentionsions required by journal.pdf", width=3, height=3)
print(p)
graphics.off()

在此输入图像描述

Journals insist on having specific plot dimensions in order to avoid scaling. 期刊坚持要求具有特定的情节尺寸以避免缩放。 If made, it can render the font size too small (or large) and inconsistent with the figure caption font size. 如果制作,它可以使字体大小太小(或大)并且与图标题字体大小不一致。 That is why the plot elements (text, point size, etc.) by design have the same absolute size regardless of pdf size. 这就是为什么设计中的绘图元素(文本,点大小等)具有相同的绝对大小,而与pdf大小无关。

You can change the default font size and point size, for example, with: 您可以更改默认字体大小和磅值,例如:

p <- ggplot(iris, aes(x=Petal.Width, y=Petal.Length, colour=Species)) +
  geom_point(size=1.5) +  # default is 2
  theme_grey(base_size=10)  # default is 12
ggsave("test.1.pdf", p)

The defaults can be changed globally, too: 默认值也可以全局更改:

update_geom_defaults("point", list(size=1.5))
theme_set(theme_grey(base_size=10))

Oddly enough, you can do this with scale= in ggsave(...) 奇怪的是,你可以用scale= in ggsave(...)来做到这一点

require(ggplot2)
p <- qplot(data=iris, x=Petal.Width, y=Petal.Length, colour=Species)
ggsave("test.1.pdf",p)
ggsave("test.2.pdf",p, width=3, height=3, units="in", scale=3)

Try playing with the scale parameter and see what you get... 尝试使用scale参数,看看你得到了什么......

Consider using width , height and pointsize in pdf() function. 考虑在pdf()函数中使用widthheightpointsize

If you like to stick with using pdf() instead of other ways like using sweave, then you would better use pdf function with more arguments, like bellow (I do not have ggplot2 installed, so simillar case is supplied). 如果你喜欢使用pdf()而不是像使用sweave那样的其他方法,那么你最好使用带有更多参数的pdf函数,比如bellow(我没有安装ggp​​lot2,所以提供了simillar case)。

# making scaled plot in pdf
# using paper=a4 just to see the efect

for (sc in c(0.5,0.75,1)) {
pdf(width=7*sc,height=7*sc,pointsize=12*sc,file=paste("scale",sc,".pdf",sep=""),paper="a4")
plot(sin((1:314)/100),main=paste("PDF sc",sc))
dev.off()  
}

It is quite usable, but works in some extent. 它非常实用,但在某种程度上有效。 Once the scale is too small, pdf will start to enforce linewidth, fontsize etc. 一旦比例太小,pdf将开始强制执行线宽,字体大小等。

See the results in scale*.pdf created by example. 查看示例创建的scale * .pdf结果。

And for ggplot2 ... 而对于ggplot2 ......

sc <- c(0.5,0.75,1)
fi <- c("pic1.pdf","pic2.pdf","pic3.pdf")

require(ggplot2)
p <- qplot(data=iris,
x=Petal.Width,
y=Petal.Length,
colour=Species)

for (i in 1:3) {
pdf(width=7*sc[i],height=7*sc[i],pointsize=12*sc[i],file=fi[i])
print(p)
dev.off()  
}

Latex code to test the files how they look in one document: 用于测试文件在一个文档中的外观的Latex代码:

\documentclass[a4paper,11pt]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphics}

\begin{document}

\begin{figure}
     \includegraphics{pic1.pdf}
 \end{figure}

\begin{figure}
     \includegraphics{pic2.pdf}
\end{figure}

\begin{figure}
    \includegraphics{pic3.pdf}
\end{figure}

\end{document}

An option equally good or better than pdf is tiff. 与pdf同样好或更好的选项是tiff。 All journals I have come across like tiff. 我遇到的所有期刊都像tiff一样。

tiff(filename="name.tiff", width=5, height=5, units="in",
     pointsize=8, compression="lzw", bg="white", res=600,
     restoreConsole=TRUE)
qplot(data=iris, x=Petal.Width, y=Petal.Length, colour=Species)
dev.off()

If you are on linux, drop restoreConsole=TRUE , seems only windows likes that. 如果你在Linux上,删除restoreConsole=TRUE ,似乎只有Windows喜欢它。

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

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