【问题标题】:Creating Crisp or sharp graphics with R for importing into PowerPoint使用 R 创建清晰或锐利的图形以导入 PowerPoint
【发布时间】:2015-10-09 15:42:28
【问题描述】:

这可能是重复的,虽然我找不到与Google 或通过Stack Overflow 上的Questions that may already have your answer 功能类似的帖子。

我不确定使用 R 创建清晰或锐利图形的最佳方法,然后可以将其导入到,例如 PowerPoint

以下是创建PDF 文件的代码。导入到PowerPoint 时,生成的图像非常模糊且质量非常差。

接下来,我将展示使用 Cairo 包创建漂亮清晰图像的代码和说明。但是,这涉及将生成的文件导入名为Inkscape 的第三方软件,并在将文件导入PowerPoint 之前将文件保存为新格式。

有没有一种无需将文件导入第三方软件的中间步骤即可创建漂亮清晰的图形的方法?

感谢您的任何建议。抱歉,如果这是重复的。代码如下:

setwd('c:/users/markm/simple R programs/')

a <- seq(1,20)
b <- a^0.25
plot(a, b, bty = "l")

pdf("example_pdf_plot_Oct5_2015.pdf")
     plot(a, b, bty = "l")
     title('Example PDF Plot')
dev.off()

#
# After creating the file below with the Cairo package:
#
# 1. Install the free software 'Inkscape'
# 2. Open the *.svg file with Inkscape and save as a *.emf file.
# 2b. Click 'OK' when asked to convert texts to paths.
# 2c. Click 'Close without saving'
# 3. Import the *.emf file into PowerPoint as a picture.
# 4. Resize and position image in PowerPoint to taste.
#
# install.packages('Cairo')
library(Cairo)

CairoSVG('example_svg_plot_Oct5_2015.svg', onefile = TRUE, pointsize = 12, width=8, height=6, bg="white")
     plot(a, b, bty = "l")
     title('Example SVG Plot')
dev.off()

【问题讨论】:

  • 如果您在 R Markdown 中构建您的 R 脚本,您可以在 knitr 选项中设置 fig.retina=2,它会为您完成所有的点大小(等)机制。如果你很好地命名代码块,你也会得到好的文件名。
  • @hrbrmstr 我不熟悉R Markdown。那是像R Studio 这样的图形用户界面吗?也许它是R Studio 的一部分?
  • @hrbmstr: fig.retina=2 也是 LaTeX 中的命令吗?我正在尝试使用 .Rnw 脚本提高 .png 文件的质量。

标签: r svg plot


【解决方案1】:

你可以看看包ReporteRs

library(ReporteRs)

my_graph_fun <- function( ){
  a <- seq(1,20)
  b <- a^0.25
  plot(a, b, bty = "l")  
}

doc = pptx()
doc = addSlide(doc, "Title and Content")
doc = addPlot(doc, fun = my_graph_fun )
writeDoc(doc, "example.pptx")

【讨论】:

    【解决方案2】:

    这是R 代码,它创建的文件看起来与使用Inkscape 创建的SVG 文件一样清晰。不需要对TIFF 文件进行中间编辑。只需将R创建的TIFF文件直接导入PowerPoint即可。

    tiff(file = "example_tiff_plot_Oct5_2015.tiff", 
         compression= "lzw", width = 8, height = 6, res = 500, unit = "in", pointsize = 12)
    
         plot(a, b, bty = "l")
         title('Example Tiff Plot')
    
    dev.off()
    

    这是有人向我建议的另一种方法。这种png 方法生成的文件比TIFF 文件更小,我想可能会更好地重新缩放。

    a <- seq(1,20)
    b <- a^0.25
    
    png(file = "example_png_plot_Oct6_2015.png", 
         width = 8, height = 6, res = 500, unit = "in", pointsize = 12)
    
         plot(a, b, bty = "l")
         title('Example Png Plot')
    
    dev.off()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 2021-12-06
      • 2011-09-02
      • 1970-01-01
      • 2014-07-14
      • 1970-01-01
      • 2013-10-01
      相关资源
      最近更新 更多