R-ggplot2-如何绘制对称条形图并添加旋转标签?

作者: TroyShen | 来源:发表于2020-02-06 18:30 被阅读0次

目录

  • 0.问题导入
  • 1.示例数据随机生成
  • 2.对称正负y空间展示双正样本
  • 3.如何使得y轴的标签均为正?
  • 4.如何增加垂直方向上的标注?
  • 5.再优化下
  • 6.总结
  • 7.本篇所用到的R-packages(没有的需要用install.packages('包名')进行安装)
  • 8.参考文献
  • 9.致谢
  • 10.号外:技术公众号【TheWhoOPs】上线啦~~

0.问题导入

在日常条形图绘制过程中,为了突出对比两个样本并便于标注值的大小,我们需要将两个赋值均为正的样本分别在y值的正负空间进行展示与标注,如图1。


问题说明(Li et al., 2019)

那么问题来了:
1. 如何将两组正值分别在y轴的正负空间进行分别展示?
2. 如何使得y轴的标签均为正?
3. 如何增加垂直方向上的标注?

本篇将逐一解决以上问题

1. 示例数据随机生成

setwd('/Users/jerseyshen/Documents/JianShu_Project/20200206')

df = data.frame( 
  Group = rep(c('Group_A','Group_B','Group_C','Group_D'),2),
  Frequency = c(runif(4,0,100),runif(4,-100,0)),
  Type = rep(c('TypeA','TypeB'),each = 4)
)
df$Label = paste0(round(df$Frequency,2),"%")
head(df)
 Group  Frequency  Type   Label
1 Group_A  27.415620 TypeA  27.42%
2 Group_B  39.089917 TypeA  39.09%
3 Group_C  17.649449 TypeA  17.65%
4 Group_D  68.757908 TypeA  68.76%
5 Group_A -26.046330 TypeB -26.05%
6 Group_B  -3.421074 TypeB  -3.42%

2. 对称正负y空间展示双正样本

实现这一步的思路是将需要展示在y轴负空间的值赋予相反值,本篇示例数据df已将TypeB的值进行了求负处理(图2)。但是问题是我们需要展示的样本真值为正,如果我们把图2直接插入到论文中,肯定会导致读者误认为TypeB值为负的问题。因此,接下来,我们需要将y轴的轴标签进行均正处理。

p1 = ggplot()+
  geom_bar(data = df,aes(x = Group,y = Frequency,fill = Type),
           color = 'black',stat = 'identity',position = 'stack')+
  theme_bw()+
  theme(legend.position = 'bottom',
        legend.direction = 'horizontal',
        axis.text = element_text(size = 12,hjust = 0.5,face = 'bold',color = 'black'),
        axis.title = element_text(size = 14,hjust = 0.5,face = 'bold',color = 'black'),
        legend.text = element_text(size = 12,hjust = 0.5,face = 'bold',color = 'black'),
        legend.title = element_text(size = 14,hjust = 0.5,face = 'bold',color = 'black'))+
  ylab('Frequency (%)')

png('plot1.png',
    height = 15,
    width = 20,
    units = 'cm',
    res = 800)
print(p1)
dev.off()
图2 对称正负y空间展示双正样本

3. 如何使得y轴的标签均为正?

p2 = ggplot()+
  geom_bar(data = df,aes(x = Group,y = Frequency,fill = Type),
           color = 'black',stat = 'identity',position = 'stack')+
  scale_y_continuous(limits = c(-100,100),breaks = c(-100,-50,0,50,100),labels = c(100,50,0,50,100))+
  theme_bw()+
  theme(legend.position = 'bottom',
        legend.direction = 'horizontal',
        axis.text = element_text(size = 12,hjust = 0.5,face = 'bold',color = 'black'),
        axis.title = element_text(size = 14,hjust = 0.5,face = 'bold',color = 'black'),
        legend.text = element_text(size = 12,hjust = 0.5,face = 'bold',color = 'black'),
        legend.title = element_text(size = 14,hjust = 0.5,face = 'bold',color = 'black'))+
  ylab('Frequency (%)')

png('plot2.png',
    height = 15,
    width = 20,
    units = 'cm',
    res = 800)
print(p2)
dev.off()
图3 如何使得y轴的标签均为正?

4. 如何增加垂直方向上的标注?

p3 = ggplot()+
  geom_bar(data = df,aes(x = Group,y = Frequency,fill = Type),
           color = 'black',stat = 'identity',position = 'stack')+
  geom_text(data = df,aes(x = Group, y= Frequency, label = Label,color = Type),
            angle = 90,size = 5,nudge_y =c(rep(20,4),rep(-20,4)))+
  scale_y_continuous(limits = c(-100,100),breaks = c(-100,-50,0,50,100),labels = c(100,50,0,50,100))+
  theme_bw()+
  theme(legend.position = 'bottom',
        legend.direction = 'horizontal',
        axis.text = element_text(size = 12,hjust = 0.5,face = 'bold',color = 'black'),
        axis.title = element_text(size = 14,hjust = 0.5,face = 'bold',color = 'black'),
        legend.text = element_text(size = 12,hjust = 0.5,face = 'bold',color = 'black'),
        legend.title = element_text(size = 14,hjust = 0.5,face = 'bold',color = 'black'))+
  ylab('Frequency (%)')

png('plot3.png',
    height = 15,
    width = 20,
    units = 'cm',
    res = 800)
print(p3)
dev.off()
图4 如何增加垂直方向上的标注?

5. 再优化下

疫情期间用Lancet 色带组祈福!

p4 = ggplot()+
  geom_bar(data = df,aes(x = Group,y = Frequency,fill = Type),
           color = 'black',stat = 'identity',position = 'stack',width = 0.4)+
  geom_text(data = df,aes(x = Group, y= Frequency, label = Label,color = Type),
            angle = 90,size = 5,nudge_y =c(rep(20,4),rep(-20,4)))+
  scale_y_continuous(limits = c(-100,100),breaks = c(-100,-50,0,50,100),labels = c(100,50,0,50,100))+
  scale_fill_lancet()+
  scale_color_lancet()+
  geom_hline(aes(yintercept = 0),size = 0.2)+
  theme_bw()+
  theme(legend.position = 'bottom',
        legend.direction = 'horizontal',
        axis.text = element_text(size = 12,hjust = 0.5,face = 'bold',color = 'black'),
        axis.title = element_text(size = 14,hjust = 0.5,face = 'bold',color = 'black'),
        legend.text = element_text(size = 12,hjust = 0.5,face = 'bold',color = 'black'),
        legend.title = element_text(size = 14,hjust = 0.5,face = 'bold',color = 'black'),
        panel.grid = element_line(color = 'transparent'))+
  ylab('Frequency (%)')

png('plot4.png',
    height = 15,
    width = 20,
    units = 'cm',
    res = 800)
print(p4)
dev.off()
图4 优化后的图件

6. 总结

本篇共解决了以下几个问题:
1. 如何将两组正值分别在y轴的正负空间进行分别展示?
2. 如何使得y轴的标签均为正?
3. 如何增加垂直方向上的标注?

7. 本篇所用到的R-packages(没有的需要用install.packages('包名')进行安装)

library(ggplot2)
library(ggsci)

8. 参考文献

Li, J., Wu, C., Wang, X., Peng, J., Dong, D., Lin, G., & Gonsamo, A. (2020). Satellite observed indicators of the maximum plant growth potential and their responses to drought over Tibetan Plateau (1982–2015). Ecological Indicators, 108, 105732.

9. 致谢

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

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

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

祝大家身体健康!!

小编联系方式

10. 号外:技术公众号【TheWhoOPs】上线啦~~

为了方便大家的阅读方便,技术公众号【TheWhoOPs】已经上线啦!!欢迎大家关注及转发哈,感谢大家鼎力支持!!


相关文章

网友评论

    本文标题:R-ggplot2-如何绘制对称条形图并添加旋转标签?

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