[R] barplot and missing values?

Dan Bolser dmb at mrc-dunn.cam.ac.uk
Sat Jun 4 15:50:42 CEST 2005


On Sat, 4 Jun 2005, Uwe Ligges wrote:

>Dan Bolser wrote:
>
>> On Sat, 4 Jun 2005, Uwe Ligges wrote:
>> 
>> 
>>>Dan Bolser wrote:
>>>
>>>>I want to include missing values in my barplot to get the correct x-axis,
>>>>for example,
>>>>
>>>>x <- c(1,2,3,4, 9)
>>>>y <- c(2,4,6,8,18)
>>>>
>>>>barplot(y)
>>>>
>>>>The above looks wrong because the last height in y should be a long way
>>>>over.
>>>>
>>>>So I want to do something like...
>>>>
>>>>x <- c(1,2,3,4,5,6,7,8, 9)
>>>>y <- c(2,4,6,8,0,0,0,0,18)
>>>>
>>>>barplot(y)
>>>>
>>>>However... 
>>>>
>>>>I am actually using barplot2 to use the "log='y'" function, so I can't use
>>>>zero values on a log scale...
>>>>
>>>>So I need...
>>>>
>>>>x <- c(1,2,3,4, 5, 6, 7, 8, 9)
>>>>y <- c(2,4,6,8,NA,NA,NA,NA,18)
>>>>
>>>>barplot(y)
>>>>
>>>>But that don't work.
>>>
>>>
>>>Actually, it works, at least for me (R-2.1.0, WinNT, but you have not 
>>>told us your details!).
>>>
>>>BTW: In the meantime package gregmisc has been superseded by the 
>>>gregmisc bundle, and later on by a number of packages (such as gtools, 
>>>gdata, ...).
>>>
>>>Your setup seems to be rather outdated.
>> 
>> 
>> yeah :(
>> 
>> R 2.0.0 (2004-10-04).
>
>
>Hmm. I just tested with R-1.9.1, and your last example even works with 
>that one...
>
>
>> I will upgrade to 2.1.0 (latest stable?)
>> 
>> Instead of gregmisc what should I use to get barplot2?
>
>package "gplots"
>
>
>You example "y" is also handled perfectly well by barplot2() on my 
>system, BTW.
>


This must be because of the "log='y'" option that I am using here.

y <- c(2,4,6,8,NA,NA,NA,NA,18)

barplot2(y,log='y')

Above fails.


I appreciate that what I am trying to do is somewhat artificial (handle
zero values on a log scale), but it does reflect the data I have.

I tried plot(..., type='h'), but that dosn't do the "beside=T" stuff that
I want to do.

I am now trying things like...

barplot2(
  dat.y.plot + 0.11, # Dirty hack
  offset=-0.1,       #
  xpd=F,             #
  log='y',
  beside=T
)

Which looks messy. 

Any way to cleanly handle NA values with barplot2 on a log scale
(log='y')?



Thanks for your help :)



>Uwe Ligges
>
>
>
>
>> Will barplot() ever become barplot2()
>> 
>> I will try upgrading....
>> 
>> Dan.
>> 
>> 
>> 
>>>Uwe Ligges
>>>
>>>
>>>
>>>
>>>>Am I missing something?
>>>>
>>>>
>>>>To avoid confusion here is my data...
>>>>
>>>>
>>>>
>>>>>dat.y.plot
>>>>
>>>>   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
>>>>Ho  653   80  132   10   34    3   10    0    7     2     7     7
>>>>He  139   56   69    6   24    3   11    3    2     1     2     6
>>>>attr(,"names")
>>>> [1]
>>>>"2"   "3"   "4"   "5"   "6"   "7"   "8"   "9"   "10"  "11"  "12"  ">12"
>>>>[13] NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA   
>>>>
>>>>
>>>>
>>>>And here is what I call...
>>>>
>>>>barplot(dat.y.plot,
>>>>        ylim=c(0,max(dat.y.plot + 50)), # I don't like the default
>>>>        beside=T,
>>>>        names.arg=c('2','3','4','5','6','7','8','9','10','11','12','>12'),
>>>>        cex.axis=1.5,
>>>>        cex.names=1.5,
>>>>        legend=T
>>>>        )
>>>>
>>>>Which is fine (except I don't know why I still need names.arg).
>>>>
>>>>Then I try...
>>>>
>>>>
>>>>library(gregmisc)
>>>>
>>>>barplot2(dat.y.plot+1, log='y',
>>>>        beside=T,
>>>>        names.arg=c('2','3','4','5','6','7','8','9','10','11','12','>12'),
>>>>        cex.axis=1.5,
>>>>        cex.names=1.5,
>>>>        legend=T
>>>>        )
>>>>
>>>>Which fails because of the zero. If I try ...
>>>>
>>>>dat.y.plot[dat.y.plot==0] <- NA
>>>>
>>>>It fails because of the NA.
>>>>
>>>>Any suggestions?
>>>>
>>>>______________________________________________
>>>>R-help at stat.math.ethz.ch mailing list
>>>>https://stat.ethz.ch/mailman/listinfo/r-help
>>>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>>>
>




More information about the R-help mailing list