1

I have the following graph and I would like "InProcess" to be below "Investigating". I thought reordering the state factor would do it, but all it does is affect the order the legend is printed. How would you accomplish this?

enter image description here

Here is the code and dataset that creates this graph.

Data:

Date    Identified  InProcess   Fixed   NotFixed    Duplicate
11/12/2015  22  43  10  5   8
11/19/2015  11  21  11  9   27
11/26/2015  24  10  10  4   13
12/3/2015   39  11  4   2   17
12/10/2015  36  11  11  8   8
12/17/2015  32  9   9   4   7
12/24/2015  20  6   4   12  13
12/31/2015  19  4   5   3   2
1/7/2016    21  3   5   4   2

.

plotgraph <- function() {
  require(ggplot2)
  require(reshape2)
  require(data.table)

  testdata <- read.table(header = TRUE, stringsAsFactors = FALSE, text = "Date    Identified  InProcess   Fixed   Inactionable    Duplicate
                       11/12/2015  22  43  10  5   8
                       11/19/2015  11  21  11  9   27
                       11/26/2015  24  10  10  4   13
                       12/3/2015   39  11  4   2   17
                       12/10/2015  36  11  11  8   8
                       12/17/2015  32  9   9   4   7
                       12/24/2015  20  6   4   12  13
                       12/31/2015  19  4   5   3   2
                       1/7/2016    21  3   5   4   2")
  setnames(testdata,c("Date","Investigating",   "InProcess",    "Fixed",    "Inactionable", "Duplicate"))
  testdata<-testdata[1:5,]

  testdata$Date <- as.Date(testdata$Date,format="%m/%d/%Y")
  df <- melt(testdata,id.vars="Date")
  df$group <- ''

  for (i in 1:nrow(df)) {
    if ((df$variable[i] == "Investigating") | (df$variable[i] == "InProcess")) {
      df$group[i] <- ".Open"
    }
    else {
      df$group[i] <- as.character("Closed")
    }
  }

  setnames(df,c("date","state","count","group"))
  df$state <- relevel(df$state,"Investigating")
  cols <-  c(  Investigating = "coral2",InProcess = "coral4",Fixed = "olivedrab4", Inactionable = "olivedrab3", Duplicate =  "olivedrab1")

  df<- transform(df, 
           state.ord  = factor(
             df$state,
             levels=c( 'Investigating','InProcess','Fixed','Inactionable','Duplicate' ),
             ordered =TRUE))


  a <- ggplot(df,aes(x=group,y=count,fill=state,order=state.ord)) + 
    geom_bar(stat="identity",position="stack",aes(fill=state.ord)) +
    facet_grid(~date) +
    scale_fill_manual(values=cols,name="") +
    xlab("") + ylab("Count of Issues")

  a
}
rawr
  • 20,481
  • 4
  • 44
  • 78
njfrazie
  • 91
  • 1
  • 14
  • 1
    Can you find your answer here: http://stackoverflow.com/questions/32345923/how-to-control-ordering-of-stacked-bar-chart-using-identity-on-ggplot2?rq=1 (or any of the other closely related questions)? – Thomas Jan 20 '16 at 14:56
  • I don't get nearly the same plot using your data. plus you have left out some factor levels so I get NAs. did you try running this code before posting it? also you should add the packages that you are using – rawr Jan 20 '16 at 15:03
  • Sort of. If I use the method ggplot(df[order(df$state,decreasing=F),].... I end up with processed on the bottom but now Fixed is on top. The problem just switched to the green column – njfrazie Jan 20 '16 at 15:14
  • @rawr, I edited it to include the full dataset and code. Sorry about that – njfrazie Jan 20 '16 at 15:17
  • you can just order the variables differently and it will change the bar orders, as in `df <- df[order(-xtfrm(df$state.ord)), ]` but that will reverse everything, not sure if you want that, too. but that's the general strategy--ggplot will order the bars how they appear in the data – rawr Jan 20 '16 at 15:24
  • Your reordering of the factor worked just fine - it's just ordering things in the reverse order from what you want. Put a `rev()` around your vector in `levels = c(...)` and everything will match up. – Gregor Thomas Jan 20 '16 at 15:29
  • If you want to preserve the current legend ordering, after reversing the levels add `reverse = TRUE` to `scale_fill_manual()`. – Gregor Thomas Jan 20 '16 at 15:30
  • @Gregor, I reversed the order and now the red bar is correct, but now the green bar is flipped. I want to maintain the green bars order but flip the red – njfrazie Jan 20 '16 at 15:40
  • I got it now. thanks everyone! – njfrazie Jan 20 '16 at 15:42

1 Answers1

0

This section was added/modified to get the correct graph

df<- transform(df, 
           state.ord  = factor(
             df$state,
             levels=c('Duplicate','Inactionable','Fixed' ,'Investigating','InProcess' ),
             ordered =TRUE))

  df <- df[order(-xtfrm(df$state.ord)), ]
njfrazie
  • 91
  • 1
  • 14