I've generated a lattice plot with 2 response variables plotted on a single Y axis). I have plotted one of the response variables as a line (Response1 below), the other (Response2) as points on that same Y axis. I wish to be able to highlight some of the points (in Response 2) according to a condition ('Condition'), for example, if Condition==1 then change the point color to blue and increase the point size and change the symbol. The fact that I wish to plot two response variables on the same axis excludes the 'Group' option for this and, consequently, the panel.superpose function (as far as i can tell). Example code is given below:
library(lattice)
#generate dataframe
TD=data.frame(Response1=rnorm(100,50,5),Response2=rnorm(100,70,5),
Xaxis=seq(1:100),Factor=rep(LETTERS[1:5],20),
Condition=sample(0:1,100,replace=T))
#generate plot
xyplot(Response1+Response2~Xaxis|Factor,data=TD,distribute.type=TRUE,
type=c('l','p'))
#if(condition==1) then Response2 symbol=blue and size is larger??
I have tried to do this via latticeExtra's doubleYScale plot (using a 'group' argument' in one of the plots to condition on 'Condition') but this is lost when the overlay is made.
Any guidance on this would be much appreciated.
Regards
Tom.
Manipulating an input dataset is not always desirable and might render your code unnecessarily confusing. Instead, you could create two separate xyplot objects - one with both response variables represented as points and lines, one with only those points of the second response variable for which Condition == 1 applies - and blend them together using as.layer from latticeExtra.
library(latticeExtra)
## grouped plot with lines and points
p <- xyplot(Response1 + Response2 ~ Xaxis | Factor, data = TD,
distribute.type = TRUE, type = c("l", "p"), as.table = TRUE)
## plot with 'Condition == 1' points only
p_cond1 <- xyplot(Response2 ~ Xaxis | Factor,
data = subset(TD, Condition == 1), col = "green")
## combine plots
p + as.layer(p_cond1)

Similarly, if you would like to modify the point symbol and size, I would recommend creating three different xyplot objects - one with the first response variable represented as lines, one with only those points of the second response variable for which Condition == 0 applies, and similarly one for Condition == 1.
## grouped plot with lines only
p <- xyplot(Response1 + Response2 ~ Xaxis | Factor, data = TD,
distribute.type = TRUE, type = c("l", "p"), as.table = TRUE,
col.symbol = "transparent")
## plot with 'Condition == 0' (or 'Condition == 1') points only
p_cond <- lapply(0:1, function(i) {
xyplot(Response2 ~ Xaxis | Factor,
data = subset(TD, Condition == i),
col = ifelse(i == 0, "orange", "darkgreen"),
pch = ifelse(i == 0, 1, 2), cex = ifelse(i == 0, 0.8, 1.2))
})
## combine plots
p +
as.layer(p_cond[[1]]) +
as.layer(p_cond[[2]])

You can split Response2 into separate variables according to Condition. Here is one way.
TD = within(TD, {
R2a=Response2*Condition
R2b = Response2*(1-Condition)
R2a[R2a==0]=NA
R2b[R2b==0]=NA
})
xyplot(Response1+R2a+R2b~Xaxis|Factor,data=TD,distribute.type=TRUE,
type=c('l','p','p'))
Then adjust size and color to suit you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With