Szumiloski, John
2011-Apr-11 15:57 UTC
[R] Override col.lines and col.symbol in panel.xyplot with type='b'
Dear useRs, I have a longitudinal experiment with several treatment groups, ~20 subjects per group, ~6 timepoints and a continuous dependent variable. I have been successfully been using lattice::xyplot with this data. However, I have been stumped with a particular application of it. I would like to use xyplot on my data, broken into treatment groups with the groups argument, using type='b' to show subjectwise longitudinal data. So far so good, I have done this many times. But now I wish to show the same data but having the color of the lines and symbols overridden in some arbitrary way, yet not without changing anything else about the plot, in particular the structure/topology of the plot from using the groups argument and type='b'. This requires using a panel function of some sort. I have come to think I will need to use a function with a 'subscripts' argument as the panel function, which then itself calls panel.xyplot() and uses its arguments col.line and col.symbol. The closest example I could find is on page 73 of Sarkar's UseR! Lattice book, where the subscripts argument indexed the data within each grouped subplot, and it was used as an index for a user-generated vector of colors. This seems like what I want to do. But I could not get this paradigm to work. Here is a simple example using trivial data:> unlist(R.Version())platform arch "i386-pc-mingw32" "i386" os system "mingw32" "i386, mingw32" status major "Patched" "2" minor year "12.2" "2011" month day "03" "18" svn rev language "55383" "R" version.string "R version 2.12.2 Patched (2011-03-18 r55383)"> search()[1] ".GlobalEnv" "package:stats" "package:graphics" [4] "package:grDevices" "package:utils" "package:datasets" [7] "package:methods" "Autoloads" "package:base"> require(lattice)Loading required package: lattice> set.seed(388659262) > dat <- data.frame(Panel=rep(c('A','B'), each=4),+ ID=factor(rep(letters[1:4], each=2)), + X=rep(c(0,1), times=4), + Y=runif(8) + ) ##### now for the arbitrary colors. Let's highlight one subject red, the rest black> dat$Color <- with(dat, ifelse(Panel=='A' & ID == 'a', 2, 1)) > datPanel ID X Y Color 1 A a 0 0.1138821 2 2 A a 1 0.7361403 2 3 A b 0 0.3304683 1 4 A b 1 0.5866701 1 5 B c 0 0.8819857 1 6 B c 1 0.7329025 1 7 B d 0 0.5000357 1 8 B d 1 0.6365438 1 ##### The following standard plot is fine. ##### Each subject is colored differently, ##### I believe recycling through the colors from either ##### trellis.par.get("superpose.symbol")$col or ##### trellis.par.get("superpose.line")$col, ##### but be default they are the same anyway> xyplot(Y ~ X | Panel, data=dat, groups=ID, type='b',+ scales=list(x=list(at=c(0,1),labels=c(0,1)))) ##### But for example, this following attempt to grab the ##### corresponding values of dat$Color do not have ##### my intended effect. There are now three "subjects" ##### plotted per group, each groups' line colors are the same, ##### and the symbol colors are nearly the same as the line colors....but not exactly!> xyplot(Y ~ X | Panel, data=dat, groups=ID, type='b',+ scales=list(x=list(at=c(0,1),labels=c(0,1))), + panel=function(..., groups, subscripts) panel.xyplot(..., col.symbol=dat$Color[subscripts], + col.line=dat$Color[subscripts]) + ) At one point in my efforts I was actually able to get the symbol colors correct, but the line colors were (to me) incomprehensibly wrong. But alas I have not be able to reproduce that to show here. \begin{ignorant speculation alert} I suspect that having (in the example) 8 points but only 4 lines causes undesired recycling somewhere. \end{speculation} Any assistance as to how to properly use the panel functions (or any other approach short of abandoning lattice graphics) to override the coloring would be greatly appreciated. John John Szumiloski, Ph.D. Senior Biometrician Biometrics Research WP53B-120 Merck Research Laboratories P.O. Box 0004 West Point, PA 19486-0004 USA (215) 652-7346 (PH) (215) 993-1835 (FAX) john<dot>szumiloski<at>merck<dot>com ___________________________________________________ These opinions are my own and do not necessarily reflect that of Merck & Co., Inc. Notice: This e-mail message, together with any attachme...{{dropped:14}}
Deepayan Sarkar
2011-Apr-12 07:30 UTC
[R] Override col.lines and col.symbol in panel.xyplot with type='b'
On Mon, Apr 11, 2011 at 9:27 PM, Szumiloski, John <john_szumiloski at merck.com> wrote:> Dear useRs, > > I have a longitudinal experiment with several treatment groups, ~20 subjects per group, ~6 timepoints and a continuous dependent variable. ?I have been successfully been using lattice::xyplot with this data. ?However, I have been stumped with a particular application of it. > > I would like to use xyplot on my data, broken into treatment groups with the groups argument, using type='b' to show subjectwise longitudinal data. ?So far so good, I have done this many times. ?But now I wish to show the same data but having the color of the lines and symbols overridden in some arbitrary way, yet not without changing anything else about the plot, in particular the structure/topology of the plot from using the groups argument and type='b'. > > This requires using a panel function of some sort.No, it does not. You are making things unnecessarily complicated. When you use 'groups', colors are associated with levels of the grouping variable, not individual rows of your data. So: set.seed(388659262) dat <- data.frame(Panel=rep(c('A','B'), each=4), ID=factor(rep(letters[1:4], each=2)), X=rep(c(0,1), times=4), Y=runif(8)) ##### now for the arbitrary colors. Let's highlight one subject red, the rest black myColor <- ifelse(levels(dat$ID) == "a", 2, 1) xyplot(Y ~ X | Panel, data=dat, groups=ID, type='b', col = myColor, scales=list(x=list(at=c(0,1),labels=c(0,1)))) -Deepayan
Reasonably Related Threads
- mgcv:gamm: predict to reflect random s() effects?
- tidyverse repeating error: "object 'rlang_mut_env_parent' not found"
- Etiquetas en gráfico de dispersión
- tidyverse repeating error: "object 'rlang_mut_env_parent' not found"
- tidyverse repeating error: "object 'rlang_mut_env_parent' not found"