2

I am attempting to add names to the columns of my bar plot... There are 2 bars in each group that would share the same name..

I am using code like this:

l<-c(.6286, .2212, .9961, .5831, .8703, .6990, .9952, .9948)

r<-c(.2721, .5663, .0, .3961, .0696, .1180, .0, .0)

tab<-rbind(l,r)

plot<-barplot(tab, beside=TRUE, axisnames=FALSE, main = 'Time Spent Left vs. Right', sub = 'Female 2c', xlab= 'Days After Entry', ylab = 'Proportion of Time Spent', col=c('blue', 'red'), ylim = c(0,1))
legend('topleft', .8,  c('Left' , 'Right'), pch=c(.8), col=c('blue', 'red'))
names.arg = jd2c$d.in.p

names.arg=c(3,4,5,6,6,7,10,11) #listed for informational purposes, same as jd2c$d.in.p

jd2c$d.in.p is also listed above.. For some reason the names.arg function does not seem to be doing what I would expect. Even if I put it within the parenthesis of the bar plot() function..

What am I doing wrong?

Thank you!

David Robinson
  • 77,383
  • 16
  • 167
  • 187
Ross D.
  • 21
  • 1
  • 5

2 Answers2

1

Using your data and setting names.arg appropriately and axisnames=TRUE, I get a reasonable result. I tweaked a couple of other things (set las=1 to turn axis labels horizontal, used fill rather than col in the legend, got rid of pch=0.8).

par(las=1)
bnames <- c(3,4,5,6,6,7,10,11) #listed for informational purposes, same as jd2c$
plot<-barplot(tab, beside=TRUE, axisnames=TRUE,
              main = 'Time Spent Left vs. Right',
              sub = 'Female 2c', xlab= 'Days After Entry',
              ylab = 'Proportion of Time Spent',
              col=c('blue', 'red'), ylim = c(0,1),
              names.arg=bnames)
legend('topleft', cex=1,  c('Left' , 'Right'), fill=c('blue', 'red'))

enter image description here

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
1

You are defining names.arg not as an argument to the barplot function, but as a completely separate variable- it is outside the parentheses. This:

plot<-barplot(tab, beside=TRUE, axisnames=FALSE, main = 'Time Spent Left vs. Right', sub = 'Female 2c', xlab= 'Days After Entry', ylab = 'Proportion of Time Spent', col=c('blue', 'red'), ylim = c(0,1))
legend('topleft', .8,  c('Left' , 'Right'), pch=c(.8), col=c('blue', 'red'))
names.arg = jd2c$d.in.p

should be:

plot<-barplot(tab, beside=TRUE, axisnames=FALSE, main = 'Time Spent Left vs. Right', sub = 'Female 2c', xlab= 'Days After Entry', ylab = 'Proportion of Time Spent', col=c('blue', 'red'), ylim = c(0,1), names.arg = jd2c$d.in.p)
legend('topleft', .8,  c('Left' , 'Right'), pch=c(.8), col=c('blue', 'red'))

You must also set axisnames=TRUE for the names to appear.

David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • I do have have more question.. In setting up my initial plot, instead of listing values as a vector to plot, I would like to do it from the data set.. here what what I mean.. Data as vector (after rbind function): [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] l 0.6286 0.2212 0.9961 0.5831 0.8703 0.699 0.9952 0.9948 r 0.2721 0.5663 0.0000 0.3961 0.0696 0.118 0.0000 0.0000 – Ross D. Jul 17 '12 at 14:02
  • my data from dataset looks like this after rbind: [,1] [1,] 0.2721 [2,] 0.5663 [3,] 0.0000 [4,] 0.3961 [5,] 0.0606 [6,] 0.1180 [7,] 0.0000 [8,] 0.0000 [9,] 0.6286 [10,] 0.2212 [11,] 0.9961 [12,] 0.5831 [13,] 0.8703 [14,] 0.6990 [15,] 0.9952 [16,] 0.994 – Ross D. Jul 17 '12 at 14:04