ggplot + dplyr 未正确堆叠数据或删除数据

ggplot + dplyr not stacking data correctly or removing data

我正在尝试堆叠条形图,但出于某种原因,我的条形图堆叠不正确。 The stacks are out of order and the colors consolidated. 我不太确定如何解决这个问题

g12<-terror %>% 
group_by(attacktype1_txt,iyear,nkillter) %>% 
ggplot(aes(x=iyear,y=nkillter,fill=attacktype1_txt)) +
geom_bar(stat='identity',position='stack')  + xlab('Year of Attack') + 
ylab('Number of Deaths')
g12=g12+ guides(fill=guide_legend(title="Attack Types"))
g12

or when I try and use the summarise function to make the bars stack correctly I get this oddity

g12<-terror %>% 
group_by(attacktype1_txt,iyear,nkillter) %>% summarise(number=n()) %>% 
ggplot(aes(x=iyear,y=nkillter,fill=attacktype1_txt)) +
geom_bar(stat='identity',position='stack')  + xlab('Year of Attack') + 
ylab('Number of Deaths')
g12=g12+ guides(fill=guide_legend(title="Attack Types"))
g12

这会生成一个正确堆叠的图表,但正如您通过比较这两个图表可以看到的那样,它丢掉了很多数据。是否有一些我可以使用的功能,它仍然可以整合数据而不会像总结那样丢弃数据?

我认为第一段代码有效。当然,您可以简化它:

ggplot(terror, aes(iyear, nkillter)) + 
geom_bar(aes(fill = attacktype1_txt), stat = "identity") +
    xlab('Year of Attack') + ylab('Number of Deaths') + 
    guides(fill=guide_legend(title="Attack Types"))

但这似乎是数据的正确表示,如果我理解正确的话。只是为了检查我们自己,让我们检查 2015 年:

> (terror %>% select(iyear, attacktype1_txt, nkillter) %>% 
              arrange(attacktype1_txt, nkillter) %>%
              filter(iyear==2015, nkillter > 0))
       iyear                     attacktype1_txt nkillter
    1   2015                       Armed Assault        1
    2   2015                       Armed Assault        1
    3   2015                       Armed Assault        1
    4   2015                       Armed Assault        1
    5   2015                       Armed Assault        1
    6   2015                       Armed Assault        2
    7   2015                   Bombing/Explosion        1
    8   2015                   Bombing/Explosion        1
    9   2015                   Bombing/Explosion        1
    10  2015                   Bombing/Explosion        1
    11  2015                   Bombing/Explosion        2
    12  2015 Hostage Taking (Barricade Incident)        1
    13  2015 Hostage Taking (Barricade Incident)        2
    14  2015         Hostage Taking (Kidnapping)        3

如剧情所示,我们有 7 起武装袭击死亡事件、6 起bombing/explosion、3 起路障事件和 3 起绑架事件。