R_ggparliament_会议布局


title: “R_ggparliament_会议布局”
author: “Li_Yuhui”
四川大学在读研究生

简介

参考来源:
https://github.com/RobWHickman/ggparliament
https://cran.r-project.org/web/packages/ggparliament/vignettes/basic-parliament-plots_1.html

ggparliament包用于会议座位布局的可视化,与ggplot2结合
能够反映会议成员的党派分布,投票分布,十分方便
支持多种座位布局方式:

  • 半圆形布局(semicircle layouts)
  • 马蹄形布局(horseshoe parliaments)
  • 长条形布局(opposing bench-style parliaments)
  • 教室布局(the classroom layout)
  • 圆形布局(a circular parliament)

parliament_data()数据重组

作图之前,必须使用parliament_data()函数重组数据,
通过添加特征参数,将产生座位的x和y坐标,theta角度,row所在行等新列
依靠新增加的这些列,就能确定座位的具体的位置,
不需要自己去构建复杂的坐标信息,这正是与ggplot2散点图区别的地方
语法:

parliament_data(election_data = NULL, parl_rows = NULL,
  party_seats = election_data$seats, group = NULL, plot_order = NULL,
  type = c("horseshoe", "semicircle", "circle", "classroom",
  "opposing_benches"))

参数解释:

  • election_data 表示数据源
  • type 表示座位布局方式
  • parl_rows 表示座位行数
  • party_seats 表示每个党派的席位数
  • group 表示分组 在这里插入图片描述
  • plot_order 表示指定绘图顺序
library(ggplot2)
library(ggparliament)
library(magrittr)
library(dplyr)

us_rep <- election_data %>% # ggparliament自带的各国选举数据集
  filter(country == "USA" &  # 行筛选
           year == 2016 & 
           house == "Representatives")
head(us_rep,n = 6L)

us_house_semicircle <- parliament_data(election_data = us_rep, # 数据源
                                       type = "semicircle", # 半圆形布局
                                       parl_rows = 10,      # 分10层布局
                                       party_seats = us_rep$seats) # 
head(us_house_semicircle)

setdiff(colnames(us_house_semicircle),colnames(us_rep) ) # 查看增加了哪些列

与ggplot2结合

重组数据后,可以在ggplot2中使用了。

  • 几何对象函数为geom_parliament_seats()
  • 图例函数geom_parliament_bar() 产生席位比例的图例
  • 主题函数为theme_ggparliament()
    语法:
geom_parliament_seats(mapping = NULL, data = NULL, stat = "identity",
  position = "identity", na.rm = FALSE, size = 3.5,
  show.legend = NA, inherit.aes = TRUE)
  
geom_parliament_bar(colour = colour, party = party, label = TRUE)

theme_ggparliament(legend, background_colour, border)

参数解释:

  • size 表示指定点的尺寸
  • colour 表示指定党派的颜色
  • party 表示指定要显示党派
  • label 表示是否显示比例标签

绘图流程:

us <- ggplot(us_house_semicircle, aes(x = x, y = y, colour = party_short)) +
geom_parliament_seats() + 
theme_ggparliament() +
labs(colour = NULL, 
     title = "United States Congress") +
scale_colour_manual(values = us_house_semicircle$colour, 
                    limits = us_house_semicircle$party_short) 

us

案例

马蹄形布局

library(ggplot2)
library(ggparliament)
library(magrittr)
library(dplyr)

# 构造数据
australia <- election_data %>%   
    filter(country == "Australia" & # 行筛选
               house == "Representatives" &
               year == 2016) 

# 重组数据,添加特征参数
australia_horseshoe <- parliament_data(election_data = australia,
                                       party_seats = australia$seats, # 党派席位数
                                       parl_rows = 4, # 分4层布局
                                       type = "horseshoe") # 马蹄形布局

# 绘图
au <- ggplot(australia_horseshoe, aes(x, y, colour = party_short)) +
    geom_parliament_seats() + 
    theme_ggparliament() +
    labs(colour = NULL, 
         title = "Australian Parliament") +
    scale_colour_manual(values = australia$colour, 
                        limits = australia$party_short) + 
    theme(legend.position = 'bottom')

au

在这里插入图片描述

长条形布局

library(ggplot2)
library(ggparliament)
library(magrittr)
library(dplyr)

# 重组数据,添加特征参数
uk_data <- election_data %>%
  filter(country == "UK") %>%
  filter(year == 2017) %>% # 行过滤
  parliament_data(election_data = ., 
                  party_seats = .$seats,
                  group = .$government, # 增加党内分组
                  parl_rows = 12,
                  type = "opposing_benches") # 长条形布局

# 画图
uk <- ggplot(uk_data, aes(x, y, colour = party_short)) +
  scale_colour_manual(values = uk_data$colour, limits = uk_data$party_short) +
  geom_parliament_seats() +
  theme_ggparliament()

uk

在这里插入图片描述

教室布局

library(ggplot2)
library(ggparliament)
library(magrittr)
library(dplyr)

# 重组数据,添加特征参数
russia_classroom <- election_data %>%
  filter(country == "Russia" &
    house == "Duma" &
    year == 2016) %>% 
  parliament_data(election_data = .,
                  party_seats = .$seats,
                  parl_rows = 11,
                  type = "classroom")


# 绘图
rus <- ggplot(russia_classroom, aes(x, y, colour = party_short)) +
  geom_parliament_seats() +
  theme_ggparliament() +
  labs(
    colour = NULL,
    title = "Russian Duma") +
  scale_colour_manual(
    values = russia_classroom$colour,
    limits = russia_classroom$party_short) +
  theme(legend.position = "bottom")

rus

在这里插入图片描述

圆形布局

library(ggplot2)
library(ggparliament)
library(magrittr)
library(dplyr)

# 重组数据,添加特征参数
russia_circle <- election_data %>%
  filter(country == "Russia" &
    house == "Duma" &
    year == 2016) %>% 
  parliament_data(election_data = .,
    party_seats = .$seats,
    parl_rows = 11,
    type = "circle")

# 绘图
russia_circle_example <- ggplot(russia_circle, aes(x, y, colour = party_short)) +
  geom_parliament_seats() +
  theme_ggparliament() +
  scale_colour_manual(
    values = russia_circle$colour,
    limits = russia_circle$party_short) +
  labs(colour = NULL) +
  theme(legend.position = "bottom")

russia_circle_example

在这里插入图片描述

只绘制赢的党派

library(ggplot2)
library(ggparliament)
library(magrittr)
library(dplyr)

seats <- c(rep("democrats", 40), rep("republicans", 30), rep("socialists", 20), rep("nationalists", 10))

made_up_layout <- parliament_data(election_data = NULL,
    party_seats = as.numeric(table(seats)),
    parl_rows = 4,
    type = "semicircle")

made_up_layout$party = seats

plot <- ggplot(made_up_layout, aes(x, y, colour = party)) +
    geom_parliament_seats() +
    theme_ggparliament() +
    labs(colour = NULL) +
    theme(legend.position = "bottom")

plot

在这里插入图片描述

增加比例条geom_parliament_bar()

library(ggplot2)
library(ggparliament)
library(magrittr)
library(dplyr)


data <- election_data[election_data$country == "USA" &
election_data$house == "Representatives" &
election_data$year == "2016",]

usa_data <- parliament_data(election_data = data,
type = "semicircle",
party_seats = data$seats,
parl_rows = 8)

ggplot(usa_data, aes(x, y, colour = party_long)) +
geom_parliament_seats() +
geom_parliament_bar(colour, party_long) + # 增加比例条,参数用usa_data数据集中的列
scale_colour_manual(values = usa_data$colour, limits = usa_data$party_long)  + # 手动调整颜色 
theme_ggparliament()

在这里插入图片描述

自行构建数据作图

library(ggplot2)
library(ggparliament)
library(magrittr)
library(dplyr)

# 创建数据  
house_of_reps <- data.frame(
  party = c("空缺","共和党", "民主党", "空缺"), # 党派名称
  seats = c(3, 236, 193, 3),                                     # 各个党派席位
  color = c("gray", "red", "blue", "gray")                       # 各个党派座位颜色
) 

# 数据整形
house_of_reps <- house_of_reps %>% 
  mutate(party = as.character(party), color = as.character(color)) %>% # 列运算,产生新列
  parliament_data(election_data = .,                  # 用.表示接受参数传递              
                            parl_rows = 8,            # 分8层布局
                            party_seats = .$seats,    # 各党派的席位
                            type = 'semicircle')      # 半圆形布局
head(house_of_reps)

# 绘图
ggplot(data = house_of_reps) +
  geom_parliament_seats(aes(x = x,  y = y, color = party)) + # 同时映射
  theme_ggparliament() +
  scale_color_manual(values = house_of_reps$color, 
                     limits = house_of_reps$party)

在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值