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) 

image showing facets sorted fill no longer working

you have 2 problems:

  1. the line converts myitems$variable factor should specify ordered = true, assure ordered factor.

  2. your geom_text call uses separate data frame corresponding variable isn't factor (or ordered) it's stomping on ordered nature of 1 in myitems.

convert them both or ordered factors, , should fine.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -