【问题标题】:In knitting Rmd file, print 2 plots in same row, but create separate pdfs在编织 Rmd 文件中,在同一行打印 2 个图,但创建单独的 pdf
【发布时间】:2015-12-31 07:50:07
【问题描述】:

我在 Rmd 文件中有两个图,我想在针织输出中并排绘制它们。我还想将各个图保存到单独的 pdf 中。当我每台设备只有一个绘图时,dev.copy2pdf 可以很好地避免重新绘图,我愿意不惜一切代价这样做。

但是,以下代码会生成两个 pdf,这两个都不是我想要的输出。第一个pdf是页面左半部分的第一个图;第二个pdf是并排的图。我明白为什么会发生这种情况——毕竟它是直接从当前设备复制的,但我不确定如何修改我的代码以达到我想要的结果。

data(cars)
par(mfrow=c(1,2))
plot(cars$Price,cars$Mileage)
dev.copy2pdf(file = "price-mileage.pdf")
plot(cars$Price,cars$Doors)
dev.copy2pdf(file = "price-doors.pdf")

【问题讨论】:

    标签: r pdf plot rstudio


    【解决方案1】:

    我看不到一种方法可以让您一步完成您的要求。但是,如果这是您的意思,您可以在不重新绘制 knitr 的情况下做到这一点。

    ```{r}
    data(iris)
    ```
    
    This will create your side by side plots in knitr:
    
    ```{r fig.width=7, fig.height=6}
    par(mfrow=c(1,2))
    plot(iris$Sepal.Length,iris$Sepal.Width)
    plot(iris$Sepal.Length,iris$Petal.Length)
    ```
    
    ```{r include=F}
    #This will write your plots to the individual files.  
    #It will not appear in the knitr because include=F
    pdf("plot1.pdf")
    plot(iris$Sepal.Length,iris$Sepal.Width)
    dev.off()
    pdf("plot2.pdf")
    plot(iris$Sepal.Length,iris$Petal.Length)
    dev.off()
    ```
    

    【讨论】:

    • 我忘记了包含选项,感谢您指出。不幸的是,我坚持不重新绘制的原因是因为生成绘图需要时间,这似乎相当重要(它是通过合作者编写的自定义热图函数)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 2010-11-10
    • 2015-12-30
    • 1970-01-01
    相关资源
    最近更新 更多