❝本节来介绍如何只使用「geom_segment」函数来绘制环状华夫图,这个名称也许更符合示例图表,「数据代码已经上传VIP群,请自行下载」 ❞
加载R包
代码语言:javascript复制library(tidyverse)
library(ggtext)
导入数据
代码语言:javascript复制df <- read_tsv("data.xls") %>% mutate(count =as.factor(EDA_count))
labels <- tibble(x = 0,y = 1:5,
text = c("A", "B","C","D","E"))
数据可视化
代码语言:javascript复制ggplot() # 构建画布
geom_segment(data =df, aes(x = x .5, xend = x (.5 .9),
y = plot_seg, yend = plot_seg, color = count),
size = 3) # 绘制色块
geom_segment(aes(x = 0 .5, xend = 2 (.5 .9), y = 1 - .4, yend = 1 - .4),
size = 0.5, color = "black", # 绘制一个箭头指明方向
arrow = arrow(length = unit(0.005, "npc")))
# 添加标签
geom_text(data = labels, aes(x = x, y = y, label = text),
colour="black",family="Times",fontface = "bold")
# 添加注释并通过 richtext引入markdown语法
annotate(geom = "richtext",x = 0,y = -6,label = "**count**",
color = "black", family = "Times",
fill = NA, label.color = NA, size = 5)
# 添加颜色
scale_color_brewer(palette="Paired")
# 设置坐标范围
ylim(-8, 6) xlim(0, 20)
# 转化为极坐标
coord_polar()
# 修改图例
guides(color = guide_legend(keywidth =2, keyheight = 1,title.position = "top",
title.hjust = .5))
# 设置主题
theme(
panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
legend.position = c(0.5,0.5),
legend.key = element_blank(),
legend.key.height = unit(5,"mm"),
legend.key.width = unit(5, "mm"),
legend.text = element_text(family = "Times", hjust = 0.5, size = 10, colour = "black", face = "bold"),
legend.title = element_blank(),
legend.background =element_blank(),
panel.background = element_rect(fill ="white", colour = "white"),
plot.background = element_rect(fill = "white", colour = "white", size = 3))
❝本节介绍到此结束,很简单的一个案例,如果数据量较大绘制成传统图形不方便对其进行展示,那么环状化展示也是一种不错的选择,喜欢的观众老爷欢迎分享转发