2017-10-11 40 views
0

我使用ggplot()繪製的樣本羣組數據未按照適當的時間順序進行顏色編碼或顯示。下面的代碼是用來生成的情節:gomplot中的Geom_area訂單

library(ggplot2) 

blues <- colorRampPalette(c('lightblue', 'darkblue')) 

p <- ggplot(cohort.chart, aes(x=month, y=users, group=cohort)) 
p + geom_area(aes(fill = cohort)) + 
    scale_fill_manual(values = blues(12)) + 
    ggtitle('Users by cohort') + 
    theme(plot.title = element_text(hjust = 0.5)) + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) 

enter image description here

正如你可以在圖例中看到,幾個月都沒有按時間順序排列,並且反映在情節。我試圖用as.yearmon()cohortmonth改爲yearmon對象,但是這會產生連續變量不能使用的錯誤。如何解決ggplot()中的這個問題?

下面是數據

> dput(cohort.chart) 
structure(list(cohort = structure(c(11L, 10L, 3L, 5L, 4L, 8L, 
1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 
6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 
11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 
3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 
8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 
7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 
12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 
10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 
4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 
9L, 7L, 6L, 2L, 12L), .Label = c("Apr 2017", "Aug 2017", "Dec 2016", 
"Feb 2017", "Jan 2017", "Jul 2017", "Jun 2017", "Mar 2017", "May 2017", 
"Nov 2016", "Oct 2016", "Sep 2017"), class = "factor"), month = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L), .Label = c("Oct 2016", "Nov 2016", 
"Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017", "Apr 2017", "May 2017", 
"Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017"), class = "factor"), 
    users = c(795, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 672, 92, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 526, 48, 26, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 506, 37, 14, 26, 0, 0, 0, 0, 0, 0, 0, 0, 514, 
    46, 18, 19, 37, 0, 0, 0, 0, 0, 0, 0, 532, 47, 16, 18, 22, 
    57, 0, 0, 0, 0, 0, 0, 589, 55, 15, 20, 24, 39, 88, 0, 0, 
    0, 0, 0, 548, 53, 18, 21, 25, 39, 62, 172, 0, 0, 0, 0, 559, 
    53, 20, 20, 23, 36, 61, 139, 133, 0, 0, 0, 538, 58, 17, 18, 
    22, 39, 54, 130, 99, 109, 0, 0, 519, 45, 16, 19, 26, 39, 
    50, 125, 96, 86, 115, 0, 530, 54, 18, 20, 23, 37, 51, 129, 
    88, 75, 85, 126)), row.names = c(NA, -144L), .Names = c("cohort", 
"month", "users"), class = "data.frame") 

情節應該類似於以下內容:

enter image description here

回答

2

基於數據幀你提供,你的month列在右邊的因子水平,但您的cohort列不是,因此請將您的cohort列的因子級別更改爲與month列相同。

library(ggplot2) 

blues <- colorRampPalette(c('lightblue', 'darkblue')) 

# Change the factor level 
cohort.chart$cohort <- factor(cohort.chart$cohort, levels = levels(cohort.chart$month)) 

p <- ggplot(cohort.chart, aes(x=month, y=users, group=cohort)) 
p + geom_area(aes(fill = cohort)) + 
    scale_fill_manual(values = blues(12)) + 
    ggtitle('Users by cohort') + 
    theme(plot.title = element_text(hjust = 0.5)) + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) 

enter image description here

2

當生成數據幀,R(自動地)轉換的字符串轉換爲因素,按字母順序排列的每個新cohort日期創建一個新的因子。

cohort.chart$cohort 
[1]... 12 Levels: Apr 2017 Aug 2017 Dec 2016 Feb 2017 Jan 2017 Jul 2017 Jun 2017 Mar 2017 May 2017 ... Sep 2017 

您可以使用as.yearmon來解決這個問題,你只需要轉換回一個因素之後。

cohort.chart$cohort <- as.yearmon(cohort.chart$cohort) 
cohort.chart$cohort <- as.factor(cohort.chart$cohort)