[R] lattice density plot: add vertical lines at groupwise medians for all panels

AnjaM a.mirenska at gmail.com
Tue Nov 20 15:13:07 CET 2012


Suppose you have the following code:

########## Start code########## 

data(Chem97, package="mlmRev")

densityplot(~gcsescore | factor(score), groups=gender, data=Chem97,
            auto.key=TRUE,  plot.points=FALSE, ref=TRUE,
            panel=function(x,...){
              panel.densityplot(x,...)
              median.values <- median(x) 
              panel.abline(v=median.values)}
)

##########  End code########## 

For some reason I don't understand, this adds only the median for one group
(in this case for gender == M) into each panel. How do I calculate and add
the medians for both groups in the right group colour?

This is what I tried:

########## Start code########## 

densityplot(~gcsescore | factor(score), groups=gender, data=Chem97,
            auto.key=TRUE,  plot.points=FALSE, ref=TRUE,
            panel=function(x,..., groups){
              panel.densityplot(x,..., groups)
              median.values <- tapply(x, groups, median) 
              panel.abline(v=median.values)}
)

##########  End code########## 

However, this gives an error.  What's going wrong and how can I solve this
problem?



--
View this message in context: http://r.789695.n4.nabble.com/lattice-density-plot-add-vertical-lines-at-groupwise-medians-for-all-panels-tp4650163.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list