ggplot2优雅的绘制饼状热图

欢迎关注R语言数据分析指南

最近看到一张非常有意思的图,暂且称之为饼状热图,与corrplot所绘制的有所不同此图使用的为离散型变量,例图如下所展示;由于作者未提供原始数据,小编自己构建了一份数据来进行初步的展示

论文内容

Systematic profiling of the chicken gut microbiome reveals dietary supplementation with antibiotics alters expression of multiple microbial pathways with minimal impact on community structure

此图乍一看觉得似曾相识,但是仔细一看又会发现好像不是那么的容易;应该是没有现成的包来一步出图,由于作者也未有提供数据那只有靠猜数据结构来进行可视化了,细节当然是很多,小编这次只是绘制主体图,细节问题留待以后再做介绍

加载R包

library(tidyverse)
library(scatterpie)
library(ggsci)
library(cowplot)

数据清洗

p <- read_tsv("data1.txt") %>% 
  filter(Compartments %in% c("BS","RS","RE","VE","SE","LE")) %>% 
  select(1,2,Compartments) %>% 
  left_join(.,read_tsv("data2.txt") %>% column_to_rownames(var="FAPROTAX") %>% t() %>% 
              as.data.frame() %>%
              rownames_to_column(var="Compartments") %>% 
              pivot_longer(-Compartments),by="Compartments") %>% 
  filter(Compartments=="BS",
         SampleID %in% c(read_tsv("data1.txt") %>% select(1) %>% 
                           distinct() %>% head(10) %>% pull())) %>% 
  select(-value,-Compartments) %>% 
  group_by(SampleID,name) %>% count(Phylum) %>% 
  pivot_wider(.,names_from=Phylum,values_from = n)

构建绘图数据

df <- p %>% left_join(.,p %>% select(name) %>% distinct() %>% rownames_to_column(var="lat"),by="name") %>% 
  mutate(long=case_when(SampleID =="BCCCK1" ~1,SampleID =="BCCCK2" ~2,SampleID =="BCCCK3" ~3,
                        SampleID =="BCCNPK1" ~4,SampleID =="BCCNPK2" ~5,SampleID =="BCCNPK3" ~6,
                        SampleID =="BCCNPKM1" ~7,SampleID =="BCCNPKM2" ~8,SampleID =="BCCNPKM3" ~9,
                        SampleID =="BHLCK1" ~10),lat=as.numeric(lat))

绘制饼状热图

p1 <- ggplot(aes(x=long,y=lat),data=df) +
  geom_tile(color="black",fill="white")+
  geom_scatterpie(aes(x=long,y=lat,r=0.4),data=df, color=NA,
                           cols=c("Abditibacteriota","Acidobacteriota","Actinobacteriota",
                                  "Alphaproteobacteria","Bacteroidota")) +
                             coord_equal()+
  scale_fill_brewer()+
  scale_x_discrete(expand = c(0,0))+
  scale_y_discrete(expand = c(0,0))+
  theme_test()+
  theme(axis.text.x=element_text(angle = 90,vjust = 0.5,hjust = 1),
        axis.ticks = element_blank(),
        axis.text.y=element_blank(),
        axis.title = element_blank(),
        legend.title = element_blank(),
        legend.key=element_blank(),   # 图例键为空
        legend.text = element_text(color="black",size=9), # 定义图例文本
        legend.spacing.x=unit(0.1,'cm'), # 定义文本书平距离
        legend.key.width=unit(0.5,'cm'), # 定义图例水平大小
        legend.key.height=unit(0.5,'cm'), # 定义图例垂直大小
        legend.background=element_blank())

绘制文本

p2 <- df %>% select(name,lat) %>% arrange(lat) %>% mutate(type="A") %>% 
  ggplot(aes(type,name))+
  coord_cartesian(clip="off")+
  scale_x_discrete(expand = c(0,0))+
#  scale_y_discrete(expand = c(0,0))+
  theme(panel.background = element_rect(fill="white"),
        axis.ticks = element_blank(),
        axis.text.y=element_text(color="black"),
        axis.title=element_blank(),axis.text.x=element_blank())

拼图

ggdraw(xlim=c(-0.45,1))+ 
  draw_plot(p2,x=-0.4)+
  draw_plot(p1,x=0.06)

你可能感兴趣的:(ggplot2优雅的绘制饼状热图)