R: ordering facets by value rather than alphabetical order in a ggplot2 plot -
a couple of weeks ago used ggplot2 create faceted plot facets ordered last value in data frame. had no major problems until came reordering, haven't assimilated complications of orders, factors , levels. still, after hour or 2 (or three) of referring posts got working.
when came script today no longer "working" in sorting facets alphabetical order rather final value of data frame. (i think "fixed" problem while messing around @ r console , did not add solution script.) rather spending couple of hours on tonight i'm going throw myself on mercy of so.
q. how can sort facets specified value rather alphabetical order of names of each facet? please note following code example only; real data has several dozen items.
edited code below reflect additional input @joran; facets sorted , filled appropriately. mission successful.
# version 3 require(ggplot2) ## nb script assumes 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)],] # values on 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 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)
you have 2 problems:
the line converts
myitems$variable
factor should specifyordered = true
, assure ordered factor.your
geom_text
call uses separate data frame corresponding variable isn't factor (or ordered) it's stomping on ordered nature of 1 inmyitems
.
convert them both or ordered factors, , should fine.
Comments
Post a Comment