# 跟着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

Figure1e的数据论文中是提供的，格式如下

## 柱形图的作图代码

df02<-read_excel("data/20230207/42255_2022_674_MOESM3_ESM.xlsx",
sheet = "Sheet1")
df02
df02$x<-factor(df02$x,levels = df02$x) pe1<-ggplot()+ geom_col(data=df02,aes(x=x,y=y), fill="red",color="black")+ theme_classic()+ scale_y_continuous(expand = expansion(mult=c(0,0)), limits = c(0,120), breaks = seq(0,120,20))+ labs(x=NULL,y="Number of DEGs",title="Art")+ theme(plot.title = element_text(hjust=0.5,face="bold")) df03<-read_excel("data/20230207/42255_2022_674_MOESM3_ESM.xlsx", sheet = "Sheet2") df03 df03$x<-factor(df03$x,levels = df03$x)

pe2<-ggplot()+
geom_col(data=df03,aes(x=x,y=y),
fill="#46b198",color="black")+
theme_classic()+
scale_y_continuous(expand = expansion(mult=c(0,0)),
limits = c(0,900),
breaks = seq(0,900,300))+
labs(x=NULL,y="Number of DEGs",title="Cap")+
theme(plot.title = element_text(hjust=0.5,face="bold"))

sheet = "Sheet3")
df04
df04$x<-factor(df04$x,levels = df04$x) pe3<-ggplot()+ geom_col(data=df04,aes(x=x,y=y), fill="#4472c4",color="black")+ theme_classic()+ scale_y_continuous(expand = expansion(mult=c(0,0)), limits = c(0,350), breaks = seq(0,350,50))+ labs(x=NULL,y="Number of DEGs",title="Ven")+ theme(plot.title = element_text(hjust=0.5,face="bold")) 三个柱形图的代码基本一样 ## 下三角相关系数热图 这个论文中没有提供数据，我手动整理下来了格式如下 ## 作图代码 library(readxl) library(ggplot2) library(tidyverse) library(paletteer) library(latex2exp) df<-read_excel("data/20230207/figure1f.xlsx") x_axis<-c('Brain','Heart','Lungs','Kidney','Liver','Vis AT') y_axis<-c('Sc AT','Vis AT','Liver','Kidney','Lungs','Heart') table(df$var1)
table(df\$var2)

df<-df %>%
mutate(var1=factor(var1,levels = x_axis),
var2=factor(var2,levels = y_axis))

txt.df<-data.frame(x=1:7,
y=7:1,
label=c('Brain','Heart','Lungs','Kidney','Liver','Vis AT','Sc AT'))
p1<-ggplot(data=df,aes(x=var1,y=var2))+
geom_tile(aes(fill=value),
color="black")+
geom_text(aes(label=value))+
geom_text(data=txt.df,
aes(x=x,y=y,label=label))+
#scale_x_discrete(expand = expansion(mult = c(0,0)))+
#scale_y_discrete(expand = expansion(mult = c(0,0)))+
theme_bw()+
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
legend.position = "left",
axis.title = element_blank())+
coord_cartesian(xlim = c(0,8),y=c(0,7))+
mid="white",
high="red",
breaks=c(-0.11,0,0.17),
name=TeX(r"(\textit{r} value)"),
midpoint=0)+
guides(fill=guide_colorbar(barheight = 10,
ticks.colour = "black"))

p1

p1+
labs(title="Art")+
theme(plot.title = element_text(hjust=0.5,
face="bold",
size=20)) -> pA

p1+
labs(title="Cap")+
theme(plot.title = element_text(hjust=0.5,
face="bold",
size=20),
legend.position = "none") -> pB

p1+
labs(title="Ven")+
theme(plot.title = element_text(hjust=0.5,
face="bold",
size=20),
legend.position = "none") ->pC

library(patchwork)

pA+pB+pC

(pe1+pe2+pe3)/(pA+pB+pC)

|
7月前
|

1108 0
|
7月前
|

451 0
|
7月前
|

116 0
|
9月前
|

R语言- ComplexHeatmap 绘制复杂热图示例
ComplexHeatmap是R语言中用于绘制复杂热图的一个重要包。它提供了一种灵活、高效、易于定制的方法来绘制热图，并支持多种数据类型和数据格式，支持包括多种热图类型，包括基本热图、聚类热图、分组热图、矩阵热图等。用户可以根据自己的需求选择不同的热图类型，并进行灵活的定制。在生物信息学、医学、生态学等领域得到广泛应用。 本文将通过一个复杂热图的创建示例分享 ComplexHeatmap的语法规则。
421 0
|
10月前
|

103 0
|
10月前
|

181 0
|