[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 17:49:20 CEST 2012
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
More information about the R-help
mailing list