几周前,我使用ggplot2创建了一个多面图,其中构面按数据框中的最后一个值排序.在重新排序之前我没有遇到任何重大问题,因为我没有真正理解订单,因素和水平的所有复杂情况.仍然,在提到SO帖子一两个小时(或三个)之后我就开始工作了.
当我今天回到剧本时,它不再"正在工作",因为它现在按字母顺序而不是数据框的最终值对方面进行排序.(我认为我最初"固定"了这个问题,同时在R控制台上乱搞并且实际上并没有将解决方案添加到脚本中.)而不是在今晚花费另外几个小时,我会抛弃自己的怜悯SO.
问:如何按指定值而不是按每个方面名称的字母顺序对构面进行排序?请注意以下代码仅为示例; 真实的数据有几十个项目.
编辑下面的代码以反映@joran的其他输入; 现在可以对构面进行分类和填充.任务成功.
# Version 3
require(ggplot2) ## NB This script assumes you have ggplot2 v0.90
require(scales)
require(plyr)
require(lubridate)
require(reshape)
set.seed(12345)
monthsback <- 15
date <- as.Date(paste(year(now()),month(now()),"1",sep="-")) - months(monthsback)
myitems <- data.frame(mydate=seq(as.Date(date), by="month", length.out=monthsback),
aaa = runif(monthsback, min = 600, max = 800),
bbb = runif(monthsback, min = 100, max = 200),
ccc = runif(monthsback, min = 1400, max = 2000),
ddd = runif(monthsback, min = 50, max = 120))
myitems <- melt(myitems, id = c('mydate'))
change_from_start <- function(x) {
(x - x[1]) / x[1]
}
myitems <- ddply(myitems, .(variable), transform, value = change_from_start(value))
myitems$mydate <- as.Date(myitems$mydate, format = "%Y-%m-%d")
myvals <- myitems[myitems$mydate == myitems$mydate[nrow(myitems)],] # get values on which to sort facets
myvals <- within(myvals, variable <- factor(variable, as.character(myvals[order(myvals$value, decreasing = T),]$variable),ordered = TRUE))
myitems <- within(myitems, variable <- factor(variable, as.character(myvals[order(myvals$value, decreasing = T),]$variable),ordered = TRUE))
print(levels(myitems$variable)) # check to see if ordering succeeded
myitems$fill <- ifelse(myitems$variable == "ddd", "blue", "darkgreen")
p <- ggplot(myitems, aes(y = value, x = mydate, group = variable)) +
geom_rect(aes(xmin = as.Date(myitems$mydate[1]), xmax = Inf, fill = fill), ymin = -Inf, ymax = Inf) +
scale_fill_manual(values = c("blue", "darkgreen")) +
geom_line(colour = "black") +
geom_text(data = myvals, aes(x = as.Date(myitems$mydate[1]) + 250, y = 0.2, label = sprintf("%1.1f%%", value * 100))) +
facet_wrap( ~ variable, ncol = 2) +
geom_hline(yintercept = 0, size = 0.6, linetype = "dotdash") +
scale_y_continuous(label = percent_format()) +
scale_x_date(expand = c(0,0), labels = date_format("%Y-%m"), breaks = date_breaks("year")) +
xlab(NULL) +
ylab(NULL) +
opts(legend.position = "none") +
opts(panel.grid.minor = theme_blank()) +
opts()
print(p)
Run Code Online (Sandbox Code Playgroud)

你有两个问题:
转换myitems$variable为因子的行应指定ordered = TRUE,以确保它将是一个有序因子。
您的geom_text调用使用一个单独的数据框,其对应的变量不是一个因素(或有序),因此它会破坏 中的有序性质myitems。
将它们两者都转换或排序因子,应该没问题。
| 归档时间: |
|
| 查看次数: |
4525 次 |
| 最近记录: |