Daniel Valencia C.
Daniel Valencia C.

Reputation: 2279

How to improve the quality of graphics made in R when they are copied to the clipboard as metafiles in R studio?

when I make graphics in R, I use specific settings for aesthetics. Saving graphics as PDF maintains the quality of almost all elements. But when I copy the graphics, as a metafile to the clipboard to use them in .doc or .docx documents, I lose all quality, even when I save the document as PDF

library (ggplot2)

Source <- c(rep("Water", 12), rep("Oil", 12))
Range <- rep((c(rep("First", 4), rep("Second", 8))),2)
Xaxis <- c(0,1,2,5,0,1,2,5,10,20,30,40,
           0,1,2,5,0,1,2,5,10,20,30,40)
Yaxis <- c(0,1,2,5,0,1,2,5,10,20,30,40,
           0,1,2,5,0,1,2,5,10,20,30,40)

DF <- data.frame(Source, Range, Xaxis, Yaxis)

#plot
p <- ggplot(data = DF, aes(x = Xaxis, y = Yaxis)) +
  geom_smooth(method = "lm") +
  geom_point() +
  facet_grid(Range~Source,
             scales = "free")

#Aesthetic settings
theme_set(
  theme_bw() +
    theme(line = element_line(colour = 1, size = 0.125),
          rect = element_rect(colour = 1, size = 0.125),
          text = element_text(colour = 1, size = 8),
          axis.title = element_text(face="bold", size = 8),
          axis.text = element_text(colour = 1, size = 8),
          axis.ticks = element_line(colour = 1),
          legend.title = element_text(face="bold", size = 8),
          legend.title.align = 0.5,
          legend.background = element_rect(fill = NA),
          legend.box.background = element_rect(colour = 1),
          panel.border = element_rect(colour = 1),
          strip.background = element_rect(colour = 1),
          strip.text = element_text(colour = 1, size = 8))
)

update_geom_defaults("point", list(size = 0.8))
update_geom_defaults("smooth", list(size = 0.125))
update_geom_defaults("linerange", list(size = 0.125))

p

In the R studio internal viewer

enter image description here

Plot saved as PDF (desired quality) enter image description here

Plot copied to the clipboard as a metafile and pasted into a .docx document

Crooked lines, different point sizes than expected. These aesthetic defects are maintained even when saving the .docx document as PDF enter image description here

Upvotes: 0

Views: 765

Answers (0)

Related Questions