2015-10-05 123 views
0

這可能是重複的,但我找不到GoogleStack Overflow上的Questions that may already have your answer功能的類似帖子。使用R創建清晰或清晰的圖形以導入到PowerPoint中

我不確定用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() 
+0

如果你建立R中降價的[R腳本,你可以在knitr選項中設置'fig.retina = 2',它會做所有的點大小(等)爲你加工。如果您將代碼塊命名得很好,您也可以獲得很好的文件名。 – hrbrmstr

+0

@hrbrmstr我不熟悉'R Markdown'。這是一個類似'R Studio'的GUI嗎?也許它是'R Studio'的一部分? –

+0

https://cran.rstudio.com/web/packages/rmarkdown/index.html – hrbrmstr

回答

0

這裏是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() 
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")