1

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)
Phil
  • 7,287
  • 3
  • 36
  • 66
luisa
  • 15
  • 1
  • 5
  • 1
    `ggsave` is meant to work with `ggplot` objets. The `plot()` function uses base R graphics and is not related so `ggplot` at all. So do you want to save base graphics? If so, see this duplicate: https://stackoverflow.com/questions/7144118/how-to-save-a-plot-as-image-on-the-disk. Otherwise you'll actually need to create your plot using the `ggplot` function and syntax in order to use `ggsave`. – MrFlick Feb 24 '22 at 04:19

1 Answers1

0

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

Ruam Pimentel
  • 1,288
  • 4
  • 16