我正在使用以下代码:
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
生成以下图表:
我有两个问题:
我不明白为什么变量Casetype
无法正确排序,尽管手动订购。理想情况下,我喜欢颜色在各种条形图上保持稳定而不是混乱。
另外,为什么X轴都搞砸了?它没有显示顺序 - 11,12,1,2。它仍然显示1,2,11,12,这也是一种非常混乱的方式。
以下是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")
如何解决这两个问题?在这方面的任何帮助将不胜感激。