如何控制 xyplot() 中的面板顺序?

发布于 2024-10-15 08:30:21 字数 613 浏览 2 评论 0原文

R 按字母顺序(对于字符串)或级别顺序(对于因子)确定 xyplot() 中的面板顺序。我有日期作为文本,我想按时间顺序而不是按字母顺序显示。

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)

这工作正常:

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

但这会使日期乱序:

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

我不想要条带中的完整日期,但我确实希望它们按正确的顺序排列。除了使 striptext 成为有序因子之外,还有其他解决方案吗?

R determines panel order in xyplot() by alphabetic order (for strings), or level order (for factors). I have dates as text which I would like to display in chronological order, rather than 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 fine:

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

But this puts the dates out of order:

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

I don't want the full date in the strips, but I do want them in correct order. Is there any solution besides making striptext an ordered factor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疧_╮線 2024-10-22 08:30:21

显式设置 striptext 的级别并将其存储为一个因子,这样lattice就不必为您进行强制转换(这就是级别最终按 alpha 顺序排列的位置):

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)

Set the levels of striptext explicitly and store it as a factor so that lattice doesn't have to do the coercion for you (which is where the levels end up 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文