How can I control panel order in xyplot ()?

R determines the order of the panel in xyplot()alphabetical order (for strings) or level order (for coefficients). I have dates as text that I would like to display in chronological order, and not in alphabetical order.

x <- sample(1:10, 10, replace = TRUE)
y <- sample(1:10, 10, replace = TRUE)
# overlapping 2010 and 2011
date <- rep(as.Date("2010-01-01") + sort(sample(200:400, 5)), each = 2)
striptext <- format(date, "%d-%b ...")
df <- data.frame(x, y, date, striptext)

This works great:

xyplot(y ~ x | date, data = df)

But this puts the dates out of order:

xyplot(y ~ x | striptext, data = df)

I do not need the full date in the stripes, but I want them to be in the correct order. Is there any solution beyond what striptextis an ordered factor?

+3
source share
1 answer

Set the levels striptextexplicitly and save it as a factor so that the grid does not require coercion for you (this is where the levels end in alpha order):

set.seed(1)
x <- sample(1:10, 10, replace = TRUE)
y <- sample(1:10, 10, replace = TRUE)
# overlapping 2010 and 2011
date <- rep(as.Date("2010-01-01") + sort(sample(200:400, 5)), each = 2)
striptext <- format(date, "%d-%b ...")
## set the levels explicitly
df <- data.frame(x, y, date, 
                 striptext = factor(striptext, levels = unique(striptext)))
xyplot(y ~ x | striptext, data = df)
+4
source

Source: https://habr.com/ru/post/1789364/


All Articles