R-ggplot2-Barplot拼所有之拼Boxplot

作者: TroyShen | 来源:发表于2020-01-12 20:41 被阅读0次

目录

  • 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. 总结

本篇主要解决了以下几个问题:

  1. 如何将Barplot和Boxplot拼合在一幅图里?
  2. 如何将Barplot和Boxplot不重合地拼合在一幅图里?
  3. 如何做两组样本之间的T 检验?
  4. 如何在图中增加Label与指示线?

11. 本篇所使用的软件包(没有的需要用install.packages('')进行安装)

library(ggplot2)
library(reshape2)
library(ggsci)

12. 致谢

首先,感谢大家的持续关注,小编会继续努力,持续更新下去的!

大家如果觉得有帮助啊,还麻烦大家关注点赞,也可以扩散到朋友圈,多多引导朋友加入咱们这个简书技术平台, 代码共享推动科研进程, 多谢大家啦~

大家如果在使用本代码的过程有遇到问题的,可以留言评论,也可以私信我哈~~


小编联系方式

相关文章

  • R-ggplot2-Barplot拼所有之拼Boxplot

    目录 0.问题导入 1.示例数据 2.导入示例数据 3.数据预处理 4.样本T检验 5.指示线数据框制作 6.绘图...

  • 拼,拼,拼

    努力了虽不一定成功,但不努力注定失败。 人生就是一个不断学习的过程,活到老,学到老嘛。既然是学习,肯...

  • 拼多多风波不断,上市是危机还是契机?_创成汇

    今日将在纽交所上市的拼多多近期风波不断:为拼多多执行上市文件审计的会计事务所,在招股说明书中声明,拼多多存在重大财...

  • 再出发:ZT交易所盛夏再起航

    去年的交易所混战中我们可以看到,在市场行情不错的情况下,小交易所黑马出线可以拼让利、拼玩法、拼产品,但到了下半年,...

  • 拼文凭拼实力 不如拼父母 拼朋友拼同学 不如拼兄弟姐妹 拼岳父母拼老婆 不如拼情人 举目跳望 满世界都是拼的影子

  • 拼多多的前世今生

    拼多多走进大众视野,并且引起别人所重视,除了拼多多铺天盖地的广告,还在于拼多多与2018年7月26号在美国纳斯达克...

  • 遗失的纯真

    现在有条流行语,看脸的时代,感叹社会进步这么快,不拼爹了又开始拼颜值了。整容就是变美的捷径,爱美之心人皆有之,...

  • 想降价时,先试试“拼团”

    自从拼多多火了之后,拼团模式为大家所接受。【拼团】是促销中经常被用到的功能,由于其带有较强的社交营销效果,所以往往...

  • 拼的节奏

    这是个喜欢拼的年代,有的拼爹,有的拼妈,有的拼颜值,有的拼才华,有的拼勤奋,有的拼运气,玲琅满目的拼,最后的目的无...

  • 作为一个运营,如何才能写出亮眼的文案?

    想要文案写的好,还是要多学学拼多多! 之前分析过转化文案的套路,发现还是拼多多做的最好,没有之一,至少是在电商领域...

网友评论

    本文标题:R-ggplot2-Barplot拼所有之拼Boxplot

    本文链接:https://www.haomeiwen.com/subject/idjwactx.html