0

I'm using the following code:

CaseType$Month <- factor(CaseType$Month, levels = c("11","12","1","2"))
CaseType$CaseType <- factor(CaseType$CaseType, levels = c("Civil Limited","Civil Unlimited","Family Law","Mental Health","Other","Probate"))

p <- ggplot(CaseType, aes(x=Month,y=Orders, fill=CaseType)) + geom_bar(stat='identity') + facet_wrap(~Product) +
  scale_x_discrete(limits=c(11,12,1,2)) +
  scale_fill_manual(values=c("#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99","#e31a1c","#fdbf6f","#ff7f00","#cab2d6")) 
p

to generate the following graph:

enter image description here

I have 2 problems here:

  1. I don't understand why the variable Casetype is not getting ordered properly despite ordering it manually. I would ideally like the colors to be stable across various bars and not jumble up.

  2. Also, why is the X axis all messed up? It doesn't show the order - 11,12,1,2. It still shows 1,2,11,12 and that too in a very messed up way.

Here's the output of dput(head(CaseType)):

structure(list(Orders = c(810L, 567L, 11071L, 964L, 19L, 73L), 
    Month = structure(c(2L, 2L, 3L, 2L, 4L, 4L), .Label = c("11", 
    "12", "1", "2"), class = "factor"), Product = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("eFiling ", "Filing ", "SOP "
    ), class = "factor"), CaseType = structure(c(1L, 6L, 2L, 
    3L, 5L, 6L), .Label = c("Civil Limited", "Civil Unlimited", 
    "Family Law", "Mental Health", "Other", "Probate"), class = "factor")), .Names = c("Orders", 
"Month", "Product", "CaseType"), row.names = c(NA, 6L), class = "data.frame")

How do I fix both the issues? Any help in this regard would be much appreciated.

Patthebug
  • 4,647
  • 11
  • 50
  • 91
  • 3
    I can't really do much without a fully reproducible example, but note that your x variable is a factor, but you are setting the axis limits with _integers_. The integer value stored behind an `"11"` in the factor is not `11`. Just look at your `dput` output! – joran Feb 04 '15 at 19:45
  • Oh yes, I didn't pay attention to that, it fixed the issue related to X axis. Thanks for that ! Would you happen to know how do I order the colors within the bars to be a little more consistent? – Patthebug Feb 04 '15 at 19:57

0 Answers0