跟着Nature Metabolism学作图:R语言ggplot2水平堆积柱形图并添加同组连线

论文

Single-cell profiling of vascular endothelial cells reveals progressive organ-specific vulnerabilities during obesity

https://www.nature.com/articles/s42255-022-00674-x#Sec58

s42255-022-00674-x.pdf

https://github.com/Osynchronika/sc_EC_obesity_atlas

读取数据

library(readxl)
library(tidyverse)
dat

指定因子水平和配色

x_levels<-c("Prolif","EC-art","EC-cap1","EC-cap2",
"EC-lymph1","EC-lymph2","EC-ven","EC-venule","EC-ang")

"#63b32f","#527dbf","#8eb1de","#de2080")

堆积柱形图的代码

width<-0.4

dat %>%
pivot_longer(!group) %>%
mutate(name=factor(name,levels = c("Western","chow"))) %>%
mutate(group=factor(group,levels = rev(x_levels))) %>%
ggplot(aes(x=value,y=name))+
geom_bar(aes(fill=group),
stat="identity",
position = "fill",
width = width)+
scale_fill_manual(values = rev(fill.colors)) -> p1

p1

计算添加线段的位置坐标

dat[match(x_levels,dat\$group),] %>%
mutate(x1=cumsum(chow/sum(chow)),
x2=cumsum(Western/sum(Western))) -> new.df

在p1的基础上添加线段并整体美化

p1+
geom_segment(data=new.df,
aes(x=x1,xend=x2,y=2-width/2,yend=1+width/2),
lty="dashed",
color="gray",
size=1)+
theme_classic()+
scale_x_continuous(position = "top",
expand = expansion(mult=c(0,0)),
breaks = seq(0,1,by=0.1),
labels=c(0,"",20,"",40,"",60,"",80,"",100))+
theme(axis.ticks.y = element_blank(),
panel.grid.major.x = element_line(),
axis.title = element_blank(),
plot.title = element_text(hjust=0.5),
axis.text.y = element_text(face="bold",size=25),
legend.title = element_blank())+
labs(title="Percentage of cells per cluster")

|
7月前
|

1082 0
|
7月前
|

115 0
|
10月前
|

103 0
|
10月前
|

180 0
|
10月前
|

92 0
|