ggplot2绘图小技巧(1) 修改特殊字符

本节不介绍新的绘图案例,这一节来介绍一个绘图中的小细节,如何修改图形轴文本中的特殊字符,主要介绍两种方法通过最常见的两个案例来进行展示,下面来看具体案例

加载R包

library(tidyverse)
library(linkET)
library(RColorBrewer)
library(ggtext)
library(magrittr)
library(reshape)
library(psych)

# devtools::install_github("Hy4m/linkET", force = TRUE)

加载数据

varechem <- read_tsv("varechem.xls")
varespec <-  read_tsv("varespec.xls")

案例一

例一主要介绍在常见的相关性热图中如何修改含有特殊字符的轴文本,由于完全是ggplot2语法所以使用 scale_ 函数完美兼容,主要代码如下;这类方法通用性很高很多图形中都能适用

scale_y_discrete(expand=c(0,0),position = 'right',
                   labels=c("N"=expression(NH[4]^+""),
                            "Ca"=expression(CaSO[4])))

计算相关性

table1 <- varechem
table2 <- varespec %>% select(1:20)

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"))

数据可视化

df %>% ggplot(.,aes(genus,env,col=r,fill=r))+
  geom_tile(color="grey80",fill="white",size=0.3)+
  geom_point(aes(size =abs(r)),shape=21)+
  geom_text(aes(label=p_signif),size=4,color="white",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"))+
  scale_size(range=c(1,10),guide=NULL)+
  guides(color=guide_colorbar(direction="vertical",reverse=F,barwidth=unit(.5,"cm"),
                              barheight=unit(11,"cm")))

案例二

案例二中我们使用linkET包来绘制相关性热图,此图近期朋友圈出境很频繁,但若文本中含有特殊字符该如何修改,小编初次使用上述 scale_ 函数修改名称出现不兼容的情况;后续发现可以使用ggtext::element_markdown函数通过markdown语法来解决此问题,那么前提就是在data.frame中将含有特殊字符的名称改为markdown格式,如下所示

修改名称

varechem2 <-  varechem %>% 
  dplyr::rename("NH4+"="N")

数据分析

mantel <- mantel_test(varespec, varechem2,
                      spec_select = list(Spec01 = 1:7,
                                         Spec02 = 8:18,
                                         Spec03 = 19:37,
                                         Spec04 = 38:44)) %>% 
  mutate(rd = cut(r, breaks = c(-Inf, 0.2, 0.4, Inf),
                  labels = c("< 0.2", "0.2 - 0.4", ">= 0.4")),
         pd = cut(p, breaks = c(-Inf, 0.01, 0.05, Inf),
                  labels = c("< 0.01", "0.01 - 0.05", ">= 0.05")))

数据可视化

qcorrplot(correlate(varechem2,method = "pearson"),diag=F,type="upper")+
  geom_tile()+
  geom_mark(size=2.5,sig.thres=0.05,sep="\n")+
  geom_couple(aes(colour=pd,size=rd),data=mantel,label.colour = "black",
              curvature=nice_curvature(0.15),
              nudge_x=0.2,
              label.fontface=2,
              label.size =4,
              drop = T)+
  scale_fill_gradientn(colours = RColorBrewer::brewer.pal(11,"RdBu"))+
  scale_size_manual(values = c(0.5, 1, 2)) +
  scale_colour_manual(values =c("#D95F02","#1B9E77","#A2A2A288")) +
  guides(size = guide_legend(title = "Mantel's r",override.aes = list(colour = "grey35"), order = 2),
         colour = guide_legend(title = "Mantel's p",override.aes = list(size = 3), order = 1),
         fill = guide_colorbar(title = "pearson's r",order = 3))+
  theme(plot.margin = unit(c(0,0.,0,0),units="cm"),
        legend.background=element_blank(),
        legend.key = element_blank(),
        axis.text=element_markdown(color="black",size=10))  

可以看到通过上述代码完美解决了修改特殊字符的问题,绘制一张好图所需要的细节实在是太多了,喜欢的朋友欢迎关注

你可能感兴趣的:(ggplot2绘图小技巧(1) 修改特殊字符)