在ggplot2中按两个因子变量分组

时间:2017-08-04 09:09:14

标签: r ggplot2 data.table dplyr

我试图为五个不同的小组绘制两种不同类型的作品。我可以生成情节,它已经看起来没问题了,虽然最终ggplot中的顺序并不像我想要的那样。 我找到了arrange()的解决方案,但是虽然此步骤中的顺序是正确的,但最终结果再次不同。 我想对每个组(1,2,3,4,NA)进行分组,这两种不同类型的制作是相互重叠的。

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

2 个答案:

答案 0 :(得分:2)

你会对这样的事情感兴趣吗?它并不完全符合您的预期,但它为您的数据提供了良好的可视化效果。

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()

希望这有帮助!

答案 1 :(得分:1)

在调用fillcolor之前指定ggplot()是快速完成我认为您想要的方式:

# 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