前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >跟着Nature Genetics学作图:R语言ggplot2普通箱线图/分组箱线图/分面箱线图

跟着Nature Genetics学作图:R语言ggplot2普通箱线图/分组箱线图/分面箱线图

作者头像
用户7010445
发布2023-01-06 19:06:33
6170
发布2023-01-06 19:06:33
举报

论文

Plasma proteome analyses in individuals of European and African ancestry identify cis-pQTLs and models for proteome-wide association studies

https://www.nature.com/articles/s41588-022-01051-w

本地pdf s41588-022-01051-w.pdf

代码链接

https://zenodo.org/record/6332981#.YroV0nZBzic

https://github.com/Jingning-Zhang/PlasmaProtein/tree/v1.2

今天的推文重复一下论文中的Figure3,涉及到4个图,普通箱线图,分组箱线图,箱线图分面,最后一个知识点是如何将这5个图组合到一起

首先是定义了ggplot2的主题

代码语言:javascript
复制
library(ggplot2)

My_Theme <- theme(
  panel.background = element_blank(), 
  title = element_text(size = 7),
  text = element_text(size = 6))

第一个普通的箱线图

部分示例数据集

image.png

读取数据集

代码语言:javascript
复制
library(readxl)
dat01<-read_excel("data/20220627/Fig3.xlsx",
                  sheet = "3a")

作图代码

代码语言:javascript
复制
p1 <- ggplot(data = dat01, aes(x = group)) + 
  geom_boxplot(alpha=0.6, 
               notch = TRUE, 
               notchwidth = 0.5, 
               aes(y=hsq, fill=kind)) +
  coord_cartesian(ylim = c(0,0.5)) +  
  labs(y = expression(paste("cis-",h^2)),
       x=NULL, title=NULL) +
  theme(legend.position="top",
        legend.title=element_blank(), 
        axis.text.x = element_text(color = c("#4a1486", 
                                             "#4a1486", 
                                             "#cb181d",
                                             "#cb181d"),
                                   vjust = 0.5, 
                                   hjust = 0.5, 
                                   angle = 15))+
  My_Theme+
  scale_fill_manual(values=c("#4a1486","#cb181d"))+
  theme(axis.line = element_line())
p1

image.png

分组箱线图

作图代码

代码语言:javascript
复制
dat02<-read_excel("data/20220627/Fig3.xlsx",
                  sheet = "3b")
head(dat02)

p2 <- ggplot(data = dat02, aes(x = group)) +
  geom_boxplot(alpha=0.8, 
               notch = TRUE, 
               notchwidth = 0.5, 
               aes(y=acc, fill=Model)) + 
  coord_cartesian(ylim = c(0,1.2)) +
  labs(title = NULL, x=NULL,
       y=expression(paste(R^2,"/cis-",h^2))) +
  theme(legend.position="top",
        axis.text.x = element_text(color = c("#4a1486", 
                                             "#4a1486", 
                                             "#cb181d",
                                             "#cb181d"),
                                   vjust = 0.5, 
                                   hjust = 0.5, 
                                   angle = 15))+
  My_Theme+
  scale_fill_manual(values=c("#feb24c","#41b6c4"))+
  theme(axis.line = element_line())
p2

箱线图分面

代码语言:javascript
复制
dat03<-read_excel("data/20220627/Fig3.xlsx",
                  sheet = "3c")
head(dat03)
p3 <- ggplot(data = dat03, aes(x = model)) + 
  geom_boxplot(alpha=0.8, 
               notch = TRUE, 
               notchwidth = 0.5, 
               aes(y=acc, fill=model)) + 
  facet_wrap(~race,  ncol=2)+
  labs(title = NULL, x=NULL,
       y=expression(paste(R^2,"/cis-",h^2))) +
  coord_cartesian(ylim = c(0,1.2))  +
  theme(axis.text.x = element_text(color = c("#238b45", 
                                             "#2171b5"),
                                   vjust = 0.5, 
                                   hjust = 0.5, 
                                   angle = 15),
        legend.position="none") +
  My_Theme+
  scale_fill_manual(values=c("#238b45","#2171b5"))+
  theme(axis.line = element_line(),
        panel.spacing.x = unit(0,'lines'),
        strip.background = element_rect(color="white"))
p3

这里两个小知识点,

  • 默认分面两个图之间是有空白的,如果想没有这个空白可以在主题里进行设置 panel.spacing.x = unit(0,'lines')
  • 两个图中间没有空白,上面灰色区域的地方如果想区分开,可以将边框颜色设置为白色strip.background = element_rect(color="white")

image.png

最后一个箱线图

代码语言:javascript
复制
dat04<-read_excel("data/20220627/Fig3.xlsx",
                  sheet = "3d")
head(dat04)
gtex.colors <- read_excel("data/20220627/gtex_colors.xlsx")
gtex.colors

myColors <- gtex.colors$V2
names(myColors) <- gtex.colors$V1
colScale <- scale_fill_manual(name = "gtex.colors", values = myColors)

p4 <- ggplot(data = dat04, aes(x = tissue, fill=tissue)) +
  geom_boxplot(alpha=0.8, 
               notch = TRUE, 
               notchwidth = 0.5, 
               aes(y=cor)) + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1),
        legend.position="none",
        axis.title.y = element_text(hjust=1))+
  My_Theme+
  coord_cartesian(ylim = c(-0.25,1))+
  colScale +
  labs(x = "GTEx V7 tissue", 
       y = "Correlation between cis-regulated gene       \nexpression and plasma protein SOMAmers      ",
       title=NULL)+
  theme(axis.line = element_line())
p4

image.png

将四个图组合到一起

代码语言:javascript
复制
library(ggpubr)
p <- ggarrange(ggarrange(p1, p2,
                         p3,
                         ncol = 3, labels = c("a", "b","c"),
                         widths = c(0.29,0.4,0.31)),
               p4,
               nrow = 2, heights = c(0.5,0.5),
               labels = c(NA,"d"))
p

image.png

示例数据和代码可以自己到论文中获取

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-07-05,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 论文
  • 代码链接
  • 首先是定义了ggplot2的主题
  • 第一个普通的箱线图
  • 分组箱线图
  • 箱线图分面
  • 最后一个箱线图
  • 将四个图组合到一起
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档