1
votes

I'm trying to make a histogram comparing two datasets using R. I haven't yet found a way to do this, however. Each dataset is a 69 element-long numeric list of gene counts. The first ten elements look like this:

bt549_counts
##       1       2       3       4       5       6       7       8       9      10      
##  627984  711915  690967 1414629 1097368  274380  432299  165781  329155 1984503
bt549_uniqCounts
##    1    2    3    4    5    6    7    8    9   10  
## 1794 1982 1921 3556 2851  800 1142  457 1013 4911

I've tried to plot each list and then to combine those lists into one plot like so:

btPlot <- plot(bt549_counts, type="h")
uniqPlot <- plot(bt549_uniqCounts, type="h")
combPlot <- t(cbind(bt549_counts,bt549_uniqCounts))
barplot(combPlot, beside=TRUE, space=rep(0,2*ncol(combPlot)), col=c("red", "black"))

Here are the first two plots:

btPlot:

btPlot

uniqPlot:

uniqPlot

But the resulting graph does not appear to show both sets:

result

I had expected to see two sets of bars, one black and one red, side by side. What am I reading wrong here & how can I make my desired graph?

PS: Sorry, I can't seem to get the two lists to display correctly. The first line, [1-10], is the column numbers and the second line contains the value for each column.

1

1 Answers

3
votes

If you plot your test data, you will see it's working. The problem is you're plotting counts and the counts are so different (bt549_uniqCounts is much much smaller so you barely see it). I've added border=F so it doesn't draw a black line around the bar and cover up the fill. If you look hard, you can see a bit of red by 5.

cnt1<-c(627984,711915,690967,1414629,1097368,274380,432299,165781,329155,1984503)
cnt2<-c(1794,1982,1921,3556,2851,800,1142,457,1013,4911)
names(cnt1)<-1:10
names(cnt2)<-1:10
barplot(t(cbind(cnt1, cnt2)), beside=T, col=c("black","red"), border=F)

standard bar

Perhaps you'd like to normalize them to compare relative frequencies. In this case, you can divide each by it's sum. This gives

barplot(t(cbind(cnt1/sum(cnt1), cnt2/sum(cnt2))), 
    beside=T, col=c("black","red"), border=F)

normalized bar