如何控制 xyplot() 中的面板顺序?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显式设置
striptext
的级别并将其存储为一个因子,这样lattice就不必为您进行强制转换(这就是级别最终按 alpha 顺序排列的位置):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):