rggplot2ggsave

Why is ggsave not saving my plot to my computer?


Hi I'm trying to save high quality (300 dpi) images using RStudio but no luck so far. I've looked around a lot on the internet but no answer seems to work. Even when I run the code below, no file shows up on my computer. Any help is appreciated!

install.packages("gapminder")
library(gapminder)
data("gapminder")

attach(gapminder)

plot(lifeExp ~ log(gdpPercap))
ggsave("filename.png",dpi = 300)

Solution

  • It works fine if you use ggplot() from ggplot2 instead of plot()

    Packages and data

    library(ggplot2)
    library(gapminder)
    
    data("gapminder")
    attach(gapminder)
    

    Solution

    ggplot(gapminder,
           aes(x =  log(gdpPercap), y = lifeExp)) +
      geom_point()
    
    ggsave("filename.png",dpi = 300)
    

    Here are some tweaks you came make to make it more similar to plot() appearance:

    ggplot(gapminder,
           aes(x =  log(gdpPercap), y = lifeExp)) +
      geom_point(shape = 1) +
      theme_linedraw()
    

    output from last code

    enter image description here