Plot xyplot lines correctly after re-ordering factors

data-visualization, lattice, plot, r

Solution

The lines get drawn in the order they appear in the data, I think. So if you want to get a correct ordering you could do:

xyplot(acceptability ~ character | motion, data=fin[order(fin$character),], col=1, 
aspect="xy", layout=c(6,1), type="o", scales = list(x = list(rot = 90)))

Notice only the `data` argument changed.

Problem

When I make `xyplot` with line connections before reordering factors, it gives me a nice, sequential connections between data points: ``` library(lattice) fin <- read.csv("http://dl.dropbox.com/u/2505196/unc_vall.csv", header=T) xyplot(acceptability ~ character | motion, data=fin, col=1, aspect="xy", layout=c(6,1), type="o", scales = list(x = list(rot = 90))) ``` Then I reorder factors, and everything gets screwed: ``` fin$character <- factor(fin$character, levels = c("battle","klank","manny", "skelly","zombie","loman","himan")) ``` Reordering factors' levels works fine, the values go where they should, but somehow the order of line connections stays the same. It doesn't occur to me how I could change my reordering to make it work. EDIT: I should add that solution should be universal enough to work for both `xyplot` and `xYplot` from the package `Hmisc`.

Original source