爱生活、爱健康
LOVE LIFE AND HEALTH
本节继续介绍通过ggplot2来绘制环状条形图,重点使用annotate函数对数据进行注释,后台回复20210816获取本文数据及代码,喜欢的小伙伴欢迎扫描文末二维码加入我的交流群更多精彩内容为您而准备
library(tidyverse)
自定义颜色
colors <-c("#FED439FF","#709AE1FF",
"#D5E4A2FF","#197EC0FF","#F05C3BFF","#46732EFF",
"#71D0F5FF","#370335FF","#075149FF","#C80813FF","#91331FFF",
"#1A9993FF","#FD8CC1FF")
构建数据
data1 <- mtcars %>% head(6) %>%
mutate_if(is.numeric, function(x) x+10) %>%
log10() %>% as.data.frame() %>%
rownames_to_column("type") %>%
pivot_longer(-type) %>% arrange(name) %>%
mutate(type=factor(type))
计算角度
计算标签的对齐方式:右对齐还是左对齐
如果在图的左侧,则标签当前的角度 < -90
hjust=0表示左对齐;1表示右对齐
empty_bar <- 0
data1$id <- seq(1,nrow(data1))
label_data <- data1
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id-0.5) /number_of_bar
label_data$hjust<-ifelse( angle < -90, 1, 0)
label_data$angle<-ifelse(angle < -90, angle+180, angle)
构建分组线段
df <- data1 %>%
group_by(name) %>%
summarize(start=min(id), end=max(id) - empty_bar) %>%
rowwise() %>%
mutate(title=mean(c(start,end))) %>%
mutate(name=as.factor(name))
数据可视化
ggplot(data1,aes(id,value,fill=type))+
geom_bar(stat="identity",alpha=0.8)+
scale_fill_manual(values = colors)+
labs(x=NULL,y=NULL)+
ylim(-7,8)+
coord_polar(start = 0)+
theme_void()+
theme(
legend.text = element_text(color="black"),
legend.title=element_blank(),
legend.spacing.x=unit(0.2,'cm'),
legend.key=element_blank(),
legend.key.width=unit(0.3,'cm'),
legend.key.height=unit(0.3,'cm'),
legend.position=c(0.5,0.5))+
# 添加分组线段
# geom_segment(data=df,aes(x = start,y =-0.3,xend = end,yend =-0.3),
# colour = "black",alpha=0.8,size=0.5,inherit.aes = FALSE)+
# 添加标签
geom_text(data=label_data,aes(x=id, y=value+0.5,label=type, hjust=hjust),
color="black", fontface="plain",size=2.5,
angle= label_data$angle, inherit.aes = FALSE )+
# 添加文本
annotate("text",x=4,y=-0.8,label="A",angle=-35,size=3.5,color="black")+
annotate("text",x=10,y=-0.8,label="B",angle=-50,size=3.5,color="black")+
# 添加外圈
geom_segment(aes(x=0, y=8,xend=66.5,yend =8),size=1.5,color="#3B9AB2",
arrow = arrow(length = unit(0, "npc"),type="closed"))+
# 添加竖线
annotate("segment",x=0,xend =0,y=-3,yend =8,colour="#FF0000")+
annotate("segment",x=6.5,xend =6.5,y=-3,yend =8,colour="#00A08A")+
annotate("segment",x=12.5,xend =12.5,y=-3,yend=8,colour="#FF0000")+
annotate("segment",x=18.5,xend =18.5,y=-3,yend =8,colour="#00A08A")+
annotate("segment",x=24.5,xend =24.5,y=-3,yend =8,colour="#FF0000")+
annotate("segment",x=30.5,xend =30.5,y=-3,yend =8,colour="#00A08A")+
annotate("segment",x=36.5,xend =36.5,y=-3,yend =8,colour="#FF0000")+
annotate("segment",x=42.5, xend =42.5,y=-3,yend =8,colour="#00A08A")+
annotate("segment",x=48.5,xend =48.5,y=-3,yend =8,colour="#FF0000")+
annotate("segment",x=54.5,xend =54.5,y=-3,yend =8,colour="#00A08A")+
annotate("segment",x=60.5,xend =60.5,y=-3,yend =8,colour="#FF0000")+
# 添加内圈
geom_segment(aes(x=0, y=-3,xend=66.5,yend =-3),size=0.5,color="#3B9AB2",
arrow = arrow(length = unit(0, "npc"),type="closed"))+
geom_segment(aes(x=0, y=-0.1,xend=66.5,yend =-0.1),size=0.5,color="black",
arrow = arrow(length = unit(0, "npc"),type="closed"))
后台回复202100816获取本文代码,欢迎加入我的QQ交流群,加我微信可邀您进入微信交流群
QQ交流群

小编微信

往期精彩:
配置R与Rstudio
ggplot2中的一些关键概念
tidyverse的基础使用
ggplot2绘制终极版热图
ggtree绘制进化树
初探相关性热图
再探相关性热图
ggplot2绘制相关性热图
初探柱状图
绘制具有显著性的条形图
ggplo2绘制经典条形图
ggplo2绘制双误差线条行图
揭开ggplot2中stat图层的神秘面纱
R中的图片注释神包aplot
ggplot2使用patchwork高质量拼图
ggplot2绘制基础散点图
ggplot2绘制经典散点图-1
ggplot2绘制经典散点图-2
ggplot2绘制云雨图
ggplot2绘制经典云雨图
ggplot2绘制哑铃图
ggplot2绘制经典哑铃图
ggplot2添加gif

