2017-08-04 50 views
0

我試圖爲五個不同的組劃分兩種不同類型的生產。我可以生成劇情,它已經看起來不錯,雖然最終ggplot的順序不是我想要的。 我發現arrange()的解決方案,但雖然此步驟中的排序是正確的,但最終結果會再次有所不同。 我想對每個組(1,2,3,4,NA)進行分組,兩種不同類型的生產相互正確。通過ggplot2中的兩個因子變量進行分組

library(zoo) 
library(data.table) 
library(ggplot2) 
library(dplyr) 
DT <- structure(list(Year.Quarter = structure(c(2015, 2015, 2015, 2015, 
              2015, 2015.25, 2015.25, 2015.25, 2015.25, 2015.25, 2015.5, 2015.5, 
              2015.5, 2015.5, 2015.5, 2015.75, 2015.75, 2015.75, 2015.75, 2015.75, 
              2016, 2016, 2016, 2016, 2016, 2016.25, 2016.25, 2016.25, 2016.25, 
              2016.25), class = "yearqtr") 
             , Group = c(2L, 1L, 4L, 3L, NA, 2L, 
                1L, 4L, 3L, NA, 2L, 1L, 4L, 3L, NA, 2L, 1L, 4L, 3L, NA, 2L, 1L, 
                4L, 3L, NA, 2L, 1L, 4L, 3L, NA) 
             , Conventional.Prod = c(11.78, 7.31, 7.34, 9.44, 28.72, 11.32, 5.27, 7.47, 8.08, 27.14, 11.49, 
                   4.65, 7.63, 7.07, 25.93, 10.69, 3.68, 6.96, 6.72, 18.31, 9.28, 
                   3.69, 6.86, 6.34, 19.14, 9.25, 3.69, 6.9, 6.16, 17.7) 
             , Unconventional.Prod = c(15.22, 10.69, 7.66, 15.56, 30.28, 15.68, 10.73, 7.53, 15.92, 29.86, 
                 13.51, 10.35, 7.37, 15.93, 28.07, 13.31, 10.32, 7.04, 16.28, 
           25.69, 12.72, 9.31, 7.14, 16.66, 25.86, 12.75, 9.31, 7.1, 16.84, 24.3)) 
         , .Names = c("Year.Quarter", "Group", "Conventional.Prod", "Unconventional.Prod"), row.names = c(NA, -30L), class = c("data.table", 
                 "data.frame")) 
data.table::melt(DT, 
       , id.vars = c("Year.Quarter", "Group") 
       , measure.vars = c("Conventional.Prod", "Unconventional.Prod") 
) %>% arrange(Year.Quarter, Group, variable) %>% ggplot(data = ., aes(x = Year.Quarter, y = value, color = variable, fill = as.factor(Group))) + 
     geom_area(stat = "identity", position = "fill") + 
     #geom_line(aes(x = Calendar.Data.Year.and.Quarter ,y = value)) + 
     theme(legend.title=element_blank()) + 
     scale_x_yearqtr(format = "%Y-Q%q",n = 8, expand = c(0,0)) 

arrange步驟之後的排序按預期:

Year.Quarter  Group  variable  value 
1:  2015 Q1  1 Conventional.Prod 7.31 
2:  2015 Q1  1 Unconventional.Prod 10.69 
3:  2015 Q1  2 Conventional.Prod 11.78 
4:  2015 Q1  2 Unconventional.Prod 15.22 
5:  2015 Q1  3 Conventional.Prod 9.44 
6:  2015 Q1  3 Unconventional.Prod 15.56 
7:  2015 Q1  4 Conventional.Prod 7.34 
8:  2015 Q1  4 Unconventional.Prod 7.66 
9:  2015 Q1 NA Conventional.Prod 28.72 
10:  2015 Q1 NA Unconventional.Prod 30.28 

但在最後情節的順序不知何故再次逆轉,從而使生產的主要羣體。 Plot with wrong grouping

+0

我不知道,如果這個問題可能解決的,通過使用'aes_group_order ',但不知道如何正確實現這一點。 – hannes101

回答

0

在調用ggplot()color前指定fill是一個快速的方法做什麼,我想你想:

# Not repeating all the code from your example, but change this line: 
ggplot(data = dat, aes(x = Year.Quarter, y = value, fill = as.factor(Group), color = variable)) 

enter image description here

+1

好吧,那就是我一直在尋找的東西。我嘗試了幾乎所有可能的填充和顏色組合,但我從未改變過順序。我從來沒有想到這可能會有所作爲...... – hannes101

1

你會對這樣的事情感興趣嗎?這不完全符合您的預期,但它爲您的數據提供了一個很好的可視化。

data.table::melt(DT, 
       , id.vars = c("Year.Quarter", "Group") 
       , measure.vars = c("Conventional.Prod", "Unconventional.Prod") 
) %>% ggplot(data = ., aes(x = Year.Quarter, y = value, fill = as.factor(Group))) + 
    scale_x_yearqtr(format = "%Y-Q%q") + 
    geom_bar(stat = "identity",position = "dodge") + 
    facet_grid(. ~ variable) + 
    theme_bw() 

希望這有助於!

+0

非常感謝,這是一個很好的代表。但我更感興趣的是展示每組兩種生產類型之間的差異化模式。所以這就是爲什麼我希望每個組都有相鄰的產品。 – hannes101

+0

如何用'geom_area(stat =「identity」)'替換我的方法中的'geom_bar'代碼?在添加'position =「fill」'選項後也可以看到差異(注意y軸的變化)。我認爲它應該在一定程度上滿足您的要求。 – Prem

+0

我喜歡facet plot的想法,我會盡力去做,儘管我把'facet_grid(variable〜。)+'改成'facet_grid(variable〜。)+',以便更容易地比較隨着時間的發展。 – hannes101

相關問題