Skip to content

two questions re: the use of lattice

13 messages · Ranjan Maitra, jim holtman, David L Carlson +1 more

#
Dear friends,

I have two questions regarding the use of lattice. First some code:


## begin code

z <- cbind(rep(c("BIC", "ICL", "s_v", "Q_v", "sig-q",
                      "s_lsk", "s_lML", "s_mlsk", "s_mlML", "s_la8",
"s_haar"), each = 250), rep(c(5, 10, 20, 30, 50), each = 50))
z <- rbind(cbind(z, 0), cbind(z, 20), cbind(z, 40))
z <- cbind(z, rnorm(n = nrow(z)))
z <- as.data.frame(z)
names(z) <- c("Method", "sigma", "INU", "Error")
sigma <- as.numeric(levels(z$sigma))
sigmaExprList <- lapply(sigma, function(s) bquote(italic(sigma) == .
(s))) sigmaExpr <- as.expression(sigmaExprList)
bwplot(Error~Method | sigma, data = z[z[,"INU"] == 0,],scales=list
(rot=90), horiz = F, xlab = "Method", ylab = "Relative Error",
       strip = function(which.given, which.panel, var.name,
         strip.levels = FALSE,
         strip.names = TRUE, ...) {
         strip.default(which.given, which.panel,
                       var.name = sigmaExpr[which.panel],
                       strip.levels = FALSE,
                       strip.names = TRUE, ...)
        },
        layout = c(5,1),  col = "red")


## end code

Question 1: how do I "force" the display of the "Method" in the
plotting to be in the same order (i.e.,  in the order of "BIC", "ICL",
"s_v", "Q_v", "sig-q", "s_lsk", "s_lML", "s_mlsk", "s_mlML", "s_la8",
"s_haar") as the input. As you may notice, it puts them in its own
merry order (I suspect in ascii alphabetical order, but that conjecture
is based entirely on my very few sample attempts). 

Question 2: I want to have 3x5 plots of the respective boxplots.
Something like:
Error ~ Method | sigma + INU?
But I want the labels for the sigma and the INU to be only in the
column and the rows (vertically here) as appropriate, in order to save
plotting space. How do I go about doing this?

Please reply through the mailing list so that others may also benefit.
In any case, many thanks again for reading and for any help and
pointers! 

Best wishes,
Ranjan
#
Answer to you first question, try this at the start of bwplot to
specify ordering:

bwplot(Error~factor(Method, levels = unique(Method))

On Sat, Jul 21, 2012 at 2:42 PM, Ranjan Maitra
<maitra.mbox.ignored at inbox.com> wrote:

  
    
#
Run this before the bwplot() command:

z$Method <- factor(z$Method, levels = c("BIC", "ICL", "s_v", "Q_v", 
  "sig-q", "s_lsk", "s_lML", "s_mlsk", "s_mlML", "s_la8", "s_haar"))

I don't have an answer for the 2nd question. Seems like it must be
possible.

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
#
Take a look at useOuterStrips() in package latticeExtra.

-------
David
#
Dear friends,

Many thanks to Jim (Holtman) and David (Carlson) for their quick
responses: Q1 is now solved. There are two almost equivalent ways for
doing this. They follow:
library(lattice)

z <- rbind(cbind(z, 0), cbind(z, 20), cbind(z, 40))
z <- cbind(z, rnorm(n = nrow(z)))
z <- as.data.frame(z)
names(z) <- c("Method", "sigma", "INU", "Error")
sigma <- as.numeric(levels(z$sigma))
sigmaExprList <- lapply(sigma, function(s) bquote(italic(sigma) == .
(s))) sigmaExpr <- as.expression(sigmaExprList)

z$Method <- factor(z$Method, levels = c("BIC", "ICL", "s_v", "Q_v", 
  "sig-q", "s_lsk", "s_lML", "s_mlsk", "s_mlML", "s_la8", "s_haar"))

bwplot(Error~Method | sigma, data = z[z[,"INU"] == 0,
                               ],scales=list(rot=90),
       horiz = F, xlab = "Method", ylab = "Relative Error",
       strip = function(which.given, which.panel, var.name,
         strip.levels = FALSE,
         strip.names = TRUE, ...) {
         strip.default(which.given, which.panel,
                       var.name = sigmaExpr[which.panel],
                       strip.levels = FALSE,
                       strip.names = TRUE, ...)
        },
        layout = c(1, 5),  col = "red")

# On the other hand, if we do not want to change z$Method in perpetuity, we could do the following: 


z <- cbind(rep(c("BIC", "ICL", "s_v", "Q_v", "sig-q",
                      "s_lsk", "s_lML", "s_mlsk", "s_mlML", "s_la8",
"s_haar"), each = 250), rep(c(5, 10, 20, 30, 50), each = 50))
z <- rbind(cbind(z, 0), cbind(z, 20), cbind(z, 40))
z <- cbind(z, rnorm(n = nrow(z)))
z <- as.data.frame(z)
names(z) <- c("Method", "sigma", "INU", "Error")
sigma <- as.numeric(levels(z$sigma))
sigmaExprList <- lapply(sigma, function(s) bquote(italic(sigma) == .
(s))) sigmaExpr <- as.expression(sigmaExprList)

bwplot(Error~factor(Method, levels = unique(Method)) | sigma, data = z[z
[,"INU"] == 0, ],scales=list(rot=90),
       horiz = F, xlab = "Method", ylab = "Relative Error",
       strip = function(which.given, which.panel, var.name,
         strip.levels = FALSE,
         strip.names = TRUE, ...) {
         strip.default(which.given, which.panel,
                       var.name = sigmaExpr[which.panel],
                       strip.levels = FALSE,
                       strip.names = TRUE, ...)
        },
        layout = c(1, 5),  col = "red")



# However, I am unable to solve Q2. Actually, even more basic is the fact that I can not get Box-Whisker plots without anything else. David's suggestion of using useOuterStrips appears reasonable, but as I said, I can not get anything meaningful even before then. 

# Try:

bwplot(Error~Methods | sigma + INU, data = z,scales=list(rot=90))

Any suggestions?

Many thanks again!

Best wishes,
Ranjan

____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails
#
On 2012-07-22 09:02, Ranjan Maitra wrote:
[I had to dig back to see what your Q2 was. It's good to keep context.]

Try this:

  p <- bwplot(Error~Method | sigma + INU, data = z,
         scales = list(rot=90), horiz = FALSE,
         layout = c(5,3), col = "red")

  require(latticeExtra)
  useOuterStrips(p,
       strip = <<your sigma-strip function>>,
       strip.left = strip.custom(
           var.name = "INU", sep = " = ",
           strip.names = TRUE)
  )

Peter Ehlers
#
On Sun, 22 Jul 2012 15:04:36 -0700 Peter Ehlers <ehlers at ucalgary.ca>
wrote:
This works!! Thanks very much!! May I also ask how I can put in a "%"
after the INU value? i.e., I want the labels to be INU = 0%, INU = 20%,
INU = 40% (instead of INU = 0, INU = 20 and INU = 40).

Many thanks again!
Ranjan

____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
#
On 2012-07-22 15:58, Ranjan Maitra wrote:
Define:

   INUExpr <- paste0("INU = ", c(0,20,40), "%")

Then

     useOuterStrips(p,
        strip = <<your sigma-strip function>>,
        strip.left = << your strip function with
               var.name = INUExpr[which.panel] >>
   )

Peter Ehlers
#
Thanks, Peter! I have been trying things like this all afternoon. But i
still get the same problem:

The following, from your earlier e-mail works (cut-and-paste should be
fine):


library(lattice)
library(latticeExtra)
z <- cbind(rep(c("BIC", "ICL", "s_v", "Q_v", "sig-q",
                      "s_lsk", "s_lML", "s_mlsk", "s_mlML", "s_la8",
"s_haar"), each = 250), rep(c(5, 10, 20, 30, 50), each = 50))
z <- rbind(cbind(z, 0), cbind(z, 20), cbind(z, 40))
z <- cbind(z, rnorm(n = nrow(z)))
z <- as.data.frame(z)
names(z) <- c("Method", "sigma", "INU", "Error")
sigma <- as.numeric(levels(z$sigma))
sigmaExprList <- lapply(sigma, function(s) bquote(italic(sigma) == .
(s))) sigmaExpr <- as.expression(sigmaExprList)



p <- bwplot(Error~factor(Method, levels = unique(Method)) | sigma +
INU, data = z, scales = list(rot=90), horiz = FALSE,
         layout = c(3,5), col = "red")

  require(latticeExtra)

  useOuterStrips(p,
       strip =  function(which.given, which.panel, var.name,
         strip.levels = FALSE,
         strip.names = TRUE, ...) {
         strip.default(which.given, which.panel,
                       var.name = sigmaExpr[which.panel],
                       strip.levels = FALSE,
                       strip.names = TRUE, ...)
        },
                 strip.custom(
                              var.name = "INU", sep = " = ",
                              strip.names = TRUE))



However, this (edition of the latter) does not:

INUExpr <- paste0("INU = ", c(0,20,40), "%")

  useOuterStrips(p,
       strip =  function(which.given, which.panel, var.name,
         strip.levels = FALSE,
         strip.names = TRUE, ...) {
         strip.default(which.given, which.panel,
                       var.name = sigmaExpr[which.panel],
                       strip.levels = FALSE,
                       strip.names = TRUE, ...)
        },
       strip.left = function(which.given, which.panel, var.name,
         strip.levels = FALSE,
         strip.names = TRUE, ...) {
                 strip.custom(
           var.name = INUExpr[which.panel],
           strip.names = TRUE, ...)
               }
  )


Clearly, I am doing something wrong in defining the strip function. (It
does not help that i am not completely at home with it.)  Can you or
someone please help some more?

Many thanks again!
Best wishes,
Ranjan

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
#
On 2012-07-22 18:03, Ranjan Maitra wrote:
Actually, it would work if you made strip.left a more accurate
copy of your strip function. But I hadn't taken a close enough
look at your strip function. I don't think it's needed at all.
Try this:

  p <- bwplot(Error ~ Method | sigma + INU, data = z,
        scales = list(rot=90), horiz = FALSE,
        layout = c(5,3), col = "red")

  useOuterStrips(p,
     strip = strip.custom(
          factor.levels = sigmaExpr),
     strip.left = strip.custom(
          factor.levels = INUExpr)
  )

BTW, you don't need the italic() on sigma - it does nothing in plotmath.

Peter Ehlers
#
On Sun, 22 Jul 2012 18:58:39 -0700 Peter Ehlers <ehlers at ucalgary.ca>
wrote:
Yes, thanks for this and also the corrections. One last question (I
guess): 

my sigmas are no longer in the order of 5, 10, 20, 30, 50. How do I fix
that?

Thanks again!!

Ranjan

____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
#
On 2012-07-22 19:09, Ranjan Maitra wrote:
Just reset the levels of z$sigma (and also redefine sigmaExpr):

   z$sigma <- factor(z$sigma,
                   levels = c(5,10,20,30,50))  # new levels order

   sigmaExprList <- lapply(as.numeric(levels(z$sigma)),
                           function(s) bquote(sigma == .(s)))
   sigmaExpr <- as.expression(sigmaExprList)
   INUExpr <- paste0("INU = ", c(0,20,40), "%")

   p <- bwplot(Error ~ Method | sigma + INU, data = z,
          scales = list(rot=90), horiz = FALSE,
          layout = c(5,3), col = "red")

   useOuterStrips(p,
      strip = strip.custom(
           factor.levels = sigmaExpr),
      strip.left = strip.custom(
           factor.levels = INUExpr)
   )

Peter Ehlers
#
One last question: how do I draw a line h = 0, lty =2 through each plot?

Thanks a lot, this has been quite a learning experience for me wrt
lattice!

Ranjan

____________________________________________________________
GET FREE 5GB EMAIL - Check out spam free email with many cool features!
Visit http://www.inbox.com/email to find out more!