歡迎關(guān)注R語言數(shù)據(jù)分析指南
最近看到一張非常有意思的圖呀狼,暫且稱之為餅狀熱圖,與
corrplot
所繪制的有所不同此圖使用的為離散型變量浩蓉,例圖如下所展示础钠;由于作者未提供原始數(shù)據(jù)浩峡,小編自己構(gòu)建了一份數(shù)據(jù)來進(jìn)行初步的展示
論文內(nèi)容
Systematic profiling of the chicken gut microbiome reveals dietary supplementation with antibiotics alters expression of multiple microbial pathways with minimal impact on community structure
此圖乍一看覺得似曾相識(shí)毙替,但是仔細(xì)一看又會(huì)發(fā)現(xiàn)好像不是那么的容易岸售;應(yīng)該是沒有現(xiàn)成的包來一步出圖,由于作者也未有提供數(shù)據(jù)那只有靠猜數(shù)據(jù)結(jié)構(gòu)來進(jìn)行可視化了厂画,
細(xì)節(jié)當(dāng)然是很多凸丸,小編這次只是繪制主體圖,細(xì)節(jié)問題留待以后再做介紹
加載R包
library(tidyverse)
library(scatterpie)
library(ggsci)
library(cowplot)
數(shù)據(jù)清洗
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)
構(gòu)建繪圖數(shù)據(jù)
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)