[R] barplot and missing values?
Dan Bolser
dmb at mrc-dunn.cam.ac.uk
Sat Jun 4 15:13:54 CEST 2005
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.
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?
More information about the R-help
mailing list