Mark Miller
Mark Miller

Reputation: 13103

Creating Crisp or sharp graphics with R for importing into PowerPoint

This is probably a duplicate, although I cannot find a similar post with Google or via the Questions that may already have your answer feature on Stack Overflow.

I am unsure of the best way to create crisp or sharp graphics with R that can then be imported into, for example, PowerPoint.

Below is code that creates a PDF file. The resulting image is extremely fuzzy and of very poor quality when imported into PowerPoint.

Next I show code and instructions for creating nice crisp images using the Cairo package. However, this involves importing the resulting file into a third-party software called Inkscape and saving the file to a new format before importing the file into PowerPoint.

Is there a way to create nice sharp graphics that does not involve an intermediate step of importing files into a third-party software?

Thank you for any suggestions. Sorry if this is a duplicate. Here is the code:

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

Upvotes: 0

Views: 509

Answers (2)

Mark Miller
Mark Miller

Reputation: 13103

Here is R code that creates a file that seems equally crisp as an SVG file created with Inkscape. No intermediate editing of the TIFF file is needed. Just import the TIFF file created by R directly into 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()

Here is another approach that someone suggested to me. The resulting file from this png approach is smaller in size than a TIFF file and I guess may rescale better.

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

Upvotes: 0

David Gohel
David Gohel

Reputation: 10675

You can have a look at package 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")

Upvotes: 1

Related Questions