其他
ggplot2优雅的绘制旭日图
欢迎关注R语言数据分析指南
❝本节来介绍如何使用「ggplot2」 & 「geomtextpath」来绘制旭日图,从此可以告别「sunburstR」,下面来看看具体操作
❞
安装并加载R包
package.list=c("tidyverse","geomtextpath","ggsci")
for (package in package.list) {
if (!require(package,character.only=T, quietly=T)) {
install.packages(package)
library(package, character.only=T)
}
}
加载数据
load("da.Rdata")
数据可视化
ggplot()+
geom_rect(data=data.frame(xmin=0, xmax=1, ymin=0, ymax=.75),
mapping=aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), fill="white")+
geom_rect(data=houses, mapping=aes(xmin=start_lim, xmax=end_lim, ymin=1, ymax=1.75,
fill=fill_col), color="white")+
geom_rect(data=parts, mapping=aes(xmin=start_lim, xmax=end_lim, ymin=1.90, ymax=2.75,
fill=fill_col), color="white")+
geom_textpath(data=houses, mapping=aes(x=mid_pt, y=1.35,label=toupper(speaker_type)), color="black",
size = 3, text_only = TRUE, family="Gill Sans") +
geom_textpath(data=houses %>% filter(speaker_type!="Neutral"),
mapping=aes(x=mid_pt, y=1.35,label=toupper(speaker_type)),
upright=FALSE, color="white", size = 3, text_only = TRUE) +
geom_textpath(data=parts %>% filter(speaker_type!="Neutral" & perc>=0.02),
mapping=aes(x=mid_pt, y=2.35,label=paste0(str_replace(speaker," ","\n"),"\n",
round(perc*100,1),"%")),
color="white",size =3, text_only = TRUE) +
geom_textpath(data=parts %>% filter(speaker_type=="Neutral" & perc>=0.025),
mapping=aes(x=mid_pt, y=2.35, label=paste0(str_replace(speaker," ","\n"),"\n",
round(perc*100,1),"%")),
color="black",size =3, text_only = TRUE) +
scale_fill_jco()+
scale_color_jco()+
coord_polar()+
theme(legend.position = "none",
panel.grid = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
panel.background = element_rect(fill="white"))
数据获取
❝可以看到主要用到「geom_rect()」 函数与绘制热图相似,通过「geom_textpath」函数添加弧度文本完美完成了图形,具体请参考geomtextpath官方文档,喜欢的小伙伴欢迎转发此文档附上一句话到朋友圈「30分钟后台截图给我」,即可获取对应的数据及代码,如未及时回复可添加我的微信
❞
欢迎大家扫描下方二位码加入「QQ交流群」,与全国各地上千位小伙伴交流
「关注下方公众号下回更新不迷路」,如需要加入微信交流群可添加小编微信,请备注单位+方向+姓名
往期推荐