Search code examples
rggplot2plotr-markdownsave

How to save a plot in Rmarkdown?


I am currently working on a project where I need to extract pngs from a RMarkdown, i have tried to use ggsave but it returns an error about $, but i havent used them in my prompt.

I tried this for now :

chene_cor = cor(Chene_num)
chene_cor
sauve = corrplot(chene_cor)
ggsave(filename = "corrplotchene", plot = "sauve", device = "svg")

When I run this code, I get this error :

Error in x$theme : $ operator is invalid for atomic vectors

Any ideas on how to fix that ? If not, any other ideas to save plots in Rmd ?

Thanks !


Solution

  • Just run ggsave() without the plot parameter. See below:

    ggsave(filename = "corrplotchene", device = "svg")
    

    ggsave() automatically fetches and saves the last plot.