R Programming: How do I control the order in a grouped stacked bar chart?

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?

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
}

Answers


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)), ]

Need Your Help

Linq with Left Join on SubQuery containing Count

.net linq count subquery left-join

I'm having difficulty translating sql to linq syntax.

How to handle the pylint message: Warning: Method could be a function

python oop pylint

I have a python class and ran pylint against it. One message it gave was: