arprater arprater - 1 month ago 13
R Question

How can I "generalize" what R uses as my x and y values in a plot

I have written an executable script in R that will simply plot a graph given an input file in a tab delimited format. However, the script I wrote is specific to a single file in terms of what to use as x and y. I want to have this script be able to plot whatever file I give it. All files I will be using for this script will be in the same format: Tab delimited with 4 headers with labels a, b, c, d. Labels b,c, and d have a different name for each file. My x values for the graph will be the values under header b and y values for the graph will be the values under header c. How can I plot a graph that will use whatever is under header b and c?

My script is posted below.

#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)
data = read.table((args[1]), header=TRUE, fill=TRUE, sep="\t")
attach (data)
jpeg(args[2])
plot (RPMb, RPMc)
dev.off()

Answer

Instead of using attach() (which is almost never recommended), use data frame indexing to extract the relevant variables from your data variable.

#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)
data = read.table((args[1]), header=TRUE, fill=TRUE,  sep="\t")
jpeg(args[2])
x <- names(data)[2]
y <- names(data)[3]
plot (data[[x]], data[[y]],xlab=x,ylab=y)
dev.off()

You could also just use plot(data[,2],data[,3]) ...

A couple of other details/comments:

  • it's generally best to avoid naming variables for built-in functions such as data. It will usually work, but occasionally it will bite you.
  • are you sure you want JPEG output? Either PNG or PDF are usually best for line graphs, depending on whether you need a raster or a vector format ...