Alex Karner
2008-Oct-08 22:41 UTC
[R] Lattice question: plotting two sets of data, defining groups for the second set
R friends, I'm running R 2.7.2 on Windows XP SP2. I have some data that's amenable to smoothing, and some that's not. I'm trying to plot smoothed lines for the former along with just points for the latter in a single panel. The problem comes when trying to break out the points by group. My sample code follows. data1 <- data.frame(Names=c(rep("Jon",9),rep("Karl",9)),Measurements=c(2,4,16,25,36,49,64,81,100,1,2,5,12,17,21,45,54,67),PlotAt=c(1:9,1:9)) data2 <- data.frame(Names=c(rep("Jonah",9),rep("Beth",9)),Measurements=c(1:5,1,1,1,1,rep(20,9)),PlotAt=c(1:9,1:9)) Assume data1 looks good when smoothed and that data2 does not. Plot the smoothed lines: xyplot( Measurements ~ PlotAt, data = data1, groups=Names, type="smooth", span=0.75, degree=1, panel = function(...) { panel.superpose(...); }, ) Try to alter the panel function to include groups for data2's points: xyplot( Measurements ~ PlotAt, data = data1, groups=Names, type="smooth", span=0.75, degree=1, panel = function(...) { panel.superpose(...); panel.xyplot(data2$PlotAt,data2$Measurements,groups=data2$Names) }, ) and I get a "subscripts" missing error. If I omit the "groups" argument all the points plot with the same plotting character. I suspect if I were smarter I'd be able to easily fix this, so I'm hoping someone here can help. Thanks, Alex [[alternative HTML version deleted]]
Deepayan Sarkar
2008-Oct-09 00:03 UTC
[R] Lattice question: plotting two sets of data, defining groups for the second set
On Wed, Oct 8, 2008 at 3:41 PM, Alex Karner <aakarner at ucdavis.edu> wrote:> R friends, > > I'm running R 2.7.2 on Windows XP SP2. > > I have some data that's amenable to smoothing, and some that's not. I'm > trying to plot smoothed lines for the former along with just points for the > latter in a single panel. The problem comes when trying to break out the > points by group. My sample code follows. > > data1 <- > data.frame(Names=c(rep("Jon",9),rep("Karl",9)),Measurements=c(2,4,16,25,36,49,64,81,100,1,2,5,12,17,21,45,54,67),PlotAt=c(1:9,1:9)) > > data2 <- > data.frame(Names=c(rep("Jonah",9),rep("Beth",9)),Measurements=c(1:5,1,1,1,1,rep(20,9)),PlotAt=c(1:9,1:9)) > > Assume data1 looks good when smoothed and that data2 does not. > > Plot the smoothed lines: > xyplot( > Measurements ~ PlotAt, > data = data1, > groups=Names, > type="smooth", > span=0.75, > degree=1, > panel = function(...) { > panel.superpose(...); > }, > ) > > Try to alter the panel function to include groups for data2's points: > > xyplot( > Measurements ~ PlotAt, > data = data1, > groups=Names, > type="smooth", > span=0.75, > degree=1, > panel = function(...) { > panel.superpose(...); > panel.xyplot(data2$PlotAt,data2$Measurements,groups=data2$Names) > }, > ) > > and I get a "subscripts" missing error. If I omit the "groups" argument all > the points plot with the same plotting character. I suspect if I were > smarter I'd be able to easily fix this, so I'm hoping someone here can help.You can fix this by supplying the value of 'subscripts' that would have been passed has this been a separate xyplot() call: xyplot( Measurements ~ PlotAt, data = data1, groups=Names, type="smooth", span=0.75, degree=1, panel = function(...) { panel.superpose(...); panel.xyplot(data2$PlotAt,data2$Measurements,groups=data2$Names, subscripts=seq_len(nrow(data2))) }) However, a cleaner solution would be to combine your data first: xyplot(Measurements ~ PlotAt, data = make.groups(data1, data2), groups=Names, panel = panel.superpose, distribute.type = TRUE, type=c("smooth", "smooth", "p", "p"), span=0.75, degree=1) -Deepayan