bash 绘制和保存 R 图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11020223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plotting and saving R graph
提问by E.Cross
I have a bash script that tracks memory usage over time as a command is run. It spawns the desired command and then writes a log with column1 = "memory in use by program (gigs)" and column 2 is the time elapsed so far in seconds. e.g.
我有一个 bash 脚本,它在命令运行时跟踪内存使用情况。它生成所需的命令,然后写入一个日志,其中 column1 =“程序使用的内存(演出)”,第 2 列是到目前为止经过的时间(以秒为单位)。例如
31.282 1470
31.565 1480
31.848 1490
31.989 1500
32.273 1510
32.414 1520
32.697 1530
32.980 1540
33.122 1550
33.405 1560
6.511 1570
6.935 1580
7.502 1590
7.926 1600
8.351 1610
8.775 1620
9.059 1630
9.483 1640
9.908 1650
10.333 1660
What I want to do is wait until the process is complete and then use R to plot a graph of memory usage over time and save it in the current directory. I was playing around with R and I know exactly what commands I need to use:
我想要做的是等到进程完成,然后使用 R 绘制内存使用情况随时间变化的图表并将其保存在当前目录中。我在玩 R,我确切地知道我需要使用哪些命令:
> heisenberg <- read.csv(file="4644.log",head=FALSE,sep=" ")
> plot(heisenberg$V2,heisenberg$V1,type="o",col="red",main="Memory Usage Over Time",xlab="Time (seconds)",ylab="Memory (gigabytes)")
> text(max(heisenberg$V2),max(heisenberg$V1),max(heisenberg$V1)) #Displays max value
But the part I am stuck on is saving the graph as a jpg or png. Or how I could execute this command within my bash script. Would I absolutely need to have another script written in R language and run it? Would this be possible to do all in one?
但我坚持的部分是将图形保存为 jpg 或 png。或者我如何在我的 bash 脚本中执行这个命令。我是否绝对需要用 R 语言编写另一个脚本并运行它?这是否可以合而为一?
Edit
编辑
这是我的 script.r 的代码png("mem_usage_2965.png",height=800,width=800)
heisenberg <- read.csv(file="2965.log",head=FALSE,sep=" ")
plot(heisenberg$V2,heisenberg$V1,type="o",col="red",main="oases_k85",xlab="Time (seconds)",ylab="Memory (gigabytes)")
text(max(heisenberg),max(heisenberg),max(heisenberg))
dev.off()
Can anyone help as to why the text doesn't print the maximum value in the outputted png? I am calling it in a bash script like R CMD BATCH script.r script.out
任何人都可以帮助解释为什么文本不会在输出的 png 中打印最大值?我在像这样的 bash 脚本中调用它R CMD BATCH script.r script.out
回答by Gavin Simpson
Wrap your plot calls in:
将您的情节调用包装在:
jpeg("myplot.jpg")
....plot code here....
dev.off()
or
或者
png("myplot.png")
....plot code here....
dev.off()
See their respective help pages: ?pngfor details of other arguments.
请参阅它们各自的帮助页面:?png有关其他参数的详细信息。
For a PNG this would be:
对于 PNG,这将是:
png("my_plot.png", height = 800, width = 600)
plot(heisenberg$V2,heisenberg$V1,type="o",col="red",main="Memory Usage Over Time",xlab="Time (seconds)",ylab="Memory (gigabytes)")
text(max(heisenberg$V2),max(heisenberg$V1),max(heisenberg$V1)) #Displays max value
dev.off()
As for running this in a bash script, you need to invoke R to run your script containing the R code to load the data and draw the plots. For this there are several options, two are:
至于在 bash 脚本中运行它,您需要调用 R 来运行包含 R 代码的脚本以加载数据并绘制绘图。为此,有多种选择,其中两个是:
R CMD BATCH --no-save --no-restore my_script.R
or use Rscript
或使用 Rscript
Rscript my_script.R
where my_script.Ris a text file containing syntactically-valid R code required to produce the plots.
其中my_script.R是包含生成绘图所需的语法有效的 R 代码的文本文件。
回答by LanceH
Are you just looking at the simple? http://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/png.html
你只看简单的吗? http://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/png.html
you basically tell R to start saving a .png with:
你基本上告诉R开始保存一个.png:
png(file="blah.png")
then use:
然后使用:
dev.off()
to return to normal.
恢复正常。

