我想用R画一个bar图

本文介绍了如果用拼拼凑凑的方法画出一个想要的bar图。可能并不成体系,但只要能达到目的方法,就是好方法。

0. 准备工作

  1. 加载需要的工具包。
library(ggpubr)
library(ggplot2)
  1. 加载并查看数据
    以ToothGrowth数据为例
    The response is the length of odontoblasts (cells responsible for tooth growth) in 60 guinea pigs. Each animal received one of three dose levels of vitamin C (0.5, 1, and 2 mg/day) by one of two delivery methods, orange juice or ascorbic acid (a form of vitamin C and coded as VC).
df_demo <- ToothGrowth
head(df_demo)

1. 我想要画一个简单的bar图

df_demo %>% 
  ggbarplot(x = 'dose', y = 'len',
            color = 'supp',
            add = 'mean_se', 
            position = position_dodge(0.8),
            xlab = 'Dose(mg/day)',
            ylab = 'Length',
            title = 'The length of odontoblasts'
            )
bar1.jpg

2. 我想加上每个点的数据

df_demo %>% 
  ggbarplot(x = 'dose', y = 'len',
            color = 'supp',
            add = c('mean_se', 'jitter'), # 加上jitter
            add.params =  list(alpha = 0.7, size = 0.8), # 调整点的参数,alpha为透明度,size为大小
            position = position_dodge(0.8),
            xlab = 'Dose(mg/day)',
            ylab = 'Length',
            title = 'The length of odontoblasts'
            )

bar2.jpg

3. 我想将bar图改为填充色并且加上jitter点

如果只是想单纯地改成填充,那只需要将color = 'supp'改为fill='supp',或者加上这一句就好了。但这样更改的话,add = 'jitter'会出错。我也不知道为什么。所以,如果想要将bar图改为填充色,并且还想保留jitter点的话,可以用geom_jitter

df_demo %>% 
  ggbarplot(x = 'dose', y = 'len',
            fill = 'supp',
            add = c('mean_se'), 
            add.params =  list(alpha = 0.7, size = 0.8), # 调整点的参数,alpha为透明度,size为大小
            position = position_dodge(0.8),
            xlab = 'Dose(mg/day)',
            ylab = 'Length',
            )+
  geom_jitter(aes(dose, len, fill = supp), #对应前面的x,y,fill
              shape = 16, # 从1~25可以更改不同的形状
              size = 0.9, 
              position = position_jitterdodge(jitter.width = 0.3)
              )
  
bar3.jpg

4. 我想修改图例的颜色和名称

df_demo %>% 
  ggbarplot(x = 'dose', y = 'len',
            fill = 'supp',
            add = c('mean_se'), 
            add.params =  list(alpha = 0.7, size = 0.8), # 调整点的参数,alpha为透明度,size为大小
            position = position_dodge(0.8),
            xlab = 'Dose(mg/day)',
            ylab = 'Length',
            )+
  geom_jitter(aes(dose, len, fill = supp), #对应前面的x,y,fill
              shape = 16, # 从1~25可以更改不同的形状
              size = 0.9, 
              position = position_jitterdodge(jitter.width = 0.3)
              )+
  scale_fill_manual(name = 'Supp',  # 如果前面是color就用scale_color_manual
                    label = c('Orange Juice','vitamin C'),
                    values = c('#0073c299','#efc00099'))

bar4.jpg

5. 我想修改x轴y轴字体/刻度等

df_demo %>% 
  ggbarplot(x = 'dose', y = 'len',
            fill = 'supp',
            add = c('mean_se'), 
            add.params =  list(alpha = 0.7, size = 0.8), # 调整点的参数,alpha为透明度,size为大小
            position = position_dodge(0.8),
            xlab = 'Dose(mg/day)',
            ylab = 'Length',
            )+
  geom_jitter(aes(dose, len, fill = supp), #对应前面的x,y,fill
              shape = 16, # 从1~25可以更改不同的形状
              size = 0.9, 
              position = position_jitterdodge(jitter.width = 0.3)
              )+
  scale_fill_manual(name = 'Supp',  # 如果前面是color就用scale_color_manual
                    label = c('Orange Juice','vitamin C'),
                    values = c('#0073c299','#efc00099'))+
  font('xylab', face = 'bold', size = 10)+ #对x轴y轴名称进行加粗,也可用xlab或ylab进行单独处理
    theme(axis.text.y = element_blank(), # y轴的text
        axis.ticks.y = element_blank(), # y轴的刻度
        axis.line.y =  element_blank()) # y轴的轴
bar5.jpg

6. 我想加上显著性星标

这里的显著性是需要自己先做统计分析才得到的结果

df_demo %>% 
  ggbarplot(x = 'dose', y = 'len',
            fill = 'supp',
            add = c('mean_se'), 
            add.params =  list(alpha = 0.7, size = 0.8), # 调整点的参数,alpha为透明度,size为大小
            position = position_dodge(0.8),
            xlab = 'Dose(mg/day)',
            ylab = 'Length',
            )+
  geom_jitter(aes(dose, len, fill = supp), #对应前面的x,y,fill
              shape = 16, # 从1~25可以更改不同的形状
              size = 0.9, 
              position = position_jitterdodge(jitter.width = 0.3)
              )+
  scale_fill_manual(name = 'Supp',  # 如果前面是color就用scale_color_manual
                    label = c('Orange Juice','vitamin C'),
                    values = c('#0073c299','#efc00099'))+
  font('xylab', face = 'bold', size = 13)+ #对x轴y轴名称进行加粗,也可用xlab或ylab进行单独处理
  geom_signif(y_position=c(35), # y_position是误差线所在y轴位置
              xmin=c(0.8, 1.8),
              xmax=c(1.2, 2.2),# xmin和xmax是误差线在x轴位置,可传入多个值
              annotation=c("***",'**'), # 显著性标识,可自定义输入
              tip_length=0.02, # 显著性括号下延长度
              size=0.6, # 线的大小
              textsize = 7, # 星标大小
              vjust = 0.5 #星标与线的距离
              )+
    geom_signif(y_position=c(35),
              xmin=c(2.8), xmax=c(3.2),#
              annotation=c('ns'), # 自定义输入
              tip_length=0.02,
              size=0.6, # 线的大小
              textsize = 4.5, # 此处为ns,是字母,所以字体大小需要重新设置
              vjust = 0.01 #重新设置
              ) 
bar6.jpg

7. 我想分面展示

分面(facet)展示其实更适合三个条件以上的,这里的示例数据只有两个条件。所以我这里认为加上一个gender用于演示。


df_demo %>% 
  mutate(gender = rep(c('f','m'),30)) %>% 
  ggbarplot(x = 'dose', y = 'len',
            fill = 'supp',
            add = c('mean_se'), 
            add.params =  list(alpha = 0.7, size = 0.8), # 调整点的参数,alpha为透明度,size为大小
            position = position_dodge(0.8),
            facet.by = 'gender', # 根据gender分面
            panel.labs = list(gender = c('Female','Male')), # 每个panel的名称
            xlab = 'Dose(mg/day)',
            ylab = 'Length',
            )+
  geom_jitter(aes(dose, len, fill = supp), #对应前面的x,y,fill
              shape = 16, # 从1~25可以更改不同的形状
              size = 0.9, 
              position = position_jitterdodge(jitter.width = 0.3)
              )+
  scale_fill_manual(name = 'Supp',  # 如果前面是color就用scale_color_manual
                    label = c('Orange Juice','vitamin C'),
                    values = c('#0073c299','#efc00099'))+
  font('xylab', face = 'bold', size = 13) #对x轴y轴名称进行加粗,也可用xlab或ylab进行单独处理

bar7.jpg

8. 我想分面展示并加上不同的显著性星标

使用facet.by去分面展示数据之后,如果直接用geom_signif去加显著性星标的画,R会平等地对待每一个panel,也就是说,每个panel的星标是一样的。但现实情况是,每个panel内条件间的差异是不一样的,这也是我们想要分面展示数据的原因。那要怎么给每个panel加上不同的显著性星标呢。答案是,我也不知道。但是我可以用一些曲线救图的方法来实现。也就是,分开画图,然后拼在一起。

# 先画第一个面
p_female <- df_demo %>% 
  mutate(gender = rep(c('f','m'),30)) %>% 
  filter(gender == 'f') %>%
   ggbarplot(x = 'dose', y = 'len',
            fill = 'supp',
            add = c('mean_se'), 
            add.params =  list(alpha = 0.7, size = 0.8), # 调整点的参数,alpha为透明度,size为大小
            position = position_dodge(0.8),
            facet.by = 'gender', # 根据gender分面
            panel.labs = list(gender = c('Female')), # panel的名称
            xlab = 'Dose(mg/day)',
            ylab = 'Length',
            )+
  geom_jitter(aes(dose, len, fill = supp), #对应前面的x,y,fill
              shape = 16, # 从1~25可以更改不同的形状
              size = 0.9, 
              position = position_jitterdodge(jitter.width = 0.3)
              )+
  scale_fill_manual(name = 'Supp',  # 如果前面是color就用scale_color_manual
                    label = c('Orange Juice','vitamin C'),
                    values = c('#0073c299','#efc00099'))+
  font('xylab', face = 'bold', size = 13)+ #对x轴y轴名称进行加粗,也可用xlab或ylab进行单独处理
  theme_pubr(margin = T)+
    geom_signif(y_position=c(30), # y_position是误差线所在y轴位置
              xmin=c(0.8, 1.8),
              xmax=c(1.2, 2.2),# xmin和xmax是误差线在x轴位置,可传入多个值
              annotation=c("***",'*'), # 显著性标识,可自定义输入
              tip_length=0.02, # 显著性括号下延长度
              size=0.6, # 线的大小
              textsize = 7, # 星标大小
              vjust = 0.5 #星标与线的距离
              )

# 再画第二个面,然后实现y轴消失术

p_male <- df_demo %>% 
  mutate(gender = rep(c('f','m'),30)) %>% 
  filter(gender == 'm') %>%
   ggbarplot(x = 'dose', y = 'len',
            fill = 'supp',
            add = c('mean_se'), 
            add.params =  list(alpha = 0.7, size = 0.8), # 调整点的参数,alpha为透明度,size为大小
            position = position_dodge(0.8),
            facet.by = 'gender', # 根据gender分面
            panel.labs = list(gender = c('Male')), # panel的名称
            xlab = 'Dose(mg/day)',
            ylab = ' ', # 这里把y轴名称也取消
            )+
  geom_jitter(aes(dose, len, fill = supp), #对应前面的x,y,fill
              shape = 16, # 从1~25可以更改不同的形状
              size = 0.9, 
              position = position_jitterdodge(jitter.width = 0.3)
              )+
  scale_fill_manual(name = 'Supp',  # 如果前面是color就用scale_color_manual
                    label = c('Orange Juice','vitamin C'),
                    values = c('#0073c299','#efc00099'))+
  font('xylab', face = 'bold', size = 13)+ #对x轴y轴名称进行加粗,也可用xlab或ylab进行单独处理
  theme_pubr(margin = T)+
    geom_signif(y_position=c(30), # y_position是误差线所在y轴位置
              xmin=c(0.8, 1.8),
              xmax=c(1.2, 2.2),# xmin和xmax是误差线在x轴位置,可传入多个值
              annotation=c("*",'**'), # 显著性标识,可自定义输入
              tip_length=0.02, # 显著性括号下延长度
              size=0.6, # 线的大小
              textsize = 7, # 星标大小
              vjust = 0.5 #星标与线的距离
              )+
    theme(axis.text.y = element_blank(),  # y轴消失术
        axis.ticks.y = element_blank(),
        axis.line.y =  element_blank())

# 把这两张图拼起来
ggarrange(p_female, p_male, ncol = 2, nrow = 1,common.legend = T)

bar8.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容