但是今天學(xué)習(xí)的雙向條形圖,是背靠背,中間共享同一個Y軸的坐標(biāo)軸標(biāo)簽 !圖來自的文獻(xiàn)于2024年7月10號發(fā)表在Nature雜志 ,標(biāo)題為《Tumour vasculature at single-cell resolution》 。
圖中描述了不同血管壁細(xì)胞(MCs)亞群在不同癌癥類型中與患者的生存率(包括總生存期和無進(jìn)展生存期)之間的相關(guān)性。 具體來說,它統(tǒng)計了在TCGA數(shù)據(jù)庫中,有多少種癌癥類型顯示出血管壁細(xì)胞與這些生存指標(biāo)之間存在明顯的相關(guān)性。這表明血管壁細(xì)胞可能在多種癌癥的進(jìn)展和患者預(yù)后中扮演重要角色。
圖注:
Fig. 4 | Characterization of pan-tumour MCs. h, The number of cancer types in which MCs exhibited a clear correlation with overall survival and progression-free survival in the TCGA cohorts.
示例數(shù)據(jù) 我這里根據(jù)圖片展示的結(jié)果構(gòu)造了一個數(shù)據(jù),在這里下載:鏈接: https://pan.baidu.com/s/1ohZhtShTJEbM6OV-WH46jQ?pwd=xfe3
rm(list=ls()) library(ggplot2) library(reshape2) data <- read.csv( "data.csv" ) head(data) ## 寬變長 # id.vars:指定不需要被轉(zhuǎn)換的列,這里是 "細(xì)胞類型"。 # variable.name:指定新生成的列名,用于存儲原始列名,這里是 "指標(biāo)"。 # value.name:指定新生成的列名,用于存儲原始列的值,這里是 "值"。 df <- melt(data, id.vars = c( "celltype" , "type" ), variable.name = "time" , value.name = "count" ) head(df) # celltype type time count # 1 BASP1+ matPC Risk PFS_cancerNum 19 # 2 BASP1+ matPC Protective PFS_cancerNum 0 # 3 adipPC Risk PFS_cancerNum 2 # 4 adipPC Protective PFS_cancerNum 9 # 5 SMC Risk PFS_cancerNum 2 # 6 SMC Protective PFS_cancerNum 5 # 轉(zhuǎn)換為因子固定繪圖順序: unique(df $celltype ) df $type <- factor(df $type , levels = c( "Protective" , "Risk" )) df $celltype <- factor(df $celltype , levels = rev(c( "BASP1+ matPC" , "adipPC" , "SMC" , "vdPC" , "myoPC" , "Inter.matPC" , "matPC" ))) str(df) # 摳出來文章中的配色 mycol <- c( "Risk" = "#b33b44" , "Protective" = "#4f93b8" ) mycol
數(shù)據(jù)都準(zhǔn)備好了,現(xiàn)在開始繪圖!
繪圖 這個圖是左右兩部分拼接而成,拆成下面三部分的過程 。
1.先繪制右邊: ## 繪圖 # 右側(cè)堆積柱狀圖 df1 <- df[df $time == "OS_cancerNum" , ] p1 <- ggplot(df1, aes(x = count, y = celltype, fill = type )) + geom_bar(position = "stack" , stat = "identity" , alpha = 1,color= "black" ) + xlab( "" ) + scale_fill_manual(values = mycol) + # 修改配色 scale_x_continuous(expand = c(0,0)) + # 柱子貼坐標(biāo)軸 ggtitle( "OS in TCGA" ) + theme_classic() + theme( # legend.position = "none", # 去掉圖例 title = element_text(face = "bold" ,size=16), axis.line = element_line(color = "black" , linewidth = 0.9), # 設(shè)置坐標(biāo)軸線的顏色和粗細(xì) axis.title.y = element_blank(),, # 去掉y軸標(biāo)題 axis.text.x = element_text(face = "bold" ,size=16), # x軸刻度標(biāo)簽加粗 axis.text.y = element_text(face = "bold" ,size=16,hjust = 0.5,color= "black" ), # y軸刻度標(biāo)簽加粗 axis.ticks = element_line(color = "black" , size = 1.1), # 設(shè)置刻度線的顏色和粗細(xì) axis.ticks.length = unit(0.2, "cm" ) # 設(shè)置刻度線長度 ) p1
結(jié)果如下:
2.畫左邊 接著是左邊,不需要y軸坐標(biāo)標(biāo)簽,并且旋轉(zhuǎn)坐標(biāo)軸:
# 左側(cè)堆積柱狀圖 df2 <- df[df $time == "PFS_cancerNum" , ] p2 <- ggplot(df2, aes(x = count, y = celltype, fill = type )) + geom_bar(position = "stack" , stat = "identity" , alpha = 1,color= "black" ) + scale_y_discrete(position = "right" ) + # 將Y軸放到左側(cè) scale_x_reverse(expand = c(0,0)) + # 數(shù)值也需要同時逆轉(zhuǎn) xlab( "" ) + scale_fill_manual(values = mycol) + # 修改配色 ggtitle( "PFS in TCGA" ) + theme_classic() + theme( # legend.position = "none", # 去掉圖例 title = element_text(face = "bold" ,size=16,hjust = 0), axis.line = element_line(color = "black" , linewidth = 0.9), # 設(shè)置坐標(biāo)軸線的顏色和粗細(xì) axis.title.y = element_blank(), axis.text.x = element_text(face = "bold" ,size=16), # x軸刻度標(biāo)簽加粗 axis.text.y = element_blank(), # 去掉y軸標(biāo)簽 axis.ticks = element_line(color = "black" , size = 1.1), # 設(shè)置刻度線的顏色和粗細(xì) axis.ticks.length = unit(0.2, "cm" ) # 設(shè)置刻度線長度 ) p2
結(jié)果如下:
3.拼接 然后將兩部分拼接在一起,并共用一個圖例 :
## #拼圖: library(patchwork) p <- (p2 | p1) + plot_layout(guides = 'collect' ) & theme( legend.justification = c( "right" , "bottom" ), legend.text = element_text(face = "bold" ,size=15), # 圖例標(biāo)簽加粗 legend.title = element_blank() # 移除圖例標(biāo)題 ) p
結(jié)果如下:
每周一畫,勤修苦練~ 文末友情宣傳