Rainer Hurling
2008-Aug-08 21:38 UTC
[R] Lattice: regression lines within grouped xyplot panels
Dear community, I am looking for a possibility to draw 'regression lines' instead of 'smooth' lines in grouped xyplots. The following code should give you a small example of the data structure. library(lattice) data(Gcsemv, package = "mlmRev") # Creates artificial grouping variable ... Gcsemv$Groups <- ifelse(as.numeric(as.character(Gcsemv$school))>65000, "Group1", "Group2") xyplot(written ~ course | gender, data = Gcsemv, type = c("g", "p", "smooth"), groups = Groups, panel = function(x, y, ...) { panel.xyplot(x, y, ...) # Here I want to draw the regression lines # panel.abline(x, y) }, auto.key = list(space = 'right')) I tried some variations like 'lm(y~x)' inside panel.abline() but had no success. Also googling around and reading in Deepayan Sakars interesting new book gave me no hints. Is there any chance of displaying regression lines (instead of smooth function) within grouped panels? Or am I entirely wrong with this? Thanks in advance, Rainer Hurling > sessionInfo() R version 2.7.1 Patched (2008-08-01 r46207) i386-unknown-freebsd8.0 locale: de_DE.ISO8859-15/de_DE.ISO8859-15/C/C/de_DE.ISO8859-15/de_DE.ISO8859-15 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] cclust_0.6-14 lattice_0.17-13 chron_2.3-24 loaded via a namespace (and not attached): [1] grid_2.7.1 tools_2.7.1
Deepayan Sarkar
2008-Aug-08 23:05 UTC
[R] Lattice: regression lines within grouped xyplot panels
On Fri, Aug 8, 2008 at 2:38 PM, Rainer Hurling <rhurlin at gwdg.de> wrote:> Dear community, > > I am looking for a possibility to draw 'regression lines' instead of > 'smooth' lines in grouped xyplots. The following code should give you a > small example of the data structure. > > > library(lattice) > data(Gcsemv, package = "mlmRev") > > # Creates artificial grouping variable ... > Gcsemv$Groups <- > ifelse(as.numeric(as.character(Gcsemv$school))>65000, > "Group1", "Group2") > > xyplot(written ~ course | gender, data = Gcsemv, > type = c("g", "p", "smooth"), > groups = Groups, > panel = function(x, y, ...) { > panel.xyplot(x, y, ...) > # Here I want to draw the regression lines > # panel.abline(x, y) > }, > auto.key = list(space = 'right'))Does this do what you want?: xyplot(written ~ course | gender, data = Gcsemv, type = c("g", "p", "r"), groups = Groups) The problem with your approach is that the panel function you define doesn't deal with groups. An easy workaround is to use panel.superpose: xyplot(written ~ course | gender, data = Gcsemv, type = c("g", "p"), groups = Groups, panel = panel.superpose, panel.groups = function(x, y, ...) { panel.xyplot(x, y, ...) panel.lmline(x, y, ...) }, auto.key = list(space = 'right')) -Deepayan
Possibly Parallel Threads
- lubridate does not install on FreeBSD any more
- Problem with creating default NAMESPACE on FreeBSD
- [LLVMdev] Failure to compile llvm-gcc-4.2-2.7 on FreeBSD on sparc machine
- No graphics capability on fresh install of R2.11.0 on FreeBSD 8.0
- unable to ./configure wine on FreeBSD 8