R-ggplot2-Barplot拼所有之拼Boxplot
2020-01-12 本文已影响0人
TroyShen
目录
- 0.问题导入
- 1.示例数据
- 2.导入示例数据
- 3.数据预处理
- 4.样本T检验
- 5.指示线数据框制作
- 6.绘图前数据框准备
- 7.Barplot 与Boxplot 拼合的第一次尝试(图1)
- 8.Barplot 与Boxplot 拼合时重叠问题解决(图2)
- 9.在图中增加Label与指示线(图3)
- 10.总结
- 11.本篇所使用的软件包(没有的需要用install.packages('')进行安装)
- 12.致谢
0. 问题导入
之前我们有推出:
1. R-ggplot2-并列的Barplot如何绘制Errorbar?如何将Barplot与lineplot通过双轴结合?小伙伴们看过来
2. R-ggplot2-如何将点图与密度统计图通过双坐标轴的方式结合在一起?本篇给出解决方案~
今天我们来个难度更大的,将Barplot与Boxplot评在一起~
那么,首先要解决的问题就是:这两种图重叠了,信息量都被盖住了,怎么看?
今天,我们就来解决这个问题,并实现Barplot与Boxplot的拼合。
1. 示例数据
感谢西农孙同学提供生物实验的示例数据(为啥要加生物两个字呢?现在不都流行破圈嘛,嘿嘿嘿):
数据来啦来啦,点我就对了!
2. 导入示例数据
setwd('L:\\JianShu\\20200112')
files = list.files()
df1 = read.table(files[1])
df2 = read.table(files[2])
3. 数据预处理
g1 = which(df1$V3 == 'G4-nG4')
g2 = which(df1$V3 == 'nG4-G4')
df1_g1 = df1[g1,]
df1_g2 = df1[g2,]
4. 样本T检验
根据T检验结果,可以发现被检验的样本服从于总体的假设不显著成立。
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
label_df = data.frame(
x = c('G4-nG4b','nG4-G4b'),
y = c(13.5,13.5),
label = paste0('p.value = ',
c(as.character(round(t1,3)),as.character(round(t2,3))))
)
label_df
x y label
1 G4-nG4b 13.5 p.value = 0.43
2 nG4-G4b 13.5 p.value = 0.541
5. 指示线数据框制作
seg_df = data.frame(
x = c('G4-nG4a','nG4-G4a','G4-nG4a','G4-nG4c','nG4-G4a','nG4-G4c'),
xmax = c('G4-nG4c','nG4-G4c','G4-nG4a','G4-nG4c','nG4-G4a','nG4-G4c'),
y = c(13,13,12.5,12.5,12.5,12.5),
ymax = c(13,13,13,13,13,13)
)
6. 绘图前数据框准备
df_bar = data.frame(
x = c(as.character(df2$V3[2]),as.character(df2$V3[1])),
y = c(df2$V4[2],df2$V4[1])
)
df_bar$x = c("G4-nG4",'nG4-G4')
df_box = rbind(df1_g1,df1_g2)
df_box = df_box[,-c(1,2)]
df_box = as.data.frame(df_box)
colnames(df_box) = c('x','y')
df_box$x = c(rep("G4-nG4",5),rep('nG4-G4',5))
df = rbind(df_bar,df_box)
7. Barplot 与Boxplot 拼合的第一次尝试(图1)
p1 = ggplot()+
geom_bar(data = df[1:2,],aes(x = x,y = y,fill = x),stat = 'identity',width = 0.5,alpha= 0.8)+
geom_boxplot(data = df[3:12,],aes(x = x, y =y,fill = x),width = 0.5,alpha = 0.8)+
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')
png('plot1.png',
height = 15,
width = 20,
units = 'cm',
res = 800)
print(p1)
dev.off()
图1
不明白篇头重叠的问题,图1就说明了一切,Barplot与Boxplot重叠了,Barplot的信息被挡的严严实实
所以,如何解决呢?接着往下看哈~
8. Barplot 与Boxplot 拼合时重叠问题解决(图2)
8.1 首先呀,我们要重新调整下绘图的Data.Frame。
df_bar$x = c("G4-nG4a",'nG4-G4a')
df_box = rbind(df1_g1,df1_g2)
df_box = df_box[,-c(1,2)]
df_box = as.data.frame(df_box)
colnames(df_box) = c('x','y')
df_box$x = c(rep("G4-nG4c",5),rep('nG4-G4c',5))
df_bar_label= data.frame(
x = c('G4-nG4b','nG4-G4b'),
y = c(NA,NA)
)
df = rbind(df_bar,df_box,df_bar_label)
df$type = c('G4-nG4','nG4-G4',rep('G4-nG4',5),rep('nG4-G4',5),'G4-nG4','G4-nG4')
8.2 Barplot 与Boxplot 拼合第二次尝试
p2 = ggplot()+
geom_bar(data = df[1:2,],aes(x = x,y = y,fill = type),stat = 'identity',width = 0.9,alpha= 0.8)+
geom_bar(data = df[13:14,],aes(x = x,y = y),stat = 'identity',width = 0.2)+
geom_boxplot(data = df[3:12,],aes(x = x, y =y,fill = type),width = 0.9,alpha = 0.8)+
scale_x_discrete(labels = c('','G4-nG4','',
'','nG4-G4',''))+
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')
png('plot2.png',
height = 15,
width = 20,
units = 'cm',
res = 800)
print(p2)
dev.off()
图2 Barplot 与Boxplot 拼合时重叠问题解决
9. 在图中增加Label与指示线(图3)
p3 = ggplot()+
geom_bar(data = df[1:2,],aes(x = x,y = y,fill = type),stat = 'identity',width = 0.9,alpha= 0.8)+
geom_bar(data = df[13:14,],aes(x = x,y = y),stat = 'identity',width = 0.2)+
geom_boxplot(data = df[3:12,],aes(x = x, y =y,fill = type),width = 0.9,alpha = 0.8)+
geom_segment(data = seg_df,aes(x = x,y = y,xend = xmax,yend = ymax),
color = 'black',size = 0.2)+
geom_text(data = label_df,aes(x = x,y = y,label = label),
fontface = 'bold',color = 'black',size = 5)+
scale_x_discrete(labels = c('','G4-nG4','',
'','nG4-G4',''))+
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')
png('plot3.png',
height = 15,
width = 20,
units = 'cm',
res = 800)
print(p3)
dev.off()
图3 在图中增加Label与指示线
10. 总结
本篇主要解决了以下几个问题:
- 如何将Barplot和Boxplot拼合在一幅图里?
- 如何将Barplot和Boxplot不重合地拼合在一幅图里?
- 如何做两组样本之间的T 检验?
- 如何在图中增加Label与指示线?
11. 本篇所使用的软件包(没有的需要用install.packages('')进行安装)
library(ggplot2)
library(reshape2)
library(ggsci)
12. 致谢
首先,感谢大家的持续关注,小编会继续努力,持续更新下去的!
大家如果觉得有帮助啊,还麻烦大家关注点赞,也可以扩散到朋友圈,多多引导朋友加入咱们这个简书技术平台, 代码共享推动科研进程, 多谢大家啦~
大家如果在使用本代码的过程有遇到问题的,可以留言评论,也可以私信我哈~~
小编联系方式