論文
A global reptile assessment highlights shared conservation needs of tetrapods
https://www.nature.com/articles/s41586-022-04664-7#Sec33
數據代碼鏈接
https://github.com/j-marin/Global-reptile-assessment-
今天的推文學習一下推文中的Figure 1b的環(huán)形堆積柱形圖,沒有找到論文中的作圖代碼,但是找到了原始數據集,有了原始數據集就可以自己寫代碼來做這個圖

image.png
代碼可以參考這個鏈接 https://r-graph-gallery.com/297-circular-barplot-with-groups.html
部分示例數據截圖

image.png
每個組之間的空白應該是通過缺失值實現(xiàn)的
讀取數據
library(readxl)
dat01<-read_excel("data/20220630/41586_2022_4664_MOESM3_ESM.xlsx",
sheet = "Fig 1b",na="NA")
head(dat01)
給數據集增加一列用來做x
library(tidyverse)
dat01 %>%
mutate(new_x = rep(paste0('X',formatC(1:31,width = 2,flag = 0)),each=8)) -> dat01
最基本的堆積柱形圖
library(ggplot2)
ggplot(data = dat01,aes(x=new_x,y=n,fill=rlCodes))+
geom_bar(stat = "identity",position = "fill")

image.png
這里有一個問題是論文中的圖第一個柱子不是1,暫時沒有想明白是什么意思
這里有點看起來是分組堆積柱形圖的效果,ggplot2好像沒有做分組堆積柱形圖的函數,他這里的處理方式是增加x,并給新增加的x賦值為零
變成環(huán)狀
ggplot(data = dat01,aes(x=new_x,y=n,fill=rlCodes))+
geom_bar(stat = "identity",position = "fill")+
coord_polar()+
ylim(-1,NA)

image.png
接下來是修改細節(jié)
ggplot(data = dat01,aes(x=new_x,y=n,fill=rlCodes))+
geom_bar(stat = "identity",position = "fill")+
coord_polar()+
scale_x_discrete(expand = expansion(add = 0),
labels=coalesce(dat01$orderName[seq(1,248,8)],""))+
scale_y_continuous(limits = c(-1,NA))+
theme(axis.text.x = element_text(angle = cumsum(c(90,-rep(12,15))),
vjust=0,hjust = 1),
panel.background = element_blank(),
axis.text.y = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank())-> p1
p1

image.png
p1 +
annotate(geom = "text",x=2,y=-0.1,label="Birds",angle=-15)+
annotate(geom = "text",x=8,y=-0.1,label="Amphibians",angle=-90)+
annotate(geom = "text",x=14,y=-0.1,label="Mammals",angle=15)+
annotate(geom = "text",x=24,y=-0.1,label="Reptiles",angle=90)+
scale_fill_manual(values = c("LC"="#98d09d","NT"="#d7e698",
"DD"="#dadada","VU"="#fbf398",
"EN"="#f7a895","CR"="#e77381",
"EW"="#9b8191","EX"="#8f888b"),
limits=c("EX","EW","CR","EN","VU","DD","NT","LC"),
name="") -> p2
p2

image.png
構造新的數據集用來添加坐標軸
new_dat01<-data.frame(
x=0.5,xend=0.3,
y=c(0,0.25,0.5,0.75,1),
yend=c(0,0.25,0.5,0.75,1)
)
new_dat02<-data.frame(x=0.3,
y=c(0,0.25,0.5,0.75,1),
label=c(0,0.25,0.5,0.75,1)*100)
p2 + annotate(geom = "segment",
x=0.5,xend=0.5,y=0,yend=1)+
geom_segment(data=new_dat01,
aes(x=x,xend=xend,y=y,yend=yend),
inherit.aes = FALSE)+
geom_text(data=new_dat02,aes(x=x,y=y,label=label),
inherit.aes = FALSE,hjust=1)+
annotate(geom = "text",x=30,y=0.5,label="Species\nthreatened (%)",
angle=90,vjust=-0.1)

image.png
拼圖
library(patchwork)
library(rcartocolor)
p3+
scale_fill_manual(values = carto_pal(8,"Safe"))+
theme(legend.position = "bottom")+
guides(fill=guide_legend(nrow = 1))+
p3+
theme(legend.position = "bottom")+
guides(fill=guide_legend(nrow = 1))

image.png
示例數據可以到論文中去下載,示例代碼可以在推文中復制
歡迎大家關注我的公眾號
小明的數據分析筆記本
小明的數據分析筆記本 公眾號 主要分享:1、R語言和python做數據分析和數據可視化的簡單小例子;2、園藝植物相關轉錄組學、基因組學、群體遺傳學文獻閱讀筆記;3、生物信息學入門學習資料及自己的學習筆記!