撒拉嘿哟木头 2011-08-22 07:06 采纳率: 100%
浏览 423
已采纳

如何保存为磁盘上的图像?

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 questions: First, I am already looking at the plot on my monitor and I would like to save it as is. Second, I have not yet generated the plot, but I would like to directly save it to disk when I execute my plotting code.

转载于:https://stackoverflow.com/questions/7144118/how-to-save-a-plot-as-image-on-the-disk

  • 写回答

11条回答 默认 最新

  • 斗士狗 2011-08-22 07:15
    关注

    There are two closely-related questions, and an answer for each.


    1. An image will be generated in future in my script, how do I save it to disk?

    To save a plot, you need to do the following:

    1. Open a device, using png(), bmp(), pdf() or similar
    2. Plot your model
    3. Close the device using dev.off()

    Some example code for saving the plot to a png file:

    fit <- lm(some ~ model)
    
    png(filename="your/file/location/name.png")
    plot(fit)
    dev.off()
    

    This is described in the (combined) help page for the graphical formats ?png, ?bmp, ?jpeg and ?tiff as well as in the separate help page for ?pdf.

    Note however that the image might look different on disk to the same plot directly plotted to your screen, for example if you have resized the on-screen window.


    Note that if your plot is made by either lattice or ggplot2 you have to explicitly print the plot. See this answer that explains this in more detail and also links to the R FAQ: ggplot's qplot does not execute on sourcing


    2. I'm currently looking at a plot on my screen and I want to copy it 'as-is' to disk.

    dev.print(pdf, 'filename.pdf')
    

    This should copy the image perfectly, respecting any resizing you have done to the interactive window. You can, as in the first part of this answer, replace pdf with other filetypes such as png.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(10条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集