birdy
birdy

Reputation: 9646

how to plot a graph on lattice with two different colors

I'm trying to plot the following data

> data
  epochs      rmse learner momentum
1      1 0.2992122     0.3      0.0
2      1 0.3082895     0.1      0.2
3      1 0.2955586     0.5      0.2
4      1 0.2955182     0.3      0.4
5     11 0.2916979     0.3      0.0
6     11 0.2919140     0.1      0.2
7     11 0.2928490     0.5      0.2
8     11 0.2906339     0.3      0.4

I want a graph that has epochs on x-axis, rmse on y-axist, and graphs a separate line for each row while labeling the learner and momentum.

I tried plot it like this:

 > xyplot(rmse ~ epochs, data=data, groups = data$learner,
       type = "l",
       auto.key =
           list(space = "right", points = FALSE, lines = TRUE))

but this is creating graph with only learner values, it is not taking into account the momentum as well.

enter image description here

How can I fix the graph such that the labels read:

L = 0.1, M=0.2 <somecolor>
L = 0.3, M=0.0 <somecolor>
L = 0.5, M=0.2 <somecolor>
L = 0.3, M=0.4 <somecolor>

Upvotes: 0

Views: 83

Answers (1)

Matthew Lundberg
Matthew Lundberg

Reputation: 42689

I think you want the groups to be the interaction of learner and momentum:

xyplot(rmse ~ epochs, data=data,
       groups = interaction(learner,momentum, sep=" : ", drop=TRUE),
       type = "l",
       auto.key =
           list(space = "right", points = FALSE, lines = TRUE))

enter image description here

(Note that we don't need to specify data$learner, etc, as the data frame is pulled into the environment.)

Above, interaction is creating a factor based on the inputs learner and momentum (after coercing these to factors), but we can create any factor that we desire to use for the groups. In particular, we can use paste to create a vector to use for the groups, with the labels that you want:

xyplot(rmse ~ epochs, data=data,
       groups = paste("L =", learner, "M =", momentum),
       type = "l",
       auto.key =
           list(space = "right", points = FALSE, lines = TRUE))

enter image description here

Upvotes: 1

Related Questions