0

I'm doing a fairly straightforward xyplot lattice graph, using both points and a line connecting them for clarity. However, in some of my panels, data is sparse and I don't want to connect the points. I want to be able to specify which panels should have a line. I've tried using par.settings and panel.superpose, but I must be missing something because nothing is quite working. When I tried panel.superpose, it seems to nullify all my other graphical settings that aren't in that statement. I've found answers on how to change line types for different groups within one panel, but I want them to be different between panels and I'm stumped.

Here's the code:

theme1 <-list(strip.background=list(col="gray75"))

myplot=xyplot(Average.of.response~bin|disttype, 
   groups=X_LEVEL_, 
   data=initbehav.disttype[order(initbehav.disttype$disttype),], 
   subset=initbehav=="Foraging",
   par.settings = theme1,
   ylab=list(label="Average Probability of Response Type",cex=1.1, fontface="bold"),
   xlab=list(label="Distance to Disturbance Source (m)", cex=1.2, fontface="bold"),

   key=list(text=list(
     lab=as.character(unique(initbehav.disttype$X_LEVEL_)),
     padding.text=10),
     points=list(cex=1.5, pch=c(0,16,2,18), col="black"),  
     columns=4, 
     space="bottom"),

   type=c("l","p"),
   pch=c(0,16,2,18),
   cex=1.4,
   col="black",
   scales=list(
     x=list(rot=45, cex=0.9), 
     y=list(cex=1.1)
   )

)

EDITED with sample data:

For argument's sake, if this was my code:

group=(c("a","b","c"))

data=data.frame(
Y=runif(30,min=0, max=25),
X=rep(c("dog", "car", "person"), each=10),
Z=sample(group, 30, replace=T))

library(lattice)

xyplot(Y~X|Z, 
       data=data,
       type=c("smooth","p"),
       cex=1.4,
       col="black",
       scales=list(
         x=list(rot=45, cex=0.9), 
         y=list(cex=1.1)
       )
)

And I want to remove the smooth line from JUST the "dogs" panel, while leaving everything else intact, how would I use a custom panel function to achieve that? Can I still keep arguments that I wish to apply to every panel outside of the custom panel function?

mmd5582
  • 3
  • 4
  • It would help to post some [sample data](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) here. If we can't run your function, it's very difficult to help you fix it. Make it clear in your examples which panels you would like to connect and which you would not. You'll definitely need a custom panel function. – MrFlick Dec 18 '14 at 18:50
  • Thanks, I updated with simple sample code. – mmd5582 Dec 18 '14 at 19:41
  • In your sample you have panels for a,b, and c, not for the dogs. Did you mean `Y~Z|X` for the formula? – MrFlick Dec 18 '14 at 19:44

2 Answers2

0

Assuming I switch things around so you actually have a "dog" panel, here's how you can conditionally change the panel function

xyplot(Y~Z|X, 
    data=data,
    type=c("smooth","p"),
    cex=1.4,
    col="black",
    panel = function(..., type) {
        grpname <- dimnames(trellis.last.object())[[1]][packet.number()]
        if(grpname == "dog") {
            panel.xyplot(..., type=setdiff(type,"smooth"))
        } else {
            panel.xyplot(..., type=type)
        }
    },
    scales=list(
        x=list(rot=45, cex=0.9), 
        y=list(cex=1.1)
    ),
    layout=c(3,1) # (optional)
)

And this results in

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Works perfectly. I would not have known to look for that trellis.last.object call. Thank you! – mmd5582 Dec 18 '14 at 20:21
  • Yeah. unfortunately there's not an easier way (that i know of) to get the value of the conditioning variables inside the panel function. If the logic to include was based on number of observations, the code would be simpler as @BondedDust has already shown. But that's not what you asked for in the sample. – MrFlick Dec 18 '14 at 20:23
0

If you make the task a bit harder by giving the groups different sizes you can make the plotting conditional on group size:

 group=(c("a","b","c"))

data=data.frame(
Y=runif(30,min=0, max=25),
X=c( rep(c( "car", "person"), each=13),rep("dog", each=4)),
Z=sample(group, 30, replace=T)
)
library(lattice)
xyplot(Y~Z|X,   data=data,  cex=1.4,  col="black",
    panel = function(x, y, ...) {
           if( length(x) >5) {
            panel.xyplot(x, y, ..., type=c("p", "smooth"))
        } else {
            panel.xyplot(x, y, ..., type="p")
        }
    },
    scales=list(
        x=list(rot=45, cex=0.9), 
        y=list(cex=1.1)
             )
    )
IRTFM
  • 258,963
  • 21
  • 364
  • 487