欢迎关注R语言数据分析指南
❝本节来介绍一个简单的图形绘制思路,虽然图形简单但是在代码细节上也有些许可取之处。源数据来源与NC上的一篇论文,小编根据自己对数据的理解来进行绘图与原文有所出入,只做绘图展示仅供参考。
论文
Multiomic analysis of familial adenomatous polyposis reveals molecular pathways associated with early tumorigenesis
论文图
图形解读
❝作为基础图此图的内容无需再做介绍,那么绘制此图的方法大致有两种。
1.自定义分面刻度一气呵成绘制全图
2.单独绘图最终拼接成整图
显然第一种方法对于初学者来说稍有难度,绝大多数情况下选择第二种思路较为简单。那么此图绘制还有一个细节即下方天蓝色的柱状图Y轴文本也为正值,看起来不太符合常理,其实在此是针对绘图数据取了绝对值从而将Y轴转换为了正值。理解了这一点则问题就迎刃而解了,需要注意类似替换轴刻度或者轴标签的方法在一些特殊图中会经常用到。scale_y_continuous要根据需求合理设置
数据展示
代码展示
library(tidyverse)
#library(ggplotify)
library(patchwork)
df <- tibble::tribble(
~Graph, ~Set, ~Transcriptomics, ~Proteomics, ~Lipidome, ~Metabolome,
"Histogram", "M-B.U", NA, 154L, 21L, 89L,
"Histogram", "M-D.U", 774L, 241L, 32L, 139L,
"Histogram", "B-D.U", NA, NA, 12L, 45L,
"Histogram", "M-B.D", NA, 371L, 381L, 69L,
"Histogram", "M-D.D", 1336L, 685L, 406L, 94L,
"Histogram", "B-D.D", NA, NA, 48L, 50L
) %>%
separate(Set, into = c("Set_Part1", "Set_Part2"), sep = "\\.")
df$Set_Part1 <- factor(df$Set_Part1,levels = unique(df$Set_Part1))
p1 <- df %>% slice_head(n=3) %>% ggplot(aes(Set_Part1,Transcriptomics))+
geom_col(fill="red")+
scale_y_continuous(expand = c(0,0),limits = c(0,1400),
breaks = seq(0,1400,by = 200))+
theme_classic()+
theme(axis.text.x=element_text(color="black",angle = 90,vjust=0.5),
axis.text.y=element_text(color="black"),
axis.ticks.x = element_blank(),
plot.margin = margin(0,0.5,0,0.5))+
labs(x=NULL,y=NULL)
p2 <- df %>% slice(4:n()) %>%
ggplot(aes(Set_Part1,-Transcriptomics))+
geom_col(fill="skyblue") +
scale_y_continuous(expand = c(0,0),
limits = c(-1400,0),
breaks = seq(-1400,0,by = 200),
# 将标签转换为正值
labels = function(x) abs(x))+
scale_x_discrete(position = "top") +
labs(x=NULL,y=NULL) +
theme_classic()+
theme(axis.text.x=element_blank(),
axis.ticks.x = element_blank(),
plot.margin = margin(0,0.5,0,0.5),
axis.text.y=element_text(color="black",angle =0,vjust=0.5))
p3 <- df %>% slice_head(n=3) %>% ggplot(aes(Set_Part1,Proteomics))+
geom_col(fill="red")+
scale_y_continuous(expand = c(0,0),
limits = c(0,700),
breaks=seq(0,700, by = 100))+
theme_classic()+
theme(axis.text.x=element_text(color="black",angle = 90,vjust=0.5),
axis.text.y=element_text(color="black"),
axis.ticks.x = element_blank(),
plot.margin = margin(0.5,0.5,0,0.5))+
labs(x=NULL,y=NULL)
p4 <- df %>% slice(4:n()) %>%
ggplot(aes(Set_Part1,-Proteomics))+
geom_col(fill="skyblue") +
scale_y_continuous(expand = c(0,0),
limits = c(-700,0),
breaks=seq(-700,0,by = 100),
labels = function(x) abs(x))+
scale_x_discrete(position = "top") +
labs(x=NULL,y=NULL) +
theme_classic()+
theme(axis.text.x=element_blank(),
axis.ticks.x = element_blank(),
plot.margin = margin(0,0.5,0,0.5),
axis.text.y=element_text(color="black",angle =0,vjust=0.5))
p5 <- df %>% slice_head(n=3) %>% ggplot(aes(Set_Part1,Metabolome))+
geom_col(fill="red")+
scale_y_continuous(expand = c(0,0),
limits = c(0,150),
breaks=seq(0,150, by =50))+
theme_classic()+
theme(axis.text.x=element_text(color="black",angle = 90,vjust=0.5),
axis.text.y=element_text(color="black"),
axis.ticks.x = element_blank(),
plot.margin = margin(0.5,0.5,0,0.5))+
labs(x=NULL,y=NULL)
p6 <- df %>% slice(4:n()) %>%
ggplot(aes(Set_Part1,-Metabolome))+
geom_col(fill="skyblue") +
scale_y_continuous(expand = c(0,0),
limits = c(-150,0),
breaks=seq(-150,0,by = 50),
labels = function(x) abs(x))+
scale_x_discrete(position = "top") +
labs(x=NULL,y=NULL) +
theme_classic()+
theme(axis.text.x=element_blank(),
axis.ticks.x = element_blank(),
plot.margin = margin(0,0.5,0,0.5),
axis.text.y=element_text(color="black",angle =0,vjust=0.5))
(p1/p2)|(p3/p4)|(p5/p6)
关注下方公众号下回更新不迷路
购买介绍
❝本节介绍到此结束,有需要学习R数据可视化的朋友欢迎到淘宝店铺:R语言数据分析指南,购买小编的R语言可视化文档(2024版),购买将赠送2023年的绘图文档内容。目前此文档(2023+2024)已经更新上传200案例文档,每个案例都附有相应的数据和代码,并配有对应的注释文档,方便大家学习和参考。
2024更新的绘图内容将同时包含数据+代码+注释文档+文档清单,2023无目录仅有数据文件夹,小编只分享案例文档,不额外回答问题,无答疑服务,零基础不推荐买。
案例特点
所选案例图均属于个性化分析图表完全适用于论文发表,内容异常丰富两年累计发布案例图200+,2024年6月起提供html版注释文档更加直观易学。文档累计上千人次购买拥有良好的社群交流体验。R代码结构清晰易懂,为防止中文乱码提供单独的注释文档
R代码结构清晰易懂,为防止中文乱码2024年6月起提供单独html注释文档