[R] BarPlot

Marc Schwartz MSchwartz at mn.rr.com
Sun Oct 15 17:52:03 CEST 2006


On Sat, 2006-10-14 at 23:52 -0400, Mohsen Jafarikia wrote:
> Hello everyone,
> 
> I have the following program to draw a barplot.
> 
> 
> 
> MP<-read.table(file='AR.out')
> 
> names(MP)<-c('BN','BL','LR','Q')
> 
> Graph<- barplot(MP$LR, main='LR Value', col='orange', border='black', space=
> 0.05, width=(MP$BL), xlab='Length', ylab='LR each')
> 
> axis(1, at=Graph, sprintf('%0.2f',MP$BL))
> 
> mtext(1, at=Graph, text=sprintf('%0.2f',MP$Q), line=2)
> 
> mtext(1, at=par('usr')[1], text='BL', line=1)
> 
> mtext(1, at=par('usr')[1], text='Var', line=2)
> 
> abline(h=3.841,col='blue')
> 
> abline(h=6.635,col='red')
> 
> 
> 
> I have two more questions about the graph that I have:
> 
> 1) I want to write the 'Q' only when it is not equal to zero.
> 
> 2) I would like to change the bars when 'Q' is not zero. For example, from
> orange to green.
> 
> 
> 
> I would appreciate your input to this question.
> Thanks,
> 
> Mohsen

It would be helpful to have the data that you are working with so that
we can provide each other a working example.

However, here are some hints:

1.

  mtext(1, at = Graph, 
        text = ifelse(MP$Q != 0, sprintf('%0.2f',MP$Q), ""), 
        line=2)


2.

  cols <- ifelse(MP$Q != 0, "orange", "green")
  barplot(..., col = cols, ...)


See ?ifelse

HTH,

Marc Schwartz



More information about the R-help mailing list