zl程序教程

您现在的位置是:首页 >  其他

当前栏目

跟着Nature Metabolism学作图:R语言ggplot2散点图

2023-02-19 12:27:47 时间

论文

Independent phenotypic plasticity axes define distinct obesity sub-types

https://www.nature.com/articles/s42255-022-00629-2#Sec15

s42255-022-00629-2.pdf

论文中没有公开代码,但是所有作图数据都公开了,我们可以试着用论文中提供的数据模仿论文中的图

今天的推文重复一下论文中的Fig1a 散点图

image.png

散点图背后的圆圈暂时搞不懂是怎么做的,ggplot2里有一个函数geom_contour ()应该可以实现,但是暂时没有搞清楚怎么使用

两个图我采用拼图的形式来实现,但是拼图怎么把横坐标轴的标题居中暂时搞不明白了,这里出图后再编辑

还有一个新知识点,给坐标轴的截断添加子截断,比如图中20到25之间还有一个小阶段,可以借助ggh4x来实现

参考帮助文档代码

p <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point()
p + scale_y_continuous(guide = "axis_minor")

# Minor break positions are still controlled by the scale
p + scale_y_continuous(guide = "axis_minor",
                       minor_breaks = seq(4, 8, by = 0.2))

# Minor tick length is controlled relative to major ticks
p + scale_y_continuous(guide = "axis_minor") +
  theme(ggh4x.axis.ticks.length.minor = rel(0.1))

子截断那个长度应该是一个相对值

部分示例数据截图

image.png

作图代码

fig1a<-read.delim("data/20220921/fig1a.txt",
                  header=TRUE,
                  sep="\t")


library(ggplot2)
#install.packages("ggh4x")
library(ggh4x)
library(latex2exp)
library(tidyverse)

fig1a %>% head()

table(fig1a$genotype)

help(package="ggh4x")

p1<-fig1a %>% 
  filter(genotype=="WT") %>% 
  ggplot(aes(x=Lean.Mass,y=Fat.mass))+
  geom_point(shape=21,size=5,fill="#929292",color="black")+
  scale_x_continuous(limits = c(20,40),
                     breaks = seq(20,40,5),
                     guide = "axis_minor",
                     minor_breaks = seq(22.5, 37.5, by = 5))+
  scale_y_continuous(limits = c(0,25),
                     breaks = seq(0,25,5),
                     guide = "axis_minor",
                     minor_breaks = c(8,12))+
  theme_classic()+
  theme(ggh4x.axis.ticks.length.minor= rel(0.5),
        axis.ticks.length.x = unit(0.5,'lines'))+
  guides(x=guide_axis_minor(trunc_lower = 20,
                            trunc_upper = 40),
         y=guide_axis_truncated(trunc_lower = 0,
                                trunc_upper = 25))+
  geom_vline(xintercept = 32.5,lty="dashed")+
  geom_hline(yintercept = 10,lty="dashed")+
  labs(x="Lean mass (g)",y="Fat mass (g)")+
  annotate(geom = "point",x=20,y=23+1,shape=21,size=5,fill="#929292")+
  annotate(geom = "text",x=20.5,y=23+1,label="WT",size=5,hjust=0)+
  annotate(geom = "point",x=20,y=22-1,shape=21,size=5,fill="#0533ff")+
  annotate(geom = "text",x=20.5,y=22-1,label=TeX(r"(\textit{Nnat}${^+}{^/}{^-}{^p}$)"),size=5,hjust=0)

p2<-fig1a %>% 
  filter(genotype=="Nnat+/-p") %>% 
  ggplot(aes(x=Lean.Mass,y=Fat.mass))+
  geom_point(shape=21,size=5,fill="#0533ff",color="black")+
  scale_x_continuous(limits = c(20,40),
                     breaks = seq(20,40,5),
                     guide = "axis_minor",
                     minor_breaks = seq(22.5, 37.5, by = 5))+
  scale_y_continuous(limits = c(0,25),
                     breaks = seq(0,25,5),
                     guide = "axis_minor",
                     minor_breaks = c(8,12))+
  theme_classic()+
  theme(ggh4x.axis.ticks.length.minor= rel(0.5),
        axis.ticks.length.x = unit(0.5,'lines'),
        axis.line.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank())+
  guides(x=guide_axis_minor(trunc_lower = 20,
                            trunc_upper = 40),
         y=guide_axis_truncated(trunc_lower = 0,
                                trunc_upper = 25))+
  geom_vline(xintercept = 32.5,lty="dashed")+
  geom_hline(yintercept = 10,lty="dashed")+
  labs(x="Lean mass (g)",y=NULL)

library(patchwork)
p1+p2