跟着Nature学作图:R语言ggplot2簇状柱形图完整示例
2023-02-19 12:27:42 时间
论文
A global reptile assessment highlights shared conservation needs of tetrapods
https://www.nature.com/articles/s41586-022-04664-7#Sec33
数据代码链接
https://github.com/j-marin/Global-reptile-assessment-
今天的推文学习一下推文中的Figure 3的簇状柱形图,没有找到论文中的作图代码,但是找到了原始数据集,有了原始数据集就可以自己写代码来做这个图
image.png
部分示例数据集
image.png
加载需要用到的R包
library(readxl)
library(ggplot2)
library(tidyverse)
library(patchwork)
Figure 3a
dat01<-read_excel("data/20220630/41586_2022_4664_MOESM4_ESM.xlsx",
sheet = "Fig 3a")
head(dat01)
dim(dat01)
dat01$Threat<-factor(dat01$Threat,
levels = dat01$Threat %>% unique())
ggplot(data=dat01,aes(x=Threat,y=n,fill=className))+
geom_bar(stat="identity",position = "dodge")+
theme_classic()+
geom_vline(xintercept = 5.5,lty="dashed")+
geom_vline(xintercept = 9.5,lty="dashed")+
annotate(geom = "text",x=2.5,y=0.9,label="Habitat destruction")+
annotate(geom = "text",x=7.5,y=0.9,label="Habitat change")+
annotate(geom = "text",x=11,y=0.9,label="Other")+
theme(legend.position = "bottom",
axis.text.x = element_text(angle=60,hjust = 1,vjust = 1),
legend.title = element_blank())+
labs(x=NULL,y="Species threatened (%)")+
scale_fill_manual(values = c("#936eaa","#401f51",
"#5f6798","#de6eaa"))+
scale_y_continuous(labels = function(x){x*100}) -> p1
p1
image.png
Figure 3b
和Figure 3a是一样的,唯一的区别是配色不一样
dat02<-read_excel("data/20220630/41586_2022_4664_MOESM4_ESM.xlsx",
sheet = "Fig 3b")
head(dat02)
dim(dat02)
dat02$Threat<-factor(dat02$Threat,
levels = dat02$Threat %>% unique())
ggplot(data=dat02,aes(x=Threat,y=n,fill=className))+
geom_bar(stat="identity",position = "dodge")+
theme_classic()+
geom_vline(xintercept = 5.5,lty="dashed")+
geom_vline(xintercept = 9.5,lty="dashed")+
annotate(geom = "text",x=2.5,y=0.9,label="Habitat destruction")+
annotate(geom = "text",x=7.5,y=0.9,label="Habitat change")+
annotate(geom = "text",x=11,y=0.9,label="Other")+
theme(legend.position = "bottom",
axis.text.x = element_text(angle=60,hjust = 1,vjust = 1),
legend.title = element_blank())+
labs(x=NULL,y="Species threatened (%)")+
scale_fill_manual(values = c("#4868af","#e41f24",
"#edb91d","#973692"))+
scale_y_continuous(labels = function(x){x*100}) -> p2
p2
image.png
最后是拼图
p1/p2 + plot_annotation(tag_levels = "a")
image.png
image.png
论文中的figure4也是簇状柱形图,感兴趣的可以自己试着复现一下
示例数据和代码可以自己到论文中获取
相关文章
- Jgit的使用笔记
- 利用Github Action实现Tornadofx/JavaFx打包
- 叹息!GitHub Trending 即将成为历史!
- 微软软了?开源社区讨论炸锅,GitHub CEO 亲自来答
- GitHub Trending 列表频现重复项,前后端都没去重?
- Photoshop Elements 2021版本软件安装教程(mac+windows全版本都有)
- (ps全版本)Photoshop 2020的安装与破解教程(mac+windows全版本都有)
- (ps全版本)Photoshop cc2018的安装与破解教程(mac+windows全版本,包括2023
- 环境搭建:Oracle GoldenGate 大数据迁移到 Redshift/Flat file/Flume/Kafka测试流程
- 每个开发人员都要掌握的:最小 Linux 基础课
- 来撸羊毛了!Windows 环境下 Hexo 博客搭建,并部署到 GitHub Pages
- 超实用!手把手入门 MongoDB:这些坑点请一定远离
- 【GitHub日报】22-10-09 zustand、neovim、webtorrent、express 等4款App今日上新
- 【GitHub日报】22-10-10 brew、minio、vite、seaweedfs、dbeaver 等8款App今日上新
- 【GitHub日报】22-10-11 cobra、grafana、vue、ToolJet、redwood 等13款App今日上新
- Photoshop 2018 下载及安装教程(mac+windows全版本都有,包括最新的2023)
- Photoshop 2017 下载及安装教程(mac+windows全版本都有,包括最新的2023)
- Photoshop 2020 下载及安装教程(mac+windows全版本都有,包括最新的2023)
- Photoshop 2023 资源免费下载(mac+windows全版本都有,包括最新的2023)
- 最新版本Photoshop CC2018软件安装教程(mac+windows全版本都有,包括2023