跟着Nature Communications学作图:R语言ggplot2堆积柱形图组合哑铃图

分享
藏宝库编辑 2024-9-30 09:46:23 6 0 来自 中国
论文

A latitudinal gradient of deep-sea invasions for marine fishes
https://www.nature.com/articles/s41467-023-36501-4
s41467-023-36501-4.pdf
论文中对应的图实现的代码都有,链接是
https://github.com/stfriedman/Depth-transitions-paper
本日的推文我们重复一下论文中的figure1A,此中一个堆积柱形图和一个哑铃图,哑铃图就是点和线段的组合
起首是右侧哑铃图

部分示例数据截图
有一些分组数据论文中没有提供,这部分数据我就任意构造了,终极的出图不会和论文中完全划一
加载用到的R包
library(readxl)library(tidyverse)library(wesanderson)读取数据
fig1_data<-read_excel("data/20230302/41467_2023_36501_MOESM7_ESM.xlsx")fig1_data %>% dim()给数据集添加两列分组
fig1_data %>%   mutate(cat=sample(c("small","norm","big"),46,replace = TRUE),         col=sample(c("Above expectation",                      "Within expectation",                      "Below expctation"),                    46,replace = TRUE)) -> fig1_data作图代码
ggplot(fig1_data, aes(x = family, y = trans_num)) +  geom_segment(aes(x = family, xend = family, y = sim_0.05, yend = sim_0.95),               col = "grey90", lwd = 3, lineend = "round"  ) +  geom_segment(aes(x = family, xend = family, y = sim_median, yend = trans_num),               col = "grey50", lwd = 0.6  ) +  geom_point(aes(x = family, y = sim_median),             pch = 21,             fill = "white", col = "grey50", size = 3, stroke = 1  ) +  geom_point(size = 3.3,aes(color=col)) +  theme_classic() +  theme(    axis.text.y = element_blank(),    plot.margin = unit(c(0, 0.4, 0, 0), "cm"),    panel.grid.major.x = element_blank(),    panel.border = element_blank(),    panel.grid.major.y = element_line(size = 0.4),    axis.line.x = element_line(size = 0.2),    axis.line.y = element_blank(),    axis.ticks.x = element_line(size = 0.1),    axis.ticks.y = element_blank(),    legend.position = c(0.8,0.1),    legend.background = element_rect(color="black",fill="transparent"),    legend.title = element_blank(),    axis.title = element_text(size = 12),    axis.title.y = element_blank()  ) +  coord_flip() +  ylab("Number of Transitions") +  labs(col = "Speciation Rate") +  scale_color_manual(values = c(    wes_palette("Darjeeling1")[2], "grey40",    wes_palette("Darjeeling1")[3]  )) -> p1p1堆积柱形图

这个数据论文中没有提供,这里我们任意构造数据
df1<-data.frame(x=rep(fig1_data$family,3),                y1=c(rep(c("Shallow","Deep","Intermediate"),each=46)),                y2=sample(1:100,46*3,replace = TRUE))df1 %>% head()预备颜色
depth_cols <- setNames(  c("powderblue", "#2C8EB5", "#16465B"),  c("Shallow", "Intermediate", "Deep"))作图代码
ggplot(data=df1,       aes(y = y2, x = x)) +  geom_bar(position = "fill", stat = "identity", width = 0.7,           aes(fill=y1)) +  coord_flip() +  theme_classic() +  theme(    #axis.text.y = element_text(colour = famcol),    axis.text.x = element_blank(),    axis.ticks = element_blank(),    axis.line = element_blank(),    legend.position = "bottom",    legend.justification = c(0,0),    legend.key.size = unit(2,'mm'),    legend.title = element_blank(),    legend.background = element_rect(color="black",fill="transparent")  ) +  xlab("") +  ylab("") +  guides(fill=guide_legend(ncol = 2))+  scale_fill_manual(values = depth_cols) -> p2p2末了是拼图

library(patchwork)p2+p1+  plot_layout(widths = c(1,5))
示例数据和代码可以给推文点赞,然后点击在看,末了留言获取
接待各人关注我的公众号
小明的数据分析笔记本
小明的数据分析笔记本 公众号 紧张分享:1、R语言和python做数据分析和数据可视化的简朴小例子;2、园艺植物相干转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!
微信公众号好像又有改动,假如没有将这个公众号设为星标的话,会经常错过公众号的推文,个人发起将 小明的数据分析笔记本 公众号添加星标,添加方法是
点开公众号的页面,右上角有三个点

点击三个点,会跳出界面

直接点击 设为星标 就可以了

7.png
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 18:16, Processed in 0.178134 second(s), 35 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表