Plotting and saving R graph

12,043

Solution 1

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: ?png for details of other arguments.

For a PNG this would be:

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:

R CMD BATCH --no-save --no-restore my_script.R

or use Rscript

Rscript my_script.R

where my_script.R is a text file containing syntactically-valid R code required to produce the plots.

Solution 2

Are you just looking at the simple? http://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/png.html

you basically tell R to start saving a .png with:

png(file="blah.png")

then use:

dev.off()

to return to normal.

Share:
12,043

Related videos on Youtube

E.Cross
Author by

E.Cross

Updated on September 18, 2022

Comments

  • E.Cross
    E.Cross over 1 year

    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.

    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:

    > 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?


    Edit

    Here is the code for my 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

  • E.Cross
    E.Cross almost 12 years
    So is there a way to do this within my bash script? Is it possible to use R in command-line?
  • Gavin Simpson
    Gavin Simpson almost 12 years
    See ?RScript for details of a scripting interface to R. There are other ways, i.e. using R CMD BATCH. But this is another Question so you should ask another Question on that topic.
  • Gavin Simpson
    Gavin Simpson almost 12 years
    @LanceH Not within a bash script you can't, you need to invoke R to run R code. You could combine your snippet with RScript for example to invoke R and run the contents of the .r file.
  • E.Cross
    E.Cross almost 12 years
    R CMD BATCH seems like what I am looking for. I could output an r script via echoing within my bash script. I may then be able to do something like call the script somehow.
  • Gavin Simpson
    Gavin Simpson almost 12 years
    Rscript is probably better suited.
  • E.Cross
    E.Cross almost 12 years
    I am having trouble showing the maximum value on the plot. When I open the saved png there is no text.
  • Gavin Simpson
    Gavin Simpson almost 12 years
    @Dan can you post the image somewhere so we can take a look, and it would help if we had a reproducible example.