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

David Winsemius dwinsemius at comcast.net
Fri Jun 8 15:22:05 CEST 2012


On Jun 8, 2012, at 3:52 AM, maxbre wrote:

> thanks david,
> yes, you are right PART of the confusion is because of what you  
> mentioned
> (sorry for that) but going back to my own data this is JUST PART of  
> the
> problem…
>
> ...see my reproducible example
>
> teq<-structure(list(site = structure(c(4L, 2L, 2L, 4L, 2L, 4L, 4L,
> 3L, 1L, 3L, 1L, 1L, 3L, 4L, 5L, 4L, 3L, 1L, 3L, 1L, 4L, 4L, 2L,
> 4L, 4L, 2L, 1L, 3L, 4L, 4L, 2L, 4L, 4L, 2L, 4L, 4L, 2L, 4L, 4L,
> 2L, 4L, 2L, 4L, 2L, 4L, 2L, 4L, 2L, 2L, 2L, 2L, 4L), .Label = c("A",
> "B", "C", "D", "E"), class = "factor"), lower_b_i = c(0.302,
> 0.956, 0.72, 1.21, 0.887, 0.728, 1.294, 20.493, 0.902, 0.031,
> 0.468, 2.318, 4.795, 89.581, 4.59, 3.366, 32.786, 5.506, 61.038,
> 1.485, 42.25, 31.279, 21.375, 96.62, 36.099, 30.173, 0.532, 1.005,
> 0.697, 6.248, 2.121, 0.576, 0.335, 0.442, 22.704, 18.244, 16.53,
> 626.664, 42.714, 59.87, 9.983, 110.158, 4.763, 3.363, 2.608,
> 2.41, 23.904, 73.281, 3618.353, 109.663, 21.797, 3.39),  
> date_sampl_time_recs
> = structure(c(11L,
> 10L, 9L, 8L, 12L, 12L, 13L, 7L, 6L, 14L, 14L, 4L, 3L, 5L, 2L,
> 1L, 18L, 19L, 24L, 23L, 17L, 15L, 16L, 20L, 21L, 22L, 26L, 25L,
> 29L, 28L, 27L, 32L, 31L, 30L, 34L, 33L, 33L, 35L, 36L, 37L, 38L,
> 39L, 40L, 41L, 42L, 43L, 46L, 47L, 48L, 49L, 45L, 44L), .Label =
> c("2008-07-15, 2 h, 16/17",
> "2008-07-16, 2 h, 14/17", "2008-12-03, 4 h, 13/17", "2008-12-03, 4 h,
> 15/17",
> "2009-01-29, 24 h, 0/17", "2009-03-17, 24 h, 14/17", "2009-03-17, 24  
> h,
> 8/17",
> "2009-04-17, 135 h, 13/17", "2009-04-20, 96 h, 14/17", "2009-04-21,  
> 24 h,
> 13/17",
> "2009-04-21, 24 h, 14/17", "2009-07-17, 168 h, 13/17", "2009-07-21,  
> 24 h,
> 12/17",
> "2009-08-18, 24 h, 16/17", "2009-10-27, 168 h, 3/17", "2009-10-27,  
> 168 h,
> 5/17",
> "2009-11-01, 24 h, 4/17", "2009-12-15, 24 h, 6/17", "2009-12-15, 24 h,
> 9/17",
> "2010-01-05, 12 h, 3/17", "2010-01-15, 168 h, 3/17", "2010-01-15,  
> 168 h,
> 5/17",
> "2010-01-19, 24 h, 12/17", "2010-01-19, 24 h, 2/17", "2010-03-30, 24  
> h,
> 13/17",
> "2010-03-30, 24 h, 14/17", "2010-04-22, 168 h, 11/17", "2010-04-22,  
> 168 h,
> 9/17",
> "2010-04-22, 24 h, 14/17", "2010-07-22, 168 h, 14/17", "2010-07-22,  
> 168 h,
> 15/17",
> "2010-07-22, 24 h, 15/17", "2010-10-26, 168 h, 7/17", "2010-10-26,  
> 24 h,
> 8/17",
> "2011-01-05, 12 h, 0/17", "2011-01-11, 168 h, 0/17", "2011-01-19,  
> 168 h,
> 0/17",
> "2011-03-15, 168 h, 2/17", "2011-03-23, 168 h, 0/17", "2011-05-10,  
> 168 h,
> 5/17",
> "2011-05-18, 168 h, 7/17", "2011-07-05, 168 h, 7/17", "2011-07-13,  
> 168 h,
> 7/17",
> "2011-09-13, 168 h, 6/17", "2011-09-21, 168 h, 1/17", "2011-11-08,  
> 168 h,
> 0/17",
> "2011-11-16, 168 h, 0/17", "2012-01-05, 12 h, 1/17", "2012-01-09, 12  
> h,
> 2/17"
> ), class = "factor")), .Names = c("site", "lower_b_i",
> "date_sampl_time_recs"
> ), class = "data.frame", row.names = c(NA, -52L))
>
> ...and my code
>
> dotplot(date_sampl_time_recs ~ lower_b_i | site, data=teq,
>        scales=list(x=list(log=TRUE)),
>        xscale.components = xscale.components.logpower,
>        layout=c(5,1),
>        panel = function(x,y,...) {
>          panel.grid(h=53, v=-1, lty="dotted", col="gray")
>          panel.dotplot(x,y,...)
>          medians <- median(x)
>          panel.abline(v=medians, col.line="red", lty="dotted")
>          means<-mean(x)
>          panel.abline(v=means, col.line="blue", lty="dotted")
>        }
>        )
>
> attach(teq)
>
> #check median values
> tapply(lower_b_i,site,median)
 > tapply(lower_b_i,site,median)
       A       B       C       D       E
  1.1935 18.9525 12.6440  6.2480  4.5900
 > #check mean values
 > tapply(lower_b_i,site,mean)
         A         B         C         D         E
   1.86850 254.50619  20.02467  46.32865   4.59000

> #check mean values
 > #check mean values
 > tapply(lower_b_i,site,mean)
         A         B         C         D         E
   1.86850 254.50619  20.02467  46.32865   4.59000
> tapply(lower_b_i,site,mean)
>
> detach(teq)
>
>
> ...and now the question is why the plotting of means (but not  
> medians) is
> wrong (check against results of tapply)?
>

This is the context that you failed to include:

>> Part of the confusion may be that you have reversed the colors for  
>> mean and median in two different examples. The other confusion may  
>> be that mean(log(.)) != log(mean(.))

I do not see reference to which of those sources of confusion you  
believe you have adequately addressed. I suspect you may have only  
read the first sentence.

with(teq, tapply(lower_b_i,site,function(x) exp(mean(log(x) )) )  )
         A         B         C         D         E
  1.273034 12.084054  4.277616  7.151560  4.590000



-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list