该内容已被发布者删除 该内容被自由微信恢复。
文章于 4月1日 下午 2:10 被检测为删除。
被用户删除
其他
R语言绘图 | 使用ggchicklet绘制圆角条形图
安装和加载R包
# 安装 ggchicklet 包
install.packages("ggchicklet", repos = "https://cinc.rud.is")
#加载R包
library(ggchicklet)
library(hrbrthemes)
library(tidyverse)
加载和处理数据
# 载入 debates2019 数据集
data("debates2019")
debates2019 %>%
filter(debate_group == 1) %>% # 筛选出辩论组为1的数据
mutate(speaker = fct_reorder(speaker, elapsed, sum, .desc=FALSE), # 重新排序演讲者,按照时间 elapsed 的总和
topic = fct_other(topic, c("Immigration", "Economy", "Climate Change", "Gun Control", "Healthcare", "Foreign Policy"))) -> df # 将话题中的其他值转换为 "Other",仅保留指定的主题
定义主题颜色
# 定义主题的颜色
pal7 = c("Immigration" = "#194a55",
"Economy" = "#f49128",
"Gun Control" = "#f26115",
"Foreign Policy" = "#187c65",
"Healthcare" = "#83ba9e",
"Other" = "#cccccc")
绘图
#绘图
df %>%
ggplot(aes(speaker, elapsed, group=timestamp, fill = topic)) + # 使用 ggplot 创建图表
geom_chicklet( width = 0.75, color="grey15", size=0.1) + # 添加 chicklet 几何对象
scale_y_continuous(
expand = c(0, 0), # 设置 y 轴的扩展
position = "right", # 将 y 轴移动到右侧
breaks = seq(0, 14, 2), # 设置 y 轴刻度
labels = c(0, sprintf("%d min.", seq(2, 14, 2)))) + # 设置 y 轴标签
scale_fill_manual(
name = NULL, # 设置填充的图例名称
values = pal7, # 设置填充的颜色
breaks = setdiff(unique(debates2019$topic), "Other")) + # 设置填充颜色的分组
guides(fill = guide_legend(nrow = 1)) + # 设置图例
coord_flip() + # 交换 x 和 y 轴
labs(
x = NULL, y = NULL, fill = NULL, # 设置 x、y 轴和填充的标签
title = "How Long Each Candidate Spoke", # 设置标题
subtitle = "Nights 1 & 2 of the June 2019 Democratic Debates") + # 设置副标题
theme_minimal() + # 使用最小化主题
theme(plot.background = element_rect(fill='white', color='white'), # 设置绘图区域的背景
axis.text.x = element_text(color = "gray60", size = 10), # 设置 x 轴文本的颜色和大小
legend.position = "top") # 设置图例的位置
完整代码如下
# 安装 ggchicklet 包
install.packages("ggchicklet", repos = "https://cinc.rud.is")
#加载R包
library(ggchicklet)
library(hrbrthemes)
library(tidyverse)
# 载入 debates2019 数据集
data("debates2019")
debates2019 %>%
filter(debate_group == 1) %>% # 筛选出辩论组为1的数据
mutate(speaker = fct_reorder(speaker, elapsed, sum, .desc=FALSE), # 重新排序演讲者,按照时间 elapsed 的总和
topic = fct_other(topic, c("Immigration", "Economy", "Climate Change", "Gun Control", "Healthcare", "Foreign Policy"))) -> df # 将话题中的其他值转换为 "Other",仅保留指定的主题
# 定义主题的颜色
pal7 = c("Immigration" = "#194a55",
"Economy" = "#f49128",
"Gun Control" = "#f26115",
"Foreign Policy" = "#187c65",
"Healthcare" = "#83ba9e",
"Other" = "#cccccc")
#绘图
df %>%
ggplot(aes(speaker, elapsed, group=timestamp, fill = topic)) + # 使用 ggplot 创建图表
geom_chicklet( width = 0.75, color="grey15", size=0.1) + # 添加 chicklet 几何对象
scale_y_continuous(
expand = c(0, 0), # 设置 y 轴的扩展
position = "right", # 将 y 轴移动到右侧
breaks = seq(0, 14, 2), # 设置 y 轴刻度
labels = c(0, sprintf("%d min.", seq(2, 14, 2)))) + # 设置 y 轴标签
scale_fill_manual(
name = NULL, # 设置填充的图例名称
values = pal7, # 设置填充的颜色
breaks = setdiff(unique(debates2019$topic), "Other")) + # 设置填充颜色的分组
guides(fill = guide_legend(nrow = 1)) + # 设置图例
coord_flip() + # 交换 x 和 y 轴
labs(
x = NULL, y = NULL, fill = NULL, # 设置 x、y 轴和填充的标签
title = "How Long Each Candidate Spoke", # 设置标题
subtitle = "Nights 1 & 2 of the June 2019 Democratic Debates") + # 设置副标题
theme_minimal() + # 使用最小化主题
theme(plot.background = element_rect(fill='white', color='white'), # 设置绘图区域的背景
axis.text.x = element_text(color = "gray60", size = 10), # 设置 x 轴文本的颜色和大小
legend.position = "top") # 设置图例的位置
参考资料:https://github.com/hrbrmstr/ggchicklet?tab=readme-ov-file