I have made a customized theme to a ggplot2 scatterplot, but when I try to save my plot to a PDF, the fonts go back to default font, and the grid lines and panel color also go back to default.
The question is: How can I get exactly what I see in my RStudio plot viewer to a high-res PDF?
Pictures: The first picture below shows what I want to have (how it looks in RStudio); the second picture shows the PDF with the wrong layout.
THIS is what I want (the RStudio plot),
but THIS is what I get
The customized ggplot2-theme is as follows:
windowsFonts("latex" = windowsFont("CMU Serif"))
mytheme <- theme(text = element_text(family = "latex"),
panel.grid.minor = element_blank(),
panel.grid.major = element_line(
colour="grey97",
size = (0.01)),
panel.background= element_blank(),
strip.text.x = element_text(size=12),
strip.background = element_rect(fill="lightgrey",
color = "black",
size = 0.5),
panel.border = element_rect(color = "black",
fill = NA,
size = 0.5))
And my ggsave-code is as follows:
ggsave("scatterplot.pdf",
plot = plot,
device = "pdf",
path = "[my path]",
dpi = 320)
Is there anything I can add to the ggsave-code so that it can save the exact output from the RStudioplot?
plot <- ggplot() + mytheme?
beforeggsave(plot)
- tom