欢迎关注R语言数据分析指南
结果图
图形解读
❝此图作为一张基础的相关性热图,通过代码将r值与p值进行结合直接展示在图中,同时对色块通过透明度来区分数据是否显著,其余方面没做过多的调整,其主要思路是筛选数据进行分类添加图层。 ❞
加载R包
代码语言:javascript复制library(tidyverse)
library(RColorBrewer)
library(ggtext)
library(magrittr)
library(reshape)
library(psych)
加载数据
代码语言:javascript复制table1 <- read_tsv("varechem.tsv")
table2 <- read_tsv("varespec.tsv") %>% select(1:20)
数据清洗
代码语言:javascript复制pp <- corr.test(table1,table2,method="pearson",adjust = "fdr")
df <- melt(pp$r) %>% mutate(pvalue=melt(pp$p)[,3],
p_signif=symnum(pvalue, corr = FALSE, na = FALSE,
cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
symbols = c("***", "**", "*", "", " "))) %>%
set_colnames(c("env","genus","r","p","p_signif")) %>%
mutate(p=round(p,digits = 2)) %>%
unite(.,col="p_value",p:p_signif,sep="",remove = T,na.rm = F)
数据可视化
代码语言:javascript复制ggplot()
geom_tile(data= df %>% filter(!str_detect(p_value, "\*")),
aes(genus,env,fill=r),alpha = 0.5)
geom_text(data= df %>% filter(!str_detect(p_value, "\*")),
aes(genus,env,col=r,fill=r,label=p_value),
size=3,color="grey70",hjust=0.5,vjust=0.7)
geom_tile(data= df %>% filter(str_detect(p_value, "\*")),
aes(genus,env,col=r,fill=r),alpha=1)
geom_text(data= df %>% filter(str_detect(p_value, "\*")),
aes(genus,env,col=r,fill=r,label=p_value),
size=3,color="black",hjust=0.5,vjust=0.7)
labs(x = NULL,y = NULL,color=NULL,fill=NULL)
scale_color_gradientn(colours = rev(RColorBrewer::brewer.pal(11,"RdBu")))
scale_fill_gradientn(colours = rev(RColorBrewer::brewer.pal(11,"RdBu")))
scale_y_discrete(expand=c(0,0),position = 'right',
labels=c("N"=expression(NH[4]^ ""),
"Ca"=expression(CaSO[4])))
scale_x_discrete(expand=c(0,0))
theme(axis.text.x=element_text(angle =90,hjust =1,vjust =0.5,
color="black",face = "bold",size = 10),
axis.text.y=element_text(color="black",face = "bold",size =10),
axis.ticks= element_blank(),
panel.spacing.y = unit(0,"cm"),
plot.background = element_blank(),
panel.background = element_blank())
scale_size(range=c(1,10),guide=NULL)
guides(color=guide_colorbar(direction="vertical",reverse=F,barwidth=unit(.5,"cm"),
barheight=unit(11,"cm")))