1

I think I might be overcomplicating this in my mind. I have a file that I want to use to make a barplot in R. The file looks like this:

## metadata
# filepath
## filename
# Started on: Tue Jul 30 10:46:57 EDT 2013


#HISTOGRAM    java.lang.Integer
READ:  1    2
-1  28  28
 0  27  29

I want to make a bar graph of the data I have. I was thinking it would look somewhat like this:

  |
  |
# |             |   
  |  |          |
  |  |     |    |
  |__|_____|____|___|____
  -1(1) -1(2) 0(1) 0(2)

But I am having trouble reading in the data correctly. This is my code:

# Parse the arguments
args <- commandArgs(trailing=T)
#Chart file
metricsFile  <- args[1]
#pdf file path and name that I want to produce
outputFile   <- args[2]

# Figure out where the firstLine and the chart are in the file and parse them out
startFinder <- scan(metricsFile, what="character", sep="\n", quiet=TRUE, blank.lines.skip=FALSE)
firstBlankLine=0
#finds empty strings
for (i in 1:length(startFinder))
{
        if (startFinder[i] == "") {
                if (firstBlankLine==0) {
                        firstBlankLine=i+1
                } else {
                        secondBlankLine=i+1
                        break
                }
        }
}

 #I accept some args and I read past some header information 
firstLine <- read.table(metricsFile, header=T, nrows=1, sep="\t", skip=firstBlankLine)
#prints "firstLine:  -1" "firstLine:  28" "firstLine:  28" 
secondLine <- read.table(metricsFile, header=T, nrows=1, sep="\t", skip=secondBlankLine)
#prints "secondLine:  -1" "secondLine:  27" "secondLine:  29"

# Then plot as a PDF
pdf(outputFile)
#I am just inputing firstLine here because I was trying to see how it works
#I get the error:'height' must be a vector or a matrix
barplot(firstLine,
        main=paste("Read",(i-1)," Distribution ",
        xlab="Counts",
        ylab="F/R Orientation")

dev.off()

As I commented in my code I get the error that 'height' must be a vector or matrix. I don't know R well enough to know how to read it in as a vector rather than how I am currently doing it. I also am not sure if perhaps the problem is that I am using barplot. Could it be that just 'plot' is better to use? Perhaps I am using read.table incorrectly?

Stephopolis
  • 1,765
  • 9
  • 36
  • 65
  • can you make your example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Otherwise, it will be impossible to debug. Include the exact input file, or a portion of it as well as all the extra variables you're using... you could also include `dput(firstLine)` which will help a lot. – Justin Jul 30 '13 at 15:32

1 Answers1

0

You could use ?unlist to "convert" your data.frame into a numeric vector:

d <- read.table(file="YOURFILE", sep="\t", header=TRUE, row.names=1)
barplot(unlist(d))

enter image description here

Maybe you have to adjust the labels.

BTW: You could avoid your firstline and secondline part. read.table will skip comments (lines starting with #) and blank lines automatically (see ?read.table for details).

sgibb
  • 25,396
  • 3
  • 68
  • 74
  • Thanks so much. This actually really streamlined my code. I didn't see that readtable would skip comments and blanks. – Stephopolis Jul 30 '13 at 17:03