Daniel Kornhauser
2009-Apr-27 19:33 UTC
[R] Grouping multiple runs of multiple datasets in lattice's xyplot
Hi:
I don't know if my explanation below is clear, so afterwards, I wrote a
small a self contained annotated example that generates two plots.
I execute simulations with different parameters settings that create several
datasets, and for each parameter setting (or dataset) I can obtain different
results by choosing a different random seed.
I am stuck grouping data from the datasets and runs in an xyplot of lattice.
First, I would like to group my data *by datasets* but only using color.
Then, I would like to "subgroup" the data points into to draw
individual
line *by runs*.
Does anybody have any suggestions on how to do this ?
Thanks
Daniel.
### START EXAMPLE
library("lattice")
model.run = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 11, 11,
11, 11, 11, 11, 12, 12, 12, 12, 12, 12)
model.dataset = c(2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2,
2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2, 2,
2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1,
1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
model.conditional.var = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33,
33, 33, 33, 33, 33, 33, 33, 33, 66, 66, 66, 66, 33, 33, 33, 33, 33, 33, 66,
66, 66, 66, 66, 66, 66, 66, 66, 66, 99, 99,99, 99, 66, 66, 66, 66, 66, 66,
99, 99, 99, 99, 66, 66, 66, 66, 66, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99)
model.x = c(0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 0,
1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 0, 1, 2, 3, 4, 5,
0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1,2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1,
2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4,
5, 0, 1, 2, 3, 4, 5)
model.y = c(50, 50, 50, 51, 50, 55, 50, 59, 49, 46, 50, 50, 49, 53, 51, 55,
53, 54, 53, 52, 50, 50, 52, 52, 52, 46, 83, 82, 83, 87, 83, 83, 85, 89, 85,
93, 89, 97, 92, 92, 83, 85, 85, 88, 90, 90, 116, 117, 115, 117, 83, 86, 88,
91, 91, 96, 116, 116, 118, 123, 116, 118, 121,122, 120, 121, 149, 151, 154,
158, 116, 118, 120, 123, 128, 131, 149, 148, 150, 153, 116, 116, 118, 118,
122, 126, 149, 147, 145, 149, 157, 163, 149, 156, 165, 175, 180, 180, 149,
150, 152, 157, 165, 169)
dat = data.frame (run = model.run, dataset = model.dataset, conditional.x
= model.conditional.x, x = model.x, y = model.y)
a = xyplot(y ~ x | conditional.var,
main = "Correctly grouped colors",
sub = "I would like to group multipe data sets with the colors
shown here, but I don't want the data points to be grouped in a single
lines",
groups = dataset, # Grouping by Data Set
ylab = NULL,
data = dat,
type = "l",
auto.key = list(space = "right"))
print(a)
dev.new()
b = xyplot(y ~ x | conditional.var,
main = "Correctly grouped lines",
sub = "I would like group two data sets with the lines shown here,
but not with an individual color for each line",
data = dat,
groups = run, # Grouping by Run
ylab = NULL,
type = "l",
auto.key = list(space = "right"))
print(b)
### END EXAMPLE
[[alternative HTML version deleted]]
Deepayan Sarkar
2009-Apr-28 22:14 UTC
[R] Grouping multiple runs of multiple datasets in lattice's xyplot
On Mon, Apr 27, 2009 at 12:33 PM, Daniel Kornhauser <dkor at northwestern.edu> wrote:> Hi: > > I don't know if my explanation below is clear, so afterwards, I wrote a > small a self contained annotated example that generates two plots. > I execute simulations with different parameters settings that create several > datasets, and for each parameter setting (or dataset) I can obtain different > results by choosing a different random seed. > > I am stuck grouping data from the datasets and runs in an xyplot of lattice. > First, I would like to group my data *by datasets* but only using color. > Then, I would like to "subgroup" the data points into to draw ?individual > line *by runs*. > Does anybody have any suggestions on how to do this ? > > ? ? ? ? ? ? ? Thanks > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Daniel. > > ### START EXAMPLE > library("lattice") > model.run = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, > 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, > 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, > 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 11, 11, > 11, 11, 11, 11, 12, 12, 12, 12, 12, 12) > > model.dataset = c(2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, > 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2, 2, > 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, > 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, > 2, 2, 2, 2, 2, 2, 2, 2, 2, 2) > > model.conditional.var = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, > 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, > 33, 33, 33, 33, 33, 33, 33, 33, 66, 66, 66, 66, 33, 33, 33, 33, 33, 33, 66, > 66, 66, 66, 66, 66, 66, 66, 66, 66, 99, 99,99, 99, 66, 66, 66, 66, 66, 66, > 99, 99, 99, 99, 66, 66, 66, 66, 66, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, > 99, 99, 99, 99, 99, 99, 99, 99, 99) > > model.x = c(0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 0, > 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 0, 1, 2, 3, 4, 5, > 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1,2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, > 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, > 5, 0, 1, 2, 3, 4, 5) > > model.y = c(50, 50, 50, 51, 50, 55, 50, 59, 49, 46, 50, 50, 49, 53, 51, 55, > 53, 54, 53, 52, 50, 50, 52, 52, 52, 46, 83, 82, 83, 87, 83, 83, 85, 89, 85, > 93, 89, 97, 92, 92, 83, 85, 85, 88, 90, 90, 116, 117, 115, 117, 83, 86, 88, > 91, 91, 96, 116, 116, 118, 123, 116, 118, 121,122, 120, 121, 149, 151, 154, > 158, 116, 118, 120, 123, 128, 131, 149, 148, 150, 153, 116, 116, 118, 118, > 122, 126, 149, 147, 145, 149, 157, 163, 149, 156, 165, 175, 180, 180, 149, > 150, 152, 157, 165, 169) > > dat = data.frame (run = ?model.run, ?dataset = model.dataset, conditional.x > = model.conditional.x, x = model.x, y = model.y)myColors <- c("red", "blue") xyplot(y ~ x | factor(conditional.var), data = dat, groups = interaction(dataset, run), col = myColors, ylab = NULL, type = "l", key = list(text = list(c("1", "2")), lines = list(col = myColors), space = "right")) The trick is to specify just two colors, and create the interaction() in the right order so that the colors get recycled properly. -Deepayan> > a = xyplot(y ~ x | conditional.var, > ? ? ? ?main = "Correctly grouped colors", > ? ? ? ?sub = ?"I would like to group multipe data sets with the colors > shown here, but I don't want the data points to be grouped in a single > lines", > ? ? ? ?groups = dataset, # Grouping by Data Set > ? ? ? ?ylab = ?NULL, > ? ? ? ?data = dat, > ? ? ? ?type = "l", > ? ? ? ?auto.key = list(space = "right")) > print(a) > > dev.new() > b = xyplot(y ~ x | conditional.var, > ? ? ? ?main = "Correctly grouped lines", > ? ? ? ?sub = "I would like group two data sets with the lines shown here, > but not with an individual color for each line", > ? ? ? ?data = dat, > ? ? ? ?groups = run, ? ?# Grouping by Run > ? ? ? ?ylab = ?NULL, > ? ? ? ?type = "l", > ? ? ? ?auto.key = list(space = "right")) > print(b) > ### END EXAMPLE > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >