[R] means and error bars on xyplot for binary data

Peter Ehlers ehlers at ucalgary.ca
Tue Jun 28 21:14:57 CEST 2011


On 2011-06-28 00:12, Louis Plough wrote:
> Hi,
> I have binary (0,1) data for a trait as my response variable, and
> a dependent variable, genotype, with three classes (AA, AB, BB).
>
> I would like to plot this data so that across the three genoytpes, even
> though the points are all either 0 or 1, i want them to stack up or be seen
> using 'jitter'.  So far I have been able to do this using xyplot {lattice}
> (code below) but could not get the points to jitter or stack up on boxplot
> or bwplot {lattice}.
>
>   I would also like to add to the xyplot object, the mean of the values at
> each of these classes  which will vary depending on how many data points are
> at 0 and 1 for a given genotype.  I would also like to put error (i.e.
> standard error) bars around these estimates.
>
> I have tried using the points() function to put the mean at each of the
> genotype classes, but the point ends up off the figure. Any ideas how to get
> this going?
>
> here is some example code.
>
>> gtype<-c("AA","AB","BB")
> x<-sample(gtype,20,replace=TRUE)
> y<-sample(c(0,1),20,replace=TRUE)
> bin.data<-data.frame(x,y)
> xyplot(y~x, jitter.y=TRUE, jitter.x=TRUE,factor=.6, data=bin.data)
>
> Then If I wanted to add the means to the plot, I would do this, which will
> print the mean points on a box plot, but not an xyplot:
>
> means1<- tapply(bin.data$y,bin.data$x,mean)
> points(means1,col="red",pch=18)
>
> Is there a way to get the means, and even error bars on the same xyplot?

I don't know why you want to do what you're doing, but it's
pretty easy with lattice (or with ggplot2). But do use
stripplot instead of xyplot. Define an appropriate panel
function to add the mean points and error bars:

   library(lattice)
   library(Hmisc) ## for error bars, using panel.xYplot

   mn <- with(bin.data, tapply(y, x, mean))
   M <- Cbind(mn, lower = mn - .1, upper = mn + .1) ## NB: capital 'C'

   stripplot(jitter(y, factor =.6) ~ x,
     data = bin.data,
     ylab = "",
     panel = function(...) {
       panel.stripplot(..., jitter=TRUE, factor=1.2, pch=1, cex=2)
       panel.xYplot(seq_along(mn), M, pch=16, cex=2, col="red")
     }
   )

Peter Ehlers

>
> Thanks for any help in advance,
> Louis
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list