如何將柱狀圖和箱線圖畫一起
請(qǐng)大家查看原文
參考推文:http://www.itdecent.cn/p/308874fa1e6f
接下來我們就一步一步來畫:
我們知道用R畫圖最重要的兩點(diǎn):1.數(shù)據(jù)類型;2.數(shù)據(jù)結(jié)構(gòu)
數(shù)據(jù)預(yù)處理
首先,我們先來讀取數(shù)據(jù)
file1 = read.table('file1')
file2 = read.table('file2')

file1

file2
再來就是處理數(shù)據(jù):
#對(duì)file1按A,B的標(biāo)簽的組分離
c1 = which(file1$V3 == 'A')
c2 = which(file1$V3 == 'B')
file1_c1 = file1[c1,]
file1_c2 = file1[c2,]
按A,B分離

file1_c1

file1_c2
顯著性檢驗(yàn)
注意下下file1,file2是不同來源,分A,B組對(duì)其做t.text
#對(duì)file1,file2 分A,B組做t.test
t1 = t.test(rep(df2$V4[2],length(df1_g1$V4)), df1_g1$V4)$p.value
t2 = t.test(df1_g2$V4,rep(df2$V4[1],length(df1_g2$V4)))$p.value
做柱形圖的表
這里要加a來區(qū)分,稍后我們解密
bar = data.frame(
x = c(as.character(file2$V3[2]),as.character(file2$V3[1])),
y = c(file2$V4[2],file2$V4[1])
)
#先分A,B兩類,圖1
bar$x = c("B",'A')
#在加上a作為區(qū)分,圖2
bar$x = c("Ba",'Aa')

圖1

圖2
做箱線圖的表
box = rbind(file1_c1,file1_c2)
box = box[,-c(1,2)]
box = as.data.frame(box)
colnames(box) = c('x','y')
#先分A,B兩類,圖3
box$x = c(rep("B",5),rep('A',5))
#在加上c作為區(qū)分,圖4
box$x = c(rep("Bc",5),rep('Ac',5))

圖3

圖4
畫圖
ggplot()+
geom_bar(data = daf[1:2,],aes(x = x,y = y,fill = type),stat = 'identity',width = 0.9,alpha= 0.8)+
geom_bar(data = daf[13:14,],aes(x = x,y = y),stat = 'identity',width = 0.2)+
geom_boxplot(data = daf[3:12,],aes(x = x, y =y,fill = type),width = 0.9,alpha = 0.8)

image.png
我們看這幅圖的橫坐標(biāo)的映射,不難發(fā)現(xiàn)a標(biāo)簽是來自于file1 的數(shù)據(jù),c標(biāo)簽是來自于file2;A,B則是分的類
美化這個(gè)圖
#重新做表,圖5
bar_label= data.frame(
x = c('Bb','Ab'),
y = c(NA,NA)
)
daf = rbind(bar,box,bar_label)
daf$type = c('B','A',rep('B',5),rep('A',5),'B','A')
library(ggsci)
ggplot()+
geom_bar(data = daf[1:2,],aes(x = x,y = y,fill = type),stat = 'identity',width = 0.9,alpha= 0.8)+
geom_bar(data = daf[13:14,],aes(x = x,y = y),stat = 'identity',width = 0.2)+
geom_boxplot(data = daf[3:12,],aes(x = x, y =y,fill = type),width = 0.9,alpha = 0.8)+
scale_x_discrete(labels = c('','B','','','A',''))+ #修改x坐標(biāo)軸內(nèi)容
scale_fill_lancet()+ #修改顏色
theme_bw()+
theme(
axis.text = element_text(face = 'bold',color = 'black',size = 12),
axis.title = element_text(face = 'bold',color = 'black',size = 14, hjust = .5),
legend.text = element_text(face = 'bold',color = 'black',size = 12),
legend.title = element_blank(),
legend.position = 'bottom',
legend.direction = 'horizontal'
)+
xlab('Sample Name')+
ylab('Value')

圖5
引入兩個(gè)NA是為了在x軸上占個(gè)空位,即用Ab和Bb在X軸上占位:

占空位
最后效果如圖:

image.png
加上顯著性標(biāo)簽
#先做t.test
t1 = t.test(rep(file2$V4[2],length(file1_c1$V4)), file1_c1$V4)$p.value
t2 = t.test(file1_c2$V4,rep(file2$V4[1],length(file1_c2$V4)))$p.value
#做成data.frame,圖6
label_daf = data.frame(
x = c('Bb','Ab'),
y = c(13.5,13.5),
label = paste0('p.value = ', c(as.character(round(t1,3)),as.character(round(t2,3))))
)
#做網(wǎng)格線,圖7
seg_daf = data.frame(
x = c('Ba','Aa','Ba','Bc','Aa','Ac'),
xmax = c('Bc','Ac','Ba','Bc','Aa','Ac'),
y = c(13,13,12.5,12.5,12.5,12.5),
ymax = c(13,13,13,13,13,13)
)

圖6
file1和file2之間的顯著性,這個(gè)y是在圖上顯示的坐標(biāo)

圖7
見下圖:
x指的是網(wǎng)格線的豎線部分,也就是說x只映射于Aa,Ac,Ba和Bc這四個(gè)位置;y值橫線部分,數(shù)值是在圖上的位置

image.png
美化圖
ggplot()+
geom_bar(data = daf[1:2,],aes(x = x,y = y,fill = type),stat = 'identity',width = 0.9,alpha= 0.8)+
geom_bar(data = daf[13:14,],aes(x = x,y = y),stat = 'identity',width = 0.2)+
geom_boxplot(data = daf[3:12,],aes(x = x, y =y,fill = type),width = 0.9,alpha = 0.8)+
geom_segment(data = seg_daf,aes(x = x,y = y,xend = xmax,yend = ymax),color = 'black',size = 0.2)+
geom_text(data = label_daf,aes(x = x,y = y,label = label),fontface = 'bold',color = 'black',size = 5)+
scale_x_discrete(labels = c('','B','','','A',''))+
scale_fill_lancet()+
theme_bw()+
theme(
axis.text = element_text(face = 'bold',color = 'black',size = 12),
axis.title = element_text(face = 'bold',color = 'black',size = 14, hjust = .5),
legend.text = element_text(face = 'bold',color = 'black',size = 12),
legend.title = element_blank(),
legend.position = 'bottom',
legend.direction = 'horizontal'
)+
xlab('Sample Name')+
ylab('Value')
最終效果圖見下圖:

image.png