bioinformaticsR可视化R plot

跟着Nature Communications学作图:R语言gg

2023-03-05  本文已影响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,其中一个堆积柱形图和一个哑铃图,哑铃图就是点和线段的组合

首先是右侧哑铃图

部分示例数据截图

image.png

有一些分组数据论文中没有提供,这部分数据我就随便构造了,最终的出图不会和论文中完全一致

加载用到的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]
  )) -> p1
p1
image.png

堆积柱形图

这个数据论文中没有提供,这里我们随便构造数据

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) -> p2

p2
image.png

最后是拼图

library(patchwork)

p2+p1+
  plot_layout(widths = c(1,5))
image.png

示例数据和代码可以给推文点赞,然后点击在看,最后留言获取

欢迎大家关注我的公众号

小明的数据分析笔记本

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!

微信公众号好像又有改动,如果没有将这个公众号设为星标的话,会经常错过公众号的推文,个人建议将 小明的数据分析笔记本 公众号添加星标,添加方法是

点开公众号的页面,右上角有三个点

image.png

点击三个点,会跳出界面

image.png

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

image.png
上一篇下一篇

猜你喜欢

热点阅读