今天这篇推文小编给大家介绍一个一直想绘制的图表-议会图(parliament diagrams),当然这也是柱形图系列变形的一种。绘制这种图表也是超级简单的,只需使用R-ggpol包进行绘制即可,当然,改包还提供其他优秀的绘图函数,下面就一起来看下吧。
-
R-ggpol包简介
-
R-ggpol包实例介绍(议会图为主)
-
更多详细的数据可视化教程,可订阅我们的店铺课程:
R-ggpol包简介
-
官网:
https://github.com/erocoar/ggpol
-
绘图函数
作为ggplot2绘图系统中的一员,其安装方便(RStudio直接搜索安装即可)而且还可以通过ggplot2强大的绘图函数设计出自己的可视化作品。其提供的主要绘图函数如下:
-
geom_arcbar(): 绘制跨越180度的弧形条形图。
-
geom_parliament(): 绘制议会图(parliament diagrams),即圆弧上的圆圈,其中每个圆圈代表一个议会议员。指定了fill或color即可进行填充和颜色的映射。
-
geom_circle(): 绘制具有指定半径的圆多边形,注意和ggplot2的geom_point()区分开。
-
geom_tshighlight(): 可对绘制时间序列的图表进行突出作用,也是比较常用的绘图技巧。
-
geom_boxjitter(): 绘制了一个混合箱形图-半箱形图且带有误差线的图。
R-ggpol包实例介绍
「geom_boxjitter」:
(绘制了一个混合箱形图-半箱形图且带有误差线的图)
library(ggpol)
library(tidyverse)
library(ggtext)
library(hrbrthemes)
library(LaCroixColoR)
library(ggsci) # 使用科学系配色
df <- data.frame(score = rgamma(150, 4, 1),
gender = sample(c("M", "F"), 150, replace = TRUE),
genotype = factor(sample(1:3, 150, replace = TRUE)))
boxjitter <- ggplot(df) +
geom_boxjitter(aes(x = genotype, y = score, fill = gender),
jitter.shape = 21, jitter.color = NA,
jitter.height = 0, jitter.width = 0.04,
outlier.color = NA, errorbar.draw = TRUE) +
ggsci::scale_fill_jco() +
labs(
title = "Example of <span style='color:#D20F26'>ggpol::geom_boxjitter function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_boxjitter()</span>",
caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12)
)
geom_boxjitter example
顺便提一下,很喜欢sci包的这个配色,特适合绘制科研图表哦~~
「geom_tshighlight」
可对绘制时间序列的图表进行突出作用,也是比较常用的绘图技巧。
time_hlight <- ggplot(economics, aes(x = date, y = unemploy)) +
geom_line() +
geom_tshighlight(aes(xmin = as.Date("01/01/1990", format = "%d/%m/%Y"),
xmax = as.Date("01/01/2000", format = "%d/%m/%Y")),
alpha = 0.01) +
labs(
title = "Example of <span style='color:#D20F26'>ggpol::geom_tshighlight function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_tshighlight()</span>",
caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") +
hrbrthemes::theme_ft_rc(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12)
)
geom_tshighlight example
这里使用了hrbrthemes::theme_ft_rc()主题风格(暗黑系列),也是小编较常用的主题哦~~
「geom_arcbar」
绘制跨越180度的弧形条形图。
#构建数据
test_data_bt <- data.frame(
parties = factor(c("CDU", "CSU", "AfD", "FDP", "SPD",
"Linke", "Gruene", "Fraktionslos"),
levels = c("CDU", "CSU", "AfD", "FDP", "SPD",
"Linke", "Gruene", "Fraktionslos")),
seats = c(200, 46, 92, 80, 153, 69, 67, 2),
# colors = c("black", "blue", "lightblue", "yellow", "red",
# "purple", "green", "grey"),
stringsAsFactors = FALSE)
arcbar <- ggplot(test_data_bt) +
geom_arcbar(aes(shares = seats, r0 = 5, r1 = 10, fill = parties),
color="gray50" ,sep = 0.1) +
#scale_fill_manual(values = bt$colors) +
scale_fill_manual(values = lacroix_palette(type = "paired"))+
coord_fixed() +
# 修改图例
guides(fill=guide_legend(nrow=2,byrow=TRUE,reverse = TRUE,title=NULL))+
# theme_void(base_family = "Roboto Condensed") +
labs(
title = "Example of <span style='color:#D20F26'>ggpol::geom_arcbar function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_arcbar()</span>",
caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12),
# 去除刻度线和网格线
axis.text.x = element_blank(),
axis.text.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill="#FEFBCD"),
panel.border = element_rect(fill = NA,colour = "#FEFBCD"),
plot.background = element_rect(fill="#FEFBCD",colour = "#FEFBCD"),
# 修改图例参数
legend.position = 'bottom',
legend.direction = "horizontal",
legend.spacing.y = unit(0.1,"cm"),
legend.spacing.x = unit(0.1,"cm"),
legend.key.size = unit(0.8, 'lines'),
legend.text = element_text(margin = margin(r = .5, unit = 'cm'))
)
geom_arcbar example
这里我们在hrbrthemes::theme_ipsum() 主题基础上进行了一些修改,具体代码中均有解释。
「geom_parliament」
议会(项目)图(parliament diagrams)
library(readxl)
df<-read_excel("G:\\DataCharm\\可视化包介绍(绘制)\\ggpol\\parliament.xlsx")
#head(df)
df1<-df%>%group_by(Party)%>%summarise(Seats=sum(Seats))%>%arrange(Seats) # 简单的数据处理
df1$legend <- paste0(df1$Party," (", df1$Seats,")") # 拼接字符串
parliament <- ggplot(data = df1) +
geom_parliament(aes(seats = Seats,fill = Party),color="black") +
scale_fill_manual(values = lacroix_palette(type = "paired"),labels=df1$legend) +
# 修改图例
guides(fill=guide_legend(nrow=3,byrow=TRUE,reverse = TRUE,title=NULL))+
# 添加描述性文字
annotate("text", x = 0, y = 0.4,
label = "Seats in the Parliament :\n 423 occupied \n 27 vacant",
colour = "gray20",size=5) +
labs(
title = "Example of <span style='color:#D20F26'>ggpol::geom_parliament function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_parliament()</span>",
caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
# 定制化主题
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12),
# 去除刻度线和网格线
axis.text.x = element_blank(),
axis.text.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill="#FEFBCD"),
panel.border = element_rect(fill = NA,colour = "#FEFBCD"),
plot.background = element_rect(fill="#FEFBCD",colour = "#FEFBCD"),
# 修改图例参数
legend.position = 'bottom',
legend.direction = "horizontal",
legend.spacing.y = unit(0.1,"cm"),
legend.spacing.x = unit(0.1,"cm"),
legend.key.size = unit(0.8, 'lines'),
legend.text = element_text(margin = margin(r = .5, unit = 'cm'))
)
geom_parliament example
到这里呢,议会(项目)图(parliament diagrams)也就绘制完成了,虽然一些交互式工具(如Highcharts)也可快速绘制,打我们的目的还是进行细节、配色的练习哦~~
总结
本期推文终于介绍了小编一直心心念念的可视化图了,同时,小伙伴们也应知道的是,善用一些优秀的主题包和颜色包,可以使你的可视化作品颜值提升的不止一个档次哦~,好了,感兴趣的小伙伴赶快去练习下吧~~