gganatogram: 使用ggplot绘制示意图
写在前面
为什么我要学习这部分内容呢。因为随着研究的逐步深入,图像和图表之间的界限也越来越小了,我们近经常能看到图形和图像结合的例子。这些可视化方式往往具有极强的视觉冲击力。在第一感觉就引人入胜后会下面的工作似乎就简单了许多。
安装R包
#-安装R包#--------
# devtools::install_github("jespermaag/gganatogram")
#载入所需R包#---------
library(gganatogram)
library(dplyr)
library(viridis)
library(gridExtra)
library(shiny)
library(ggplot2)
基本图形绘制 男人-女人
#-基本图形绘制#---------
#男人全图
hgMale <- gganatogram(data=hgMale_key, fillOutline='#a6bddb', organism='human', sex='male', fill="colour") + theme_void()
# 女人全图
hgFemale <- gganatogram(data=hgFemale_key, fillOutline='#a6bddb', organism='human', sex='female', fill="colour") + theme_void()
# 公老鼠
mmMale <- gganatogram(data=mmMale_key, fillOutline='#a6bddb', organism='mouse', sex='male', fill="colour") + theme_void()
# 母老鼠
mmFemale <- gganatogram(data=mmFemale_key, outline = T, fillOutline='#a6bddb', organism='mouse', sex='female', fill="colour") +theme_void()
# 组合图形
grid.arrange(hgMale, hgFemale, mmMale, mmFemale, ncol=4)
不同的配色系列
#--不同的配色系列--四种模型来一套
hgMale <- gganatogram(data=hgMale_key, fillOutline='#440154FF', organism='human', sex='male', fill="value") + theme_void() + scale_fill_viridis()
hgFemale <- gganatogram(data=hgFemale_key, fillOutline='#440154FF', organism='human', sex='female', fill="value") + theme_void() + scale_fill_viridis()
mmMale <- gganatogram(data=mmMale_key, fillOutline='#440154FF', organism='mouse', sex='male', fill="value") + theme_void() + scale_fill_viridis()
mmFemale <- gganatogram(data=mmFemale_key, outline = T, fillOutline='#440154FF', organism='mouse', sex='female', fill="value") +theme_void() + scale_fill_viridis()
grid.arrange(hgMale, hgFemale, mmMale, mmFemale, ncol=2)
画一部分对象如何选择
#--- 画一部分对象如何选择
organPlot <- data.frame(organ = c("heart", "leukocyte"),
type = c("circulation", "circulation"),
colour = c("red", "yellow"),
value = c(10, 5),
stringsAsFactors=F)
head(organPlot)## organ type colour value
## 1 heart circulation red 10
## 2 leukocyte circulation yellow 5gganatogram(data=organPlot, fillOutline='#a6bddb', organism='human', sex='male', fill="colour")
多选择几个对象
#-多选择几个对象
organPlot <- data.frame(organ = c("heart", "leukocyte", "nerve", "brain", "liver", "stomach", "colon"),
type = c("circulation", "circulation", "nervous system", "nervous system", "digestion", "digestion", "digestion"),
colour = c("red", "red", "purple", "purple", "orange", "orange", "orange"),
value = c(10, 5, 1, 8, 2, 5, 5),
stringsAsFactors=F)
head(organPlot)## organ type colour value
## 1 heart circulation red 10
## 2 leukocyte circulation red 5
## 3 nerve nervous system purple 1
## 4 brain nervous system purple 8
## 5 liver digestion orange 2
## 6 stomach digestion orange 5gganatogram(data=organPlot, fillOutline='#a6bddb', organism='human', sex='male', fill="colour")
#---修改主题
gganatogram(data=organPlot, fillOutline='#a6bddb', organism='human', sex='male', fill="colour") +
theme_void()
人体有多少个器官可供选择呢?
# 人体有多少个器官可供选择呢?
hgMale_key$organ## [1] "thyroid_gland" "bone_marrow"
## [3] "frontal_cortex" "prefrontal_cortex"
## [5] "pituitary_gland" "aorta"
## [7] "gastroesophageal_junction" "left_ventricle"
## [9] "caecum" "ileum"
## [11] "rectum" "nose"
## [13] "breast" "tongue"
## [15] "left_atrium" "pulmonary_valve"
## [17] "mitral_valve" "penis"
## [19] "nasal_pharynx" "spinal_cord"
## [21] "throat" "tricuspid_valve"
## [23] "diaphragm" "liver"
## [25] "stomach" "spleen"
## [27] "duodenum" "gall_bladder"
## [29] "pancreas" "colon"
## [31] "small_intestine" "appendix"
## [33] "smooth_muscle" "urinary_bladder"
## [35] "bone" "cartilage"
## [37] "esophagus" "salivary_gland"
## [39] "parotid_gland" "submandibular_gland"
## [41] "skin" "pleura"
## [43] "brain" "heart"
## [45] "adrenal_gland" "lymph_node"
## [47] "adipose_tissue" "skeletal_muscle"
## [49] "leukocyte" "temporal_lobe"
## [51] "atrial_appendage" "coronary_artery"
## [53] "hippocampus" "vas_deferens"
## [55] "seminal_vesicle" "epididymis"
## [57] "tonsil" "lung"
## [59] "amygdala" "trachea"
## [61] "bronchus" "nerve"
## [63] "cerebellum" "cerebellar_hemisphere"
## [65] "kidney" "renal_cortex"
## [67] "testis" "prostate"# 答案是68个
length(hgMale_key$organ)## [1] 68
仅仅展示我们需要的部分
#------------仅仅展示我们需要的部分#--------
organPlot %>%
dplyr::filter(type %in% c('circulation', 'nervous system')) %>%
gganatogram(outline=F, fillOutline='#a6bddb', organism='human', sex='male', fill="colour") +
theme_void()
同时展示两个人体,这对于处理组和对照组特别有用
#-同时展示两个人体,这对于处理组和对照组特别有用
compareGroups <- rbind(data.frame(organ = c("heart", "leukocyte", "nerve", "brain", "liver", "stomach", "colon"),
colour = c("red", "red", "purple", "purple", "orange", "orange", "orange"),
value = c(10, 5, 1, 8, 2, 5, 5),
type = rep('Normal', 7),
stringsAsFactors=F),
data.frame(organ = c("heart", "leukocyte", "nerve", "brain", "liver", "stomach", "colon"),
colour = c("red", "red", "purple", "purple", "orange", "orange", "orange"),
value = c(5, 5, 10, 8, 2, 5, 5),
type = rep('Cancer', 7),
stringsAsFactors=F))
#--使用的是ggplot中的分面
gganatogram(data=compareGroups, fillOutline='#a6bddb', organism='human', sex='male', fill="value") +
theme_void() +
facet_wrap(~type) +
scale_fill_gradient(low = "white", high = "red")
下面胡虎一点,按照器官分组,展示多个人体
#----下面胡虎一点,按照器官分组,展示多个人体
gganatogram(data=hgMale_key, outline = T, fillOutline='#a6bddb', organism='human', sex='male', fill="colour") +
facet_wrap(~type, ncol=4) +
theme_void()
#-女性有多少个器官呢
hgFemale_key$organ## [1] "atrial_appendage" "ectocervix"
## [3] "hippocampus" "pleura"
## [5] "bronchus" "trachea"
## [7] "lung" "tonsil"
## [9] "submandibular_gland" "breast"
## [11] "spinal_cord" "pancreas"
## [13] "liver" "colon"
## [15] "bone_marrow" "urinary_bladder"
## [17] "stomach" "duodenum"
## [19] "esophagus" "gall_bladder"
## [21] "spleen" "small_intestine"
## [23] "placenta" "endometrium"
## [25] "vagina" "aorta"
## [27] "pituitary_gland" "gastroesophageal_junction"
## [29] "caecum" "appendix"
## [31] "ileum" "left_atrium"
## [33] "left_ventricle" "pulmonary_valve"
## [35] "mitral_valve" "diaphragm"
## [37] "bone" "cartilage"
## [39] "throat" "rectum"
## [41] "nasal_septum" "nasal_pharynx"
## [43] "cerebellum" "cerebellar_hemisphere"
## [45] "prefrontal_cortex" "frontal_cortex"
## [47] "nose" "temporal_lobe"
## [49] "cerebral_cortex" "kidney"
## [51] "renal_cortex" "coronary_artery"
## [53] "tricuspid_valve" "thyroid_gland"
## [55] "skin" "parotid_gland"
## [57] "adipose_tissue" "heart"
## [59] "smooth_muscle" "brain"
## [61] "adrenal_gland" "lymph_node"
## [63] "skeletal_muscle" "ovary"
## [65] "leukocyte" "salivary_gland"
## [67] "fallopian_tube" "uterus"
## [69] "uterine_cervix" "nerve"#--比男性多2个
length(hgFemale_key$organ)## [1] 70
下面还有展示老鼠的内容,但是和人体一样,就不做学习了
展示细胞
#-展示细胞
gganatogram(data=cell_key[['cell']], outline = T, fillOutline='steelblue', organism="cell", fill="colour") +theme_void() + coord_fixed()
gganatogram(data=cell_key[['cell']], outline = T, fillOutline='lightgray', organism="cell", fill="value") +theme_void() + coord_fixed() + scale_fill_viridis()
展示细胞的不同部分,一共有24个部分
#--展示细胞的不同部分,一共有24个部分
figureList <- list()
for (i in 1:nrow(cell_key[['cell']])) {
figureList[[i]] <- gganatogram(data=cell_key[['cell']][i,], outline = T, fillOutline='steelblue', organism="cell", fill="colour") +theme_void() +ggtitle(cell_key[['cell']][i,]$organ) + theme(plot.title = element_text(hjust=0.5, size=16)) + coord_fixed()
}
do.call(grid.arrange, c(figureList[1:4], ncol=2))
do.call(grid.arrange, c(figureList[5:8], ncol=2))
do.call(grid.arrange, c(figureList[9:12], ncol=2))
do.call(grid.arrange, c(figureList[13:16], ncol=2))
do.call(grid.arrange, c(figureList[21:24], ncol=2))
前面学习其实对于我们做环境微生物的用处不大,所以我们继续往下
#--------前面学习其实对于我们做环境微生物的用处不大,所以我们继续往下
length(other_key)## [1] 24
#> [1] 24
names(other_key)## [1] "anolis_carolinensis"
## [2] "arabidopsis_thaliana"
## [3] "bos_taurus"
## [4] "brachypodium_distachyon.flower_parts"
## [5] "brachypodium_distachyon.whole_plant"
## [6] "gallus_gallus"
## [7] "hordeum_vulgare.flower_parts"
## [8] "hordeum_vulgare.whole_plant"
## [9] "macaca_mulatta"
## [10] "monodelphis_domestica"
## [11] "oryza_sativa.flower_parts"
## [12] "oryza_sativa.whole_plant"
## [13] "papio_anubis"
## [14] "rattus_norvegicus"
## [15] "solanum_lycopersicum.flower_parts"
## [16] "solanum_lycopersicum.whole_plant"
## [17] "sorghum_bicolor.flower_parts"
## [18] "sorghum_bicolor.whole_plant"
## [19] "tetraodon_nigroviridis"
## [20] "triticum_aestivum.flower_parts"
## [21] "triticum_aestivum.whole_plant"
## [22] "xenopus_tropicalis"
## [23] "zea_mays.flower_parts"
## [24] "zea_mays.whole_plant"
先来一头牛
#--先来一头牛
gganatogram(data=other_key[["bos_taurus"]], outline = T, fillOutline='white', organism="bos_taurus", sex='female', fill="colour") +
theme_void() +
ggtitle("bos_taurus") +
theme(plot.title = element_text(hjust=0.5)) +
coord_fixed()
组合不同对象
#-----组合不同对象----------------------
library(gridExtra)
plotList <- list()
for (organism in names(other_key)) {
plotList[[organism]] <- gganatogram(data=other_key[[organism]], outline = T, fillOutline='white', organism=organism, sex='female', fill="colour") +
theme_void() +
ggtitle(organism) +
theme(plot.title = element_text(hjust=0.5, size=9)) +
coord_fixed()
}
do.call(grid.arrange, c(plotList[1:4], ncol=2))
前几天公众平台邀请我开通了付费阅读功能,这里我想测试一下,本篇内容大部分都在上面了,被遮盖的内容只有一个,就是示意图的部分选择和展示展示,我觉得官网这个示例是带有颜色的,展示出来是否风险。开发人员也是很皮啊。正好有这个付费功能,遮盖起来吧
展示部分示意图
这是官网的例子,与我无关。
#--展示部分人体---有点涉huang啊
hgFemale_key %>%
dplyr::filter(type=='reproductive') %>%
gganatogram( outline = T, fillOutline='#a6bddb', organism='human', sex='female', fill="colour") +
theme_void() +
coord_cartesian(xlim = c(30, 75), ylim = c(-110, -80))
哈哈,我怎么会让你付一块钱,只看一个图呢,继续我们学习逐步部分的示意图。
重点学习植物
#-----------------重点学习植物--拟南芥
p =gganatogram(data=other_key[["arabidopsis_thaliana"]], outline = T, fillOutline='white', organism="arabidopsis_thaliana", sex='female', fill="colour") +
theme_void()
p
ggsave("./拟南芥.pdf",p)
但是看到这个拟南芥只有6个部分
#---------但是看到这个拟南芥只有6个部分,图形还是很鸡肋的。
#-开始尝试其他植物--这个是燕麦部分
organism = "brachypodium_distachyon.flower_parts"
gganatogram(data=other_key[[organism]], outline = T, fillOutline='white', organism=organism, sex='female', fill="colour") +
theme_void() +
ggtitle(organism) +
theme(plot.title = element_text(hjust=0.5, size=9))
大麦全部植株
#大麦全部植株
organism = "brachypodium_distachyon.whole_plant"
p = gganatogram(data=other_key[[organism]], outline = T, fillOutline='white', organism=organism, sex='female', fill="colour") +
theme_void() +
ggtitle(organism) +
theme(plot.title = element_text(hjust=0.5, size=9))
p
ggsave("./大麦.pdf",p)
花蕊
#--花蕊
organism = "hordeum_vulgare.flower_parts"
p = gganatogram(data=other_key[[organism]], outline = T, fillOutline='white', organism=organism, sex='female', fill="colour") +
theme_void() +
ggtitle(organism) +
theme(plot.title = element_text(hjust=0.5, size=9))
p
水稻,这是开玩笑吗?
#--水稻,这是开玩笑吗?
organism = "hordeum_vulgare.whole_plant"
p = gganatogram(data=other_key[[organism]], outline = T, fillOutline='white', organism=organism, sex='female', fill="colour") +
theme_void() +
ggtitle(organism) +
theme(plot.title = element_text(hjust=0.5, size=9))
p
ggsave("./水稻.pdf",p)
草–部分
#这是什么草--部分
organism = "oryza_sativa.flower_parts"
p = gganatogram(data=other_key[[organism]], outline = T, fillOutline='white', organism=organism, sex='female', fill="colour") +
theme_void() +
ggtitle(organism) +
theme(plot.title = element_text(hjust=0.5, size=9))
p
#--全部植株
organism = "oryza_sativa.whole_plant"
p = gganatogram(data=other_key[[organism]], outline = T, fillOutline='white', organism=organism, sex='female', fill="colour") +
theme_void() +
ggtitle(organism) +
theme(plot.title = element_text(hjust=0.5, size=9))
p
高粱整植株
#----------高粱整植株
organism = "oryza_sativa.whole_plant"
p = gganatogram(data=other_key[[organism]], outline = T, fillOutline='white', organism=organism, sex='female', fill="colour") +
theme_void() +
ggtitle(organism) +
theme(plot.title = element_text(hjust=0.5, size=9))
p
# ggsave("./高粱.pdf",p)
番茄部分
#-番茄部分
organism = "solanum_lycopersicum.flower_parts"
p = gganatogram(data=other_key[[organism]], outline = T, fillOutline='white', organism=organism, sex='female', fill="colour") +
theme_void() +
ggtitle(organism) +
theme(plot.title = element_text(hjust=0.5, size=9))
p
ggsave("./番茄部分.pdf",p)
番茄整个植株
#------番茄整个植株
organism = "solanum_lycopersicum.whole_plant"
p = gganatogram(data=other_key[[organism]], outline = T, fillOutline='white', organism=organism, sex='female', fill="colour") +
theme_void() +
ggtitle(organism) +
theme(plot.title = element_text(hjust=0.5, size=9))
p
ggsave("./番茄整个植株.pdf",p)
另一种草
organism = "sorghum_bicolor.whole_plant"
p = gganatogram(data=other_key[[organism]], outline = T, fillOutline='white', organism=organism, sex='female', fill="colour") +
theme_void() +
ggtitle(organism) +
theme(plot.title = element_text(hjust=0.5, size=9))
p