更改facet_wrap中facet的顺序

时间:2015-01-21 18:25:33

标签: r plot ggplot2 facet-wrap

我正在尝试使用facet_wrap绘制一些信息,但默认情况下,标题的排序是按字母顺序排列的。我的方面的标题是月份名称,我希望它们按照正确的顺序排列(Jan,Feb,Mar ....)。我正在使用以下代码:

q<-ggparcoord(data = avgRevenueBySubject,
              columns = 1:9,  
              showPoints = FALSE, 
              alphaLines = 0.3,
              groupColumn="Type",
              shadeBox = NULL,
              scale = "globalminmax",
              title = "Average revenue from unique customers trained each month starting 2014(Jan-Jun)"
)
# data to merge
mm <- cbind.data.frame(.ID=1:nrow(avgRevenueBySubject), Month=avgRevenueBySubject$Month)
#merge data
q$data<-merge(q$data, mm)
#finish plot commands
q <- q+ geom_vline(aes(xintercept=3.5),color='blue',linetype="dashed", size=1) +
  facet_wrap(~Month,scales='fixed', nrow = 2) + geom_line(size=2)
q <- q + theme_minimal() + xlab('Months') + ylab('Average Revenue (Dollars)') +
  theme(axis.text.y = element_text(hjust=0, angle=0), 
        axis.text.x = element_text(hjust=1, angle=45),
        plot.title = element_text(size=20)) +
  scale_color_manual(values=c('#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99'))
q

生成以下图:

enter image description here

以下是dput(head(avgRevenueBySubject))

的输出
structure(list(Month1 = c(27.42, 0, 30.1363636363636, 11.8138888888889, 
5.19, 18.8727272727273), Month2 = c(22.23, 0, 39.5727272727273, 
5.89444444444444, 0, 25.7727272727273), Month3 = c(23.96, 7.41428571428571, 
40.2954545454545, 18.6222222222222, 5.19, 18.9181818181818), 
    Month4 = c(39.27, 14.55, 6.9, 24.9583333333333, 28.95, 23.5318181818182
    ), Month5 = c(49.58, 7.41428571428571, 30.3136363636364, 
    14.8638888888889, 18.96, 16.3363636363636), Month6 = c(52.11, 
    3.70714285714286, 67.6727272727273, 14.4638888888889, 34.95, 
    34.6772727272727), Month7 = c(31.81, 29.1, 28.6090909090909, 
    15.5333333333333, 20.37, 25.5954545454545), Month8 = c(20.76, 
    3.70714285714286, 21.2318181818182, 21.1833333333333, 14.97, 
    23.2363636363636), Month9 = c(56.01, 3.70714285714286, 11.7954545454545, 
    27.2111111111111, 19.17, 41.5727272727273), Type = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("CC Delivery", "eServices", 
    "Filing", "ProcessServing", "Research"), class = "factor"), 
    Month = structure(1:6, .Label = c("April", "February", "January", 
    "June", "March", "May"), class = "factor")), .Names = c("Month1", 
"Month2", "Month3", "Month4", "Month5", "Month6", "Month7", "Month8", 
"Month9", "Type", "Month"), row.names = c(NA, 6L), class = "data.frame")

我希望按时间顺序排列这些方面。任何帮助将不胜感激。

0 个答案:

没有答案