[R] how to add a vertical line for each panel in a lattice dotplot with log scale?

David Winsemius dwinsemius at comcast.net
Thu Jun 7 16:36:07 CEST 2012


On Jun 7, 2012, at 10:23 AM, maxbre wrote:

> thanks kimmo
>
> I managed to get the desired result by first plotting the medians  
> and then
> adding the means through the user defind function posted in thread you
> mentioned (here it is
> http://r.789695.n4.nabble.com/Adding-mean-line-to-a-lattice-density-plot-td4455770.html#a4456502)
>
> # start
>
> dotplot(variety ~ yield | site, data = barley,
>        scales=list(x=list(log=TRUE)),
>        layout = c(1,6),
>        panel = function(x,y,...) {
>          panel.dotplot(x,y,...)
>          median.values <- median(x)
>          panel.abline(v=median.values, col.line="red")
>          })
>
> addLine<- function(a=NULL, b=NULL, v = NULL, h = NULL, ..., once=F) {
>  tcL <- trellis.currentLayout()
>  k<-0
>  for(i in 1:nrow(tcL))
>    for(j in 1:ncol(tcL))
>      if (tcL[i,j] > 0) {
>        k<-k+1
>        trellis.focus("panel", j, i, highlight = FALSE)
>        if (once) panel.abline(a=a[k], b=b[k], v=v[k], h=h[k], ...)  
> else
>          panel.abline(a=a, b=b, v=v, h=h, ...)
>        trellis.unfocus()
>      }
> }
>
> mean.values<-tapply(barley$yield, barley$site, mean)
>
> addLine(v=log10(mean.values), once=TRUE, col="blue", lty="dotted")
>
> # end
>
> but back to my previous question I still not understand why the plot  
> of
> medians is working fine BUT NOT of the means (apparently messing up  
> panel
> positions and also values): no clue for this!

Can you explain what you mean by "messing up panel positions and also  
values"? When I execute this code with and without the two code lines  
for mean vertical lines I get expected results:

dotplot(variety ~ yield | site, data = barley,
        scales=list(x=list(log=TRUE)),
        layout = c(1,6),
        panel = function(x,y,...) {
          panel.dotplot(x,y,...)
          mean.values <- mean(x)   #omitted in second run
          panel.abline(v=mean.values, col.line="red")   #omitted in  
second run
          median.values <- median(x)
          panel.abline(v=median.values, col.line="blue")
          })

---------------
David.

sessionInfo()
R version 2.14.2 (2012-02-29)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] parallel  splines   stats     grDevices utils     datasets   
graphics  methods   base

other attached packages:
  [1] lme4_0.999375-42     Matrix_1.0-5         ggplot2_0.9.0         
forecast_3.19
  [5] RcppArmadillo_0.2.36 Rcpp_0.9.10          fracdiff_1.4-0        
tseries_0.10-27
  [9] quadprog_1.5-4       zoo_1.7-6            MASS_7.3-17           
circular_0.4-3
[13] boot_1.3-4           rms_3.5-0            Hmisc_3.9-2           
survival_2.36-12
[17] sos_1.3-1            brew_1.0-6           lattice_0.20-6

loaded via a namespace (and not attached):
  [1] cluster_1.14.2     colorspace_1.1-0   dichromat_1.2-4     
digest_0.5.1       fortunes_1.4-2
  [6] grid_2.14.2        memoise_0.1        munsell_0.3         
nlme_3.1-103       plyr_1.7.1
[11] proto_0.3-9.2      RColorBrewer_1.0-5 reshape2_1.2.1      
rgl_0.92.861       scales_0.2.0
[16] stats4_2.14.2      stringr_0.6        tools_2.14.2       vcd_1.2-12


>
> I've been trying also with the use of layout() in latticeExtra but  
> without
> results...
>
> anyone can clarify me these (strange for me) issues?
>
> max
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/how-to-add-a-vertical-line-for-each-panel-in-a-lattice-dotplot-with-log-scale-tp4632513p4632692.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list