Add legend inside each panel with Lattice in R -
i wanna make scatter plot connecting lines different groups , different individuals. make panels conditioned group variable , groups conditioned individual variables. now, add legend inside each panels(see code below). in plots, have legends of individuals grp==1 in first panel, grp==2 in second panel, on forth. legends located in upper left corner of panel belong to. how shall code?
library(lattice) mydata <- data.frame(id = rep(1: 20, each = 10), grp = rep(1: 4, each = 50), x = rep(0: 9, 20)) mydata$y <- 1.2 * mydata$grp * mydata$x + rnorm(nrow(mydata), sd = mydata$grp) xyplot(y~ x | factor(grp), data = mydata, groups = id, type = "b", as.table = t, layout = c(2, 2), panel = panel.superpose, panel.groups = function (x, y, ...) { panel.xyplot(x, y, ...) } )
try this. note subset command comes in data statement in xyplot. on purpose. if call subset xyplot argument, plots have shown 20 labels in each plot.
library(lattice) mydata <- data.frame(id = rep(1:20, each = 10), grp = rep(1:4, each = 50), x = rep(0:9, 20)) mydata$y <- 1.2 * mydata$grp * mydata$x + rnorm(nrow(mydata), sd = mydata$grp) i=1; j=1 for(grp in 1:4) { <- xyplot(y~x|factor(grp), data=subset(mydata, grp==grp), groups = factor(id), type = "b", auto.key=list(columns=4,space="inside") ) print(a, split=c(i,j,2,2), more=t) i=i+1; if(i>2){i=1;j=j+1} # basically, tell plots quadrant go in }
Comments
Post a Comment