前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >跟着Nature Metabolism学作图:R语言ggplot2散点图

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

作者头像
用户7010445
发布2023-01-06 19:39:38
2290
发布2023-01-06 19:39:38
举报

论文

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来实现

参考帮助文档代码

代码语言:javascript
复制
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

作图代码

代码语言:javascript
复制
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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-09-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 论文
  • 部分示例数据截图
  • 作图代码
相关产品与服务
图数据库 KonisGraph
图数据库 KonisGraph(TencentDB for KonisGraph)是一种云端图数据库服务,基于腾讯在海量图数据上的实践经验,提供一站式海量图数据存储、管理、实时查询、计算、可视化分析能力;KonisGraph 支持属性图模型和 TinkerPop Gremlin 查询语言,能够帮助用户快速完成对图数据的建模、查询和可视化分析。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档