2

I have the following sample data set which is a subset created by user selects in an application

cons_mergedAll <- structure(list(count = c(487L, 463L, 560L, 578L, 563L, 557L, 
65L, 48L, 324L, 447L, 166L, 108L, 351L, 301L, 389L, 384L, 333L, 
345L, 417L, 384L, 316L, 336L, 381L, 379L, 230L, 252L), type = c("open", 
"closed", "open", "closed", "open", "closed", "open", "closed", 
"open", "closed", "open", "closed", "open", "closed", "open", 
"closed", "open", "closed", "open", "closed", "open", "closed", 
"open", "closed", "open", "closed"), month = c(1, 1, 2, 2, 3, 
3, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 
12, 12), month_yr = c("2015 01", "2015 01", "2015 02", "2015 02", 
"2015 03", "2015 03", "2014 04", "2014 04", "2015 04", "2015 04", 
"2014 05", "2014 05", "2014 06", "2014 06", "2014 07", "2014 07", 
"2014 08", "2014 08", "2014 09", "2014 09", "2014 10", "2014 10", 
"2014 11", "2014 11", "2014 12", "2014 12"), month_yr2 = c("2015 Jan", 
"2015 Jan", "2015 Feb", "2015 Feb", "2015 Mar", "2015 Mar", "2014 Apr", 
"2014 Apr", "2015 Apr", "2015 Apr", "2014 May", "2014 May", "2014 Jun", 
"2014 Jun", "2014 Jul", "2014 Jul", "2014 Aug", "2014 Aug", "2014 Sep", 
"2014 Sep", "2014 Oct", "2014 Oct", "2014 Nov", "2014 Nov", "2014 Dec", 
"2014 Dec")), .Names = c("count", "type", "month", "month_yr", 
"month_yr2"), row.names = c(1L, 40L, 4L, 43L, 7L, 46L, 10L, 49L, 
13L, 52L, 15L, 54L, 18L, 57L, 21L, 60L, 24L, 63L, 27L, 66L, 30L, 
69L, 33L, 72L, 36L, 75L), class = "data.frame")

I am trying to do a bar plot using ggplot2 and prevent automatic ordering on x-bar but use the order on the dataset How can i approach it

Here is the sample code I am using

   cons_mergedAll <- cons_mergedAll[order(cons_mergedAll$month, cons_mergedAll$yr_report),]
   cons_mergedAll$month_yr2 <- factor(cons_mergedAll$month_yr2, ordered = T)
   ggplot(cons_mergedAll , aes( x=month_yr2 ,y=count, fill=type )) + geom_bar( stat="identity",position="dodge") +
     ylab("Number of Tickets") +   xlab("Month")

But sorts by alphabetical order still -- I have checked here and here but still doesnt sort from 2014-Apr to 2015-Apr

Cœur
  • 37,241
  • 25
  • 195
  • 267
Keniajin
  • 1,649
  • 2
  • 20
  • 43
  • 1
    The first link you give has exactly the code you need. – Aaron left Stack Overflow May 13 '15 at 15:01
  • 2
    See also [here](http://stackoverflow.com/questions/12774210/how-do-you-specifically-order-ggplot2-x-axis-instead-of-alphabetical-order) - the same principle: `factor(the-variable-in-which-you-want-to-set-order-of-levels, levels = a-vector-with-levels-in-the-desired-order)` – Henrik May 13 '15 at 18:13

2 Answers2

3

It looks like you're trying to use ordered=TRUE to keep the order in the original file. That's not what it does. Instead use levels=... and give it the levels in the order you want.

cons_mergedAll <- cons_mergedAll[order(cons_mergedAll$month_yr),]
cons_mergedAll$month_yr2 <- factor(cons_mergedAll$month_yr2,
                                   levels=unique(cons_mergedAll$month_yr2))
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
3

This could be an option

# Add a column with order, as per your requirement
cons_mergedAll$order = c(1:length(cons_mergedAll$count))

# use the added column to reorder() your data in ggplot
ggplot(cons_mergedAll , aes(reorder(month_yr2, order) ,count, fill=type )) +
geom_bar( stat="identity",position="dodge") +
ylab("Number of Tickets") +
xlab("Month")

enter image description here

Veerendra Gadekar
  • 4,452
  • 19
  • 24