[R] Grouping multiple runs of multiple datasets in lattice's xyplot (with fixed example code)

Daniel Kornhauser dkor at northwestern.edu
Tue Apr 28 00:21:47 CEST 2009


Hi:

( A bug in my example code was pointed in the post I sent out earlier
today, so I am reposting with the fixed example code)

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.var = model.conditional.var, 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




More information about the R-help mailing list