如何在 knitr 中设置情节的 width/height?
How to set the width/height of a plot in knitr?
我有一些 R 代码,我在其中打开一个 png() 设备来写入绘图,但我希望该绘图也显示在我使用 Knit-R 从代码生成的 PDF 中。
到目前为止,我已经这样做了:
png(file="filename.png", width=5, height=5, units="in", res=300)
dev.control(displaylist="enable")
# Do the plotting here
dev.off()
这会创建包含绘图的 filename.png
,由于 dev.control()
调用,它还会显示 Knit-R 生成的 PDF 中的绘图。但是,生成的 PDF 中绘图的大小有点偏差。我希望尺寸保留我给 png() 函数的宽度和高度比例。我该怎么做?
注意:这是一个R文件,不是Rnw文件。没有任何 LaTeX,只有 R 代码,我使用 R-Studio (Ctrl-Shift-K) 中的 "File > Knit" 命令。
RStudio 在这方面 运行 knitr's spin
function,因此您可以将块选项写入代码上方的 R 注释中 运行。例如:
#-name, fig.width=5, fig.height=5
png(file="filename.png", width=5, height=5, units="in", res=300)
dev.control(displaylist="enable")
plot(rnorm(10))
dev.off()
这将生成一个 5x5 图像:
我有一些 R 代码,我在其中打开一个 png() 设备来写入绘图,但我希望该绘图也显示在我使用 Knit-R 从代码生成的 PDF 中。
到目前为止,我已经这样做了:
png(file="filename.png", width=5, height=5, units="in", res=300)
dev.control(displaylist="enable")
# Do the plotting here
dev.off()
这会创建包含绘图的 filename.png
,由于 dev.control()
调用,它还会显示 Knit-R 生成的 PDF 中的绘图。但是,生成的 PDF 中绘图的大小有点偏差。我希望尺寸保留我给 png() 函数的宽度和高度比例。我该怎么做?
注意:这是一个R文件,不是Rnw文件。没有任何 LaTeX,只有 R 代码,我使用 R-Studio (Ctrl-Shift-K) 中的 "File > Knit" 命令。
RStudio 在这方面 运行 knitr's spin
function,因此您可以将块选项写入代码上方的 R 注释中 运行。例如:
#-name, fig.width=5, fig.height=5
png(file="filename.png", width=5, height=5, units="in", res=300)
dev.control(displaylist="enable")
plot(rnorm(10))
dev.off()
这将生成一个 5x5 图像: