Home > Blockchain >  How to write a line of code to save a plot as a png?
How to write a line of code to save a plot as a png?

Time:08-19

I'm trying to save a plot I made and tried using this code:

png(filename = "file_name.png",
    width = 500, height = 500)

I don't get any error message but the plot isn't getting saved, how can I get it to save to my files list?

CodePudding user response:

If you are using base R, first use your png code, then below it put the code for the plot (i.e. plot(...), then below that put dev.off() and see if that works by checking your directory. For example:

png(filename = "file_name.png",
    width = 500, height = 500)

plot(1:100, 1:100)

dev.off()

As opposed to base R, if you are using ggplot2 then you run the plot first, then run the ggsave command:

library(ggplot2)

ggplot(data.frame(x = 1:100, y = 1:100), aes(x = x, y = y))   geom_point()

ggsave(filename = "file_name_ggplt.png", width = 500, height = 500, units = "px")
  •  Tags:  
  • r
  • Related