如何可视化组成随时间的“逐步”变化

时间:2017-01-30 17:14:44

标签: r ggplot2 geom-bar

我有一个数据框,其中包含选举当年各党派之间议席的分配情况。 最后,我想获得一个与此one类似的图表。多年来,我想想象一下议会的组成,而不仅仅是大选年。

results<-structure(list(party = c("PARTY1", "PARTY1", "PARTY1", "PARTY1", "PARTY2", "PARTY2", 
"PARTY2", "PARTY2", "PARTY2", "PARTY2", "PARTY3", "PARTY3", "PARTY3", "PARTY3", "PARTY3", 
"PARTY3", "PARTY3", "PART4", "PART4", "PART4", "PART4"), year = c(1996, 
1998, 2000, 2010, 1996, 2000, 2002, 2006, 2010, 2014, 1996, 1998, 
2000, 2002, 2006, 2010, 2014, 2002, 2006, 2010, 2014), party.seats = c(8, 
6, 5, 3, 19, 8, 10, 9, 7, 10, 9, 4, 6, 5, 3, 4, 5, 3, 7, 8, 6
)), class = "data.frame", row.names = c(NA, -21L), .Names = c("party", 
"year", "party.seats"))

我能够制作一个条形图,然而它只向我提供选举年的数据,并错过了选举之间的年份。

ggplot(data=results,aes(x=as.factor(year), y=party.seats, fill=party, label=party))+geom_bar(stat="identity")

我能够制作一个带有geom_area的ggplot图表,但这会产生误导,因为它表明座位的分配在选举后的几年内发生了变化(有一个污点,而不是“步骤”)。

ggplot(as.data.frame(xtabs(party.seats~year+party, results)), aes(x=as.Date(as.character(year), "%Y"), y = Freq, fill = party)) +  geom_area(position = "stack")

有任何帮助吗?我特别想知道是否有(时间序列相关?)命令将选举年的结果带到随后的所有年份,直到举行新的选举。所以基本上是一个命令,它将时间x的选举事件视为正在进行(=填充两年之间),直到在y时刻举行新的选举。

3 个答案:

答案 0 :(得分:2)

我认为geom_step正是您所寻找的,尽管最简单的实施方式不会将条/区域堆叠到分配的席位总数上(尽管可能更好):

ggplot(data=results
      , aes(x=year
            , y=party.seats
            , col=party)) +
  geom_step()

enter image description here

如果你真的想要你可以得到填充,虽然在@ Haboryme的回答中你需要在选举之间产生所有的积分。在这里,我使用dplyr / tidyr为选举之间的每一天添加一个新的数据行(你只需要足够小的分辨率,使“步骤”瞬间出现,而不是在整整一年内传播在最近一次选举之后添加了一些以使这些值实际显现的最终情节。然后,我在前进到下一次选举之前填补党派席位,并将缺席设置为0以获得良好的衡量标准(在该党有任何席位之前)。

请注意,你可以用选举的确切日期来扩展它,而不仅仅是几年而不需要修改太多

results %>%
  complete(year = full_seq(c(min(year), max(year) + 1), 1/365), party) %>%
  group_by(party) %>%
  fill(party.seats) %>%
  replace_na(replace = list(party.seats = 0)) %>%
  ggplot(
    aes(x=year
        , y=party.seats
        , fill=party)) +
  geom_area(position = "stack")

给出

enter image description here

我仍然更喜欢这些线条,因为当它们没有相互叠加时,更容易比较各方。例如,从2010年到2014年,很难从区域版本中判断出第2或第4方是否有更多席位(但从行中可以清楚地看出)。

答案 1 :(得分:1)

另一种选择可能是创建包含所有缺失年份的完整数据框:

library(tidyverse)                      
library(zoo)
all_years=seq(min(results$year),max(results$year)) #get the sequence of all the years considered
filled=data.frame(party=rep(unique(results$party),each=length(all_years)), #build a df with the seq of years for each party
                  year=rep(all_years,length(unique(results$party))))

然后合并您的数据并填写NA(如果在开始时为0,则使用最新的值):

df=merge(results,filled,by.y=c("party","year"),all.y=T)%>%
  group_by(party)%>%
  na.locf()%>%
  mutate(party.seats=coalesce(as.numeric(party.seats), 0))  

使用geom_barwidth=1绘制具有连续性的内容:

ggplot(data=df,aes(x=as.factor(year), y=party.seats, fill=party, label=party))+
  geom_bar(stat="identity",width = 1)

它给出(x轴需要一些调整):
enter image description here

答案 2 :(得分:1)

您也可以尝试花哨streamgraph(您也会像鼠标悬停工具提示一样获得plotly):

library(dplyr)
library(streamgraph)
results %>%
  streamgraph("party", "party.seats", "year") %>%
  sg_axis_x(1, "year", "%Y") %>%
  sg_legend(TRUE, "party")

enter image description here

results %>%
  streamgraph("party", "party.seats", "year", offset="zero", interpolate="step") %>%
  sg_axis_x(1, "year", "%Y") %>%
  sg_fill_brewer("PuOr")

enter image description here

相关问题