How to save a plot as image on the disk?

后端 未结 11 1221
失恋的感觉
失恋的感觉 2020-11-22 10:38

I plot a simple linear regression using R. I would like to save that image as PNG or JPEG, is it possible to do it automatically? (via code)

There are two different

11条回答
  •  迷失自我
    2020-11-22 10:47

    For the first question, I find dev.print to be the best when working interactively. First, you set up your plot visually and when you are happy with what you see, you can ask R to save the current plot to disk

    dev.print(pdf, file="filename.pdf");
    

    You can replace pdf with other formats such as png.

    This will copy the image exactly as you see it on screen. The problem with dev.copy is that the image is often different and doesn't remember the window size and aspect ratio - it forces the plot to be square by default.

    For the second question, (as others have already answered), you must direct the output to disk before you execute your plotting commands

    pdf('filename.pdf')
    plot( yourdata )
    points (some_more_data)
    dev.off() # to complete the writing process and return output to your monitor
    

提交回复
热议问题