Skip to content

Boxplot lattice vs standard graphics

7 messages · Bert Gunter, Massimo Bressan, David Winsemius +1 more

#
Given my reproducible example

test<-structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L), .Label = c("A", 
"B", "C", "D", "E"), class = "factor"), conc = c(2.32, 0.902, 
0.468, 5.51, 1.49, 0.532, 0.72, 0.956, 0.887, 20, 30, 2.12, 0.442, 
10, 50, 110, 3.36, 2.41, 20, 70, 3610, 100, 4.79, 20, 0.0315, 
30, 60, 1, 3.37, 80, 1.21, 0.302, 0.728, 1.29, 30, 40, 90, 30, 
0.697, 6.25, 0.576, 0.335, 20, 10, 620, 40, 9.98, 4.76, 2.61, 
3.39, 20, 4.59)), .Names = c("site", "conc"), row.names = c(NA, 
52L), class = "data.frame")



And the following code

#standard graphics
with(test,boxplot(conc~site, log="y"))

#lattice
bwplot(conc~site, data=test,
       scales=list(y=list(log=10))
       )

There is an evident difference for site A, B, D in the way some outliers are
plotted by comparing the plot produced by lattice vs. the standard graphics

I think to understand this might be due to the different treatment of data:
i.e. log transformation (before or after the plotting?)

Is it possible to achieve the same plotting result with both graphic
facilities? 
I would like to show the outliers also in lattice?

Thank you

http://r.789695.n4.nabble.com/file/n4643121/standard.png 

http://r.789695.n4.nabble.com/file/n4643121/lattice.png 





--
View this message in context: http://r.789695.n4.nabble.com/Boxplot-lattice-vs-standard-graphics-tp4643121.html
Sent from the R help mailing list archive at Nabble.com.
#
Thanks for the example. Makes it easy to see what you mean.

Yes, if I understand you correctly, you are right:
boxplot() (base) transforms the axes, so ?boxplot.stats, which is the
function that essentially computes the boxplot, does so on the
original data.
bwplot(lattice) transforms the data first, as the documentation for
the "log" component of the scales list makes clear, and **then** calls
boxplot.stats.

Although I think the latter makes more sense then the former, I think
the way to do it is to modify the "stats" function in an explicit call
to panel.bwplot to something like (UNTESTED!)
mystats <- function(x){
out <- boxplot.stats(10^x)
out$stats <- log10(out$stats)
out$conf <- log10(out$conf) ## Omit if you don't want notches
out$out <- log10(out$out)
out ## With the boxplot statistics converted to the log10 scale
}

I leave it to you to test and modify as necessary.

Cheers,
Bert
On Fri, Sep 14, 2012 at 2:37 AM, maxbre <mbressan at arpa.veneto.it> wrote:

  
    
2 days later
#
thank you for the help, bert

unfortunately, for reasons I can not understand (yet) I can not put to 
wortk it all
(I'm always in trouble with the panel functions);

max

Il 14/09/2012 18:38, Bert Gunter ha scritto:
#
here it is, I think (I hope)  I'm getting a little closer with this, but
still there is something  to sort out...
 
error using packet 1
unused argument(s)  (coef =1.5, do.out=TRUE)

by reading the help for panel.bwplot at the argument "stats" it says: "the
function must accept arguments coef and do.out even if they do not use them
(a ... argument is good enough). "
I'm not sure how to couple with this...

any help for this ?

thanks


## start code


mystats <- function(x){
  out <- boxplot.stats(10^x)
  out$stats <- log10(out$stats)
  out$conf <- log10(out$conf) ## Omit if you don't want notches
  out$out <- log10(out$out)
  out$coef<-1.5 #??
  out$do.out<-"TRUE" #??
  out ## With the boxplot statistics converted to the log10 scale
}

bwplot(conc~site, data=test,
       scales=list(y=list(log=10)),
       panel= function(x,y){
         panel.bwplot(x,y,stats=mystats)
       }         
       )


## end code



--
View this message in context: http://r.789695.n4.nabble.com/Boxplot-lattice-vs-standard-graphics-tp4643121p4643357.html
Sent from the R help mailing list archive at Nabble.com.
#
On Sep 17, 2012, at 4:18 AM, maxbre wrote:

            
No example data, so no efforts at running code.

?panel.bwplot

# Notice the Usage at the top of the page. The "..." is there for a reason.

# And notice that neither 'do.out' nor 'coef' are passed in the "stats" list

# The message was talking about what arguments your 'mystats' would accept, .... not what it would return. It's another instance of your needing to understand what the "..." formalism is doing.

?boxplot.stats

# I would be making a concerted effort to return a list with exactly the components listed there.
#
Hello,

Em 17-09-2012 18:50, David Winsemius escreveu:
Actually there is, in the op.
And since I'm terrible at graphics I try to learn as much as possible on 
R-Help. Here it goes.


library(lattice)

test<-structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L), .Label = c("A",
"B", "C", "D", "E"), class = "factor"), conc = c(2.32, 0.902,
0.468, 5.51, 1.49, 0.532, 0.72, 0.956, 0.887, 20, 30, 2.12, 0.442,
10, 50, 110, 3.36, 2.41, 20, 70, 3610, 100, 4.79, 20, 0.0315,
30, 60, 1, 3.37, 80, 1.21, 0.302, 0.728, 1.29, 30, 40, 90, 30,
0.697, 6.25, 0.576, 0.335, 20, 10, 620, 40, 9.98, 4.76, 2.61,
3.39, 20, 4.59)), .Names = c("site", "conc"), row.names = c(NA,
52L), class = "data.frame")


#standard graphics
dev.new()
with(test,boxplot(conc~site, log="y"))

#lattice
mystats <- function(x, ...){ # Here ...
     out <- boxplot.stats(10^x, ...)  # ...and here!!!
     out$stats <- log10(out$stats)
     out$conf <- log10(out$conf) ## Omit if you don't want notches
     out$out <- log10(out$out)
     out ## With the boxplot statistics converted to the log10 scale
}

dev.new()
bwplot(conc~site, data=test,
        scales = list(y=list(log=10)),
        panel = function(...){
          panel.bwplot(..., stats = mystats)
        }
)

With a median _line_ it would be perfect.
(Not a follow-up, it was already answered some time ago, use pch = "|" 
in panel.bwplot.)

Rui Barradas
#
ok, I see now!
here it is the reproducible example along with the final code (aslo with 
the median line instead of a point)

thank you all for the great help

max

# start code

library(lattice)

test<-structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L), .Label = c("A",
"B", "C", "D", "E"), class = "factor"), conc = c(2.32, 0.902,
0.468, 5.51, 1.49, 0.532, 0.72, 0.956, 0.887, 20, 30, 2.12, 0.442,
10, 50, 110, 3.36, 2.41, 20, 70, 3610, 100, 4.79, 20, 0.0315,
30, 60, 1, 3.37, 80, 1.21, 0.302, 0.728, 1.29, 30, 40, 90, 30,
0.697, 6.25, 0.576, 0.335, 20, 10, 620, 40, 9.98, 4.76, 2.61,
3.39, 20, 4.59)), .Names = c("site", "conc"), row.names = c(NA,
52L), class = "data.frame")

mystats <- function(x, ...){ # Here ...
   out <- boxplot.stats(10^x, ...)  # ...and here!!!
   out$stats <- log10(out$stats)
   out$conf <- log10(out$conf) ## Omit if you don't want notches
   out$out <- log10(out$out)
   out ## With the boxplot statistics converted to the log10 scale
}

dev.new()
bwplot(conc~site, data=test,
        pch="|",  # this is plotting a line instead of a point
        scales = list(y=list(log=10)),
        panel = function(...){
          panel.bwplot(..., stats = mystats)
        }
)

# end code

Il 17/09/2012 20:26, Rui Barradas ha scritto: