ggplot2优雅的绘制车轱辘图
优雅 绘制 ggplot2
2023-06-13 09:13:01 时间
❝之前在一篇论文里面看到一张特殊的组合饼图感觉很不错,下面来构建数据进行复现,来看具体案例❞
加载R包
library(tidyverse)
library(scales)
library(ggtext)
library(patchwork)
library(cowplot)
library(RColorBrewer)
定义颜色
mycolors <- colorRampPalette(brewer.pal(12,"Paired"))(21)
构建数据集
df <- tribble(~group,~value,
"A", 40,
"B",60)
Figure-1
p1 <- df %>% arrange(desc(value)) %>%
ggplot(.,aes(x="",y=value,fill=group))+
geom_bar(width=1,stat="identity")+
coord_polar("y",start=0)+
geom_text(aes(y = value/2 + c(0, cumsum(value)[-length(value)]),
label = percent(value/100)), size=4)+
scale_fill_manual(values = mycolors)+
theme(axis.text.x=element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank(),
panel.background = element_blank(),
legend.position = "none",
legend.title = element_blank(),
legend.text = element_text(color="black",size=9),
legend.spacing.x=unit(0.1,'cm'),
legend.key.width=unit(0.5,'cm'),
legend.key.height=unit(0.5,'cm'),
legend.background=element_blank())
数据清洗
df2 <- read_tsv("otu_taxa_table.xls") %>%
select(OTU:C5,taxonomy) %>%
separate(taxonomy,
into=c("domain","phylum","class","order","family","genus","species"),sep=";") %>%
mutate_at(vars(c(`domain`:`species`)),~str_split(.,"__",simplify=TRUE)[,2]) %>%
select(A1:C5,genus) %>% drop_na() %>%
group_by(genus) %>% filter(genus !="") %>%
count() %>% ungroup() %>% mutate(value=n/sum(n)) %>% arrange(desc(value)) %>%
mutate(group=case_when(value < 0.0088999644 ~ "others",
TRUE ~ as.character(genus))) %>%
group_by(group) %>% summarise(value=sum(value)) %>% arrange(desc(value))
df2$group <- factor(df2$group,levels = df2$group)
Figure-2
p2 <- df2 %>% ggplot(.,aes(x="",y=value,fill=group))+
geom_bar(width=1,stat="identity")+
coord_polar("y",start=0)+
scale_fill_manual(values = mycolors)+
theme(axis.text.x=element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank(),
panel.background = element_blank(),
legend.position = "none",
legend.title = element_blank(),
plot.margin=unit(c(0,13,0,0),units="cm"),
legend.text = element_text(color="black",size=9),
legend.spacing.x=unit(0.1,'cm'),
legend.key.width=unit(0.5,'cm'),
legend.key.height=unit(0.5,'cm'),
legend.background=element_blank())
拼图
p2 %>% ggdraw()+draw_plot(p1,scale=0.4,x=0.38,y=0)
相关文章
- SpringBoot:如何优雅地进行响应数据封装、异常处理?
- ggplot2优雅的绘制森林图
- ggplot2优雅的绘制流程图
- ggplot2优雅的绘制镶嵌条形图
- 还在用 RedisTemplate?试试 Redis 官方 ORM 框架吧,用起来够优雅
- 优雅地处理python异常
- ggplot2优雅绘制时间趋势变化散点图
- R优雅的自定义绘制代谢通路图
- 优雅的解决 Oracle 返回多个值问题(oracle返回多个值)
- Linux系统如何优雅完成备份(怎样备份linux系统)
- Oracle排序:优雅的中文编码体系(oracle排序中文)
- Redis活跃时光优雅长久运行之道(是redis生存时间)
- MySQL 三范式表格优雅展示让数据规范化更加简单易懂(mysql三范式表格显示)
- Oracle中优雅的连接使用技巧(oracle中连接使用)
- 更新Redis更新之法以优雅勇敢挑战(如何优雅勇redis)
- 妙用Redis优雅配置防火墙(redis防火墙配置文件)