gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:28:52 25 4
gpt4 key购买 nike

虽然发送到 PDF 的 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()

对绘图元素进行适当的缩放

test plot with default pdf size

但是,更改 PDF 大小元素不会导致绘图元素缩放。对于较小的 PDF,绘图元素与绘图空间相比会过度放大。

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

enter image description here

使用@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()

enter image description here

最佳答案

考虑在 pdf() 函数中使用宽度高度点数

如果您喜欢坚持使用 pdf() 而不是使用 sweave 等其他方式,那么您最好使用带有更多参数的 pdf 函数,如下所示(我没有安装 ggplot2,因此提供了类似的情况)。

# 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()
}

它非常有用,但在某种程度上有效。一旦比例太小,pdf将开始强制执行线宽、字体大小等。

查看示例创建的scale*.pdf 中的结果。

对于 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 代码:

\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}

关于R 在设置宽度和高度的 PDF 中缩放绘图元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21484999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com