Save all plots already present in the panel of Rstudio

后端 未结 5 1322
一生所求
一生所求 2021-02-02 12:10

I\'ve made different plots (more than a hundred) for a project and I haven\'t capture them on the way (yes it\'s bad , i know). Now, I need to save them all at once but without

5条回答
  •  名媛妹妹
    2021-02-02 12:26

    Although this discussion has been inactive for a while, there are some persons, like myself, who still come across the same problem, and the other solutions don't really seem to even get what the actual question is.

    So, hands on. Your plot history gets saved in a variable called .SavedPlots. You can either access it directly, assign it to another variable in code or do the latter from the plots window.

    # ph for plot history
    ph <- .SavedPlots
    

    In R 3.4.2, I could index ph to reproduce the corresponding plot in a device. What follows is rather straightforward:

    1. Open a new device (png, jpeg, pdf...).
    2. Reproduce your plot ph[index_of_plot_in_history].
    3. Close the device (or keep plotting if it is a pdf with multiple pages).

    Example:

    for(i in 1:lastplot) {
        png('plotname.png')
        print(ph[i])
        dev.off()
    }
    

    Note: Sometimes this doesn't happen because of poor programming. For instance, I was using the MICE package to impute many datasets with a large number of variables, and plotting as shown in section 4.3 of this paper. Problem was, that only three variables per plot were displayed, and if I used a png device in my code, only the last plot of each dataset would be saved. However, if the plots were printed to a window, all the plots of each dataset would be recorded.

提交回复
热议问题