? ???今天給大家所要展示的圖形是一種柱形圖的變形圖表——議會圖(parliament diagrams)!
安裝、加載R包
#安裝包
install.packages("ggparliament")
install.packages("tidyverse")
#加載包
library(ggparliament)
library(tidyverse)
數(shù)據(jù)
使用ggparliament包自帶的數(shù)據(jù)election_data:
df<-election_data %>%
filter(country == "Russia" & year == 2016)
使用parliament_data()函數(shù)將數(shù)據(jù)轉(zhuǎn)換成繪圖所需要的格式:
df1 <- parliament_data(election_data = df,
type = "semicircle", # 議會類型
parl_rows = 10, # 議會的行數(shù)
party_seats = df$seats) # 席位
繪圖
ggplot(df1, aes(x = x, y = y, colour = party_short)) +
geom_parliament_seats() +
geom_highlight_government(government == 1) +
geom_parliament_bar(colour = colour, party = party_long, label = TRUE) +#使用條形圖顯示比例
draw_majoritythreshold(n = 225, label = TRUE, type = "semicircle") +#添加閾值線
theme_ggparliament() +
labs(title = "R") +#標(biāo)題
scale_colour_manual(values = df1$colour,
limits = df1$party_short) +#顏色
draw_partylabels(type = "semicircle", ##標(biāo)簽
party_names = party_long,
party_seats = seats,
party_colours = colour)+
draw_totalseats(n = 450, type = "semicircle")#標(biāo)簽
其他類型展示
處理數(shù)據(jù)時泡徙,在type參數(shù)中修改相應(yīng)類型即可繪制不同類型會議圖:
df2 <- parliament_data(election_data = df,
type = "circle", # 議會類型
parl_rows = 10, # 議會的行數(shù)
party_seats = df$seats) # 席位
df3 <- parliament_data(election_data = df,
type = "classroom", # 議會類型
parl_rows = 11, # 議會的行數(shù)
party_seats = df$seats) # 席位
df4 <- parliament_data(election_data = df,
type = "horseshoe", # 議會類型
parl_rows = 10, # 議會的行數(shù)
party_seats = df$seats) # 席位
ggplot(df2, aes(x = x, y = y, color = party_short)) +
geom_parliament_seats() +
theme_ggparliament() +
labs(title = "Russia, 2016") +
scale_colour_manual(values = df1$colour,
limits = df1$party_short)
ggplot(df3, aes(x = x, y = y, color = party_short)) +
geom_parliament_seats() +
theme_ggparliament() +
labs(title = "Russia, 2016") +
scale_colour_manual(values = df1$colour,
limits = df1$party_short)
ggplot(df4, aes(x = x, y = y, color = party_short)) +
geom_parliament_seats() +
theme_ggparliament() +
labs(title = "Russia, 2016") +
scale_colour_manual(values = df1$colour,
limits = df1$party_short)