maxbre
2012-Jun-06 12:10 UTC
[R] how to add a vertical line for each panel in a lattice dotplot with log scale?
by considering this example from barley dataset #code start dotplot(variety ~ yield | site, data = barley, scales=list(x=list(log=TRUE)), layout = c(1,6), panel = function(...) { panel.dotplot(...) #median.values <- tapply(x, y, median) # medians for each variety #panel.abline(v=median.values, col.line="red") # but this is not working! #panel.curve(...) # how to properly set this? } ) #code end I want to plot as a reference vertical line the medians for each panel I?ve been reading in https://stat.ethz.ch/pipermail/r-help/2009-January/185384.html that it?s not possible with panel.abline() and it?s probably necessary to use panel.curve() instead; unfortunately I can?t figure (manage) how, any help for this? thank you -- 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-tp4632513.html Sent from the R help mailing list archive at Nabble.com.
ilai
2012-Jun-06 14:39 UTC
[R] how to add a vertical line for each panel in a lattice dotplot with log scale?
You say median for each panel but tapply gets medians for each variety (chartjunk IMHO). Regardless, *this case* has nothing to do with panel.abline. Add print(median.values) to your panel function would have hinted as to the missing piece. # medians for each panel: 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") }) # medians for each variety 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 <- tapply(x, y, median) panel.abline(v=median.values, col.line="red") } ) Cheers On Wed, Jun 6, 2012 at 6:10 AM, maxbre <mbressan@arpa.veneto.it> wrote:> by considering this example from barley dataset > > #code start > > dotplot(variety ~ yield | site, data = barley, > scales=list(x=list(log=TRUE)), > layout = c(1,6), > panel = function(...) { > panel.dotplot(...) > #median.values <- tapply(x, y, median) # medians for each > variety > #panel.abline(v=median.values, col.line="red") # but this is not > working! > #panel.curve(...) # how to properly > set this? > } > ) > > #code end > > I want to plot as a reference vertical line the medians for each panel > > I’ve been reading in > https://stat.ethz.ch/pipermail/r-help/2009-January/185384.html > that it’s not possible with panel.abline() and it’s probably necessary to > use panel.curve() instead; unfortunately I can’t figure (manage) how, any > help for this? > > thank you > > > > > -- > 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-tp4632513.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
maxbre
2012-Jun-07 07:09 UTC
[R] how to add a vertical line for each panel in a lattice dotplot with log scale?
thanks ilai sorry, I mixed up a little: I was thinking to medians of each panel but instead I was trying to plot medians for each variety (what an awful chart, indeed!) thanks for your solution (medians for each panel), it works perfectly, as usual... cheers 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-tp4632513p4632641.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2012-Jun-07 15:49 UTC
[R] how to add a vertical line for each panel in a lattice dotplot with log scale?
On Jun 7, 2012, at 11:34 AM, maxbre wrote:> a new session of R with the following sessionInfo() > >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(.))> this is the code I run > > #start > > library(lattice); library(latticeExtra) > > > #example with user function > > 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() > } > } > > > 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") > }) > > mean.values<-tapply(barley$yield, barley$site, mean) > > addLine(v=log10(mean.values), once=TRUE, col="blue") > > > # example with panel.abline > > 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") > }) > > #end > > this are the two different results I?ve got: > > example with user defined function > http://r.789695.n4.nabble.com/file/n4632706/example_with_user_function.png > > example with panel.abline > http://r.789695.n4.nabble.com/file/n4632706/example_with_panel_abline.png > > and now I?m really confused of what I?m doing and seeing? > > > > -- > 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-tp4632513p4632706.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
maxbre
2012-Jun-08 07:52 UTC
[R] how to add a vertical line for each panel in a lattice dotplot with log scale?
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) #check mean values 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)? thanks -- 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-tp4632513p4632760.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2012-Jun-08 13:22 UTC
[R] how to add a vertical line for each panel in a lattice dotplot with log scale?
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