提问人:isaac vingan 提问时间:10/12/2023 最后编辑:isaac vingan 更新时间:10/13/2023 访问量:33
在分面图中指定空列
Specify empty columns in facet plot
问:
简短的背景:我是一名神经科学家,使用来自训练和对照动物的基因表达数据。这是相关的,因为可以使用许多因素来描述每个数据点
我有一个项目列表(基因通路),我想用点图表示它们在每种条件下的表达。条件可以通过以下因素来描述:训练(训练或控制)、方向(向上或向下)和细胞标志物群(marker1、marker2、marker3)。点图将表达式的大小显示为点的大小,并将统计数据的显著性显示为点的颜色。
因此,我正在尝试构建某种嵌套点图,其中嵌套的第一级是训练因子,下一级是方向因子,然后在这个级别中,总共有 4 个图,每个图包含 3 列(每个细胞标记物群一列)。 问题在于,并非每个细胞标记物群体在每种条件下的 y 轴上都有所有基因通路列表的重要数据。
这似乎是一项明确的工作,但此软件包无法为每个小平面图选择特定的比例。当我设置 时,所有空列都会从每个小平面图中删除,包括两个:没有这些因子集的任何数据的列和应该存在但没有数据的列,因此它们被排除在外。ggh4x::facet_nested()
scales = "free"
我正在寻找一种方法来指定我希望在每个方面包含哪些列,以尊重绘图的视觉对称性
ggplot(data = EA_Big_Table_filt %>% filter(FDR < 0.01),
aes(x = region, y = GOs_ordered, color = -log10(FDR), size = enrichmentRatio)) +
geom_point() +
scale_color_viridis()+
theme_bw() +
ylab("") +
xlab("") +
scale_y_discrete(limits = rev)+
ggtitle("GO enrichment analysis")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
facet_nested(. ~ train + reg, scales = "free", space = "free")
我附上了 2 张数据图像,一张是(默认),另一张是(更接近我想看的,但不完全是)ggh4x::facet_nested(scales = "fixed")
ggh4x::facet_nested(scales = "free")
编辑以添加可重现性最低限度的数据、代码和会话信息:
数据:
min_repro_filt <- structure(list(description = c("regulation of trans-synaptic signaling",
"vesicle-mediated transport in synapse", "axon development",
"regulation of trans-synaptic signaling", "glutamate receptor signaling pathway",
"axon development", "gliogenesis", "vesicle-mediated transport in synapse",
"gliogenesis", "glutamate receptor signaling pathway", "regulation of trans-synaptic signaling",
"vesicle-mediated transport in synapse", "axon development",
"glutamate receptor signaling pathway", "regulation of trans-synaptic signaling",
"gliogenesis", "axon development", "gliogenesis", "vesicle-mediated transport in synapse",
"regulation of trans-synaptic signaling", "axon development",
"vesicle-mediated transport in synapse", "glutamate receptor signaling pathway",
"gliogenesis", "regulation of trans-synaptic signaling", "regulation of trans-synaptic signaling",
"vesicle-mediated transport in synapse", "glutamate receptor signaling pathway",
"axon development", "gliogenesis", "cell junction organization",
"gliogenesis", "regulation of trans-synaptic signaling", "glutamate receptor signaling pathway",
"axon development", "cell junction organization", "vesicle-mediated transport in synapse",
"regulation of trans-synaptic signaling", "glutamate receptor signaling pathway",
"vesicle-mediated transport in synapse", "axon development",
"gliogenesis", "cell junction organization", "axon development",
"gliogenesis", "regulation of trans-synaptic signaling", "glutamate receptor signaling pathway",
"vesicle-mediated transport in synapse", "cell junction organization",
"cell junction organization", "gliogenesis", "regulation of trans-synaptic signaling",
"axon development", "axon development", "glutamate receptor signaling pathway",
"vesicle-mediated transport in synapse", "regulation of trans-synaptic signaling",
"gliogenesis", "vesicle-mediated transport in synapse", "glutamate receptor signaling pathway"
), enrichmentRatio = c(6.68500452254813, 8.65118232329758, 4.89859154929577,
6.2819380733945, 9.26760563380282, 3.62291666666667, 2.49382842509603,
4.18795871559633, 3.31990909090909, 4.1125, 6.46920452002685,
7.49065786529425, 4.94837398373984, 6.01829268292683, 3.05371989678899,
2.69911308203991, 2.7171875, 2.76659090909091, 2.61747419724771,
5.26023800026592, 5.04057971014493, 4.85560430793777, 4.76811594202899,
2.56611330698287, 3.28467350242849, 5.19782110091743, 5.34633027522936,
8.75, 3.08333333333333, 2.20445490764216, 2.3685073658853, 1.88363636363636,
1.5572754852151, 1.96613545816733, 1.27018592297477, 1.2046511627907,
1.22357359552615, 3.74002543656578, 7.68691588785047, 3.82700277229987,
2.70872274143302, 2.28959247648903, 2.0337142475274, 1.540780651341,
1.51688473520249, 1.24355095961194, 1.65445402298851, 1.28366550669619,
0.881909729768891, 2.97470777501731, 2.55824868651489, 2.88824739006643,
2.87334770114943, 1.87807939287799, 4.25431034482759, 2.6475601075609,
1.46688571474477, 1.71719435736677, 1.2713009527788, 1.44045534150613
), FDR = c(2.73232271030821e-07, 1.91352946274925e-05, 0.000799115680928431,
0.00737891722498407, 0.0251647909352357, 0.377609720014827, 0.482007003299911,
0.559306907495358, 0.622940182995214, 0.824998329823089, 4.55977451352396e-08,
8.80653616306404e-05, 0.000190625247077728, 0.171932712695981,
0.286373116680475, 0.33443668772753, 0.408598183442779, 0.610959581662353,
0.769244655353647, 0.000673089562158502, 0.00146411622888287,
0.066152305546214, 0.500038510445596, 0.514416053344859, 0.648796603956836,
4.50750547997814e-13, 2.86037431496133e-06, 3.76246830418777e-05,
0.000518693843230148, 0.0446535816600178, 0.0446535816600178,
0.307323155761676, 0.430536006591497, 0.551164801987552, 0.733800798695753,
0.808819620683588, 0.863106002361249, 4.34523528269892e-11, 2.7178716477394e-07,
1.6300065634689e-05, 8.05719824708007e-05, 0.00157307543882146,
0.0408955420096159, 0.117659135740778, 0.372628642468256, 0.526802985860736,
0.538620753649825, 0.599873558319722, 0.936848868711327, 6.37736750075391e-05,
0.00018792064357287, 0.00216261020136876, 0.00216261020136876,
0.0192479493927484, 0.12526745194893, 0.12526745194893, 0.228192711206325,
0.530843810076351, 0.60232342998248, 0.639795828874161), region = c("TRArcPos_TRArcNeg",
"TRArcPos_TRArcNeg", "TRArcPos_TRArcNeg", "TRArcPos_TRArcNeg",
"TRArcPos_TRArcNeg", "TRArcPos_TRArcNeg", "TRArcPos_TRArcNeg",
"TRArcPos_TRArcNeg", "TRArcPos_TRArcNeg", "TRArcPos_TRArcNeg",
"TREgr1Pos_TREgr1Neg", "TREgr1Pos_TREgr1Neg", "TREgr1Pos_TREgr1Neg",
"TREgr1Pos_TREgr1Neg", "TREgr1Pos_TREgr1Neg", "TREgr1Pos_TREgr1Neg",
"TREgr1Pos_TREgr1Neg", "TREgr1Pos_TREgr1Neg", "TREgr1Pos_TREgr1Neg",
"TRJunPos_TRJunNeg", "TRJunPos_TRJunNeg", "TRJunPos_TRJunNeg",
"TRJunPos_TRJunNeg", "TRJunPos_TRJunNeg", "TRJunPos_TRJunNeg",
"UTArcPos_UTArcNeg", "UTArcPos_UTArcNeg", "UTArcPos_UTArcNeg",
"UTArcPos_UTArcNeg", "UTArcPos_UTArcNeg", "UTArcPos_UTArcNeg",
"UTArcPos_UTArcNeg", "UTArcPos_UTArcNeg", "UTArcPos_UTArcNeg",
"UTArcPos_UTArcNeg", "UTArcPos_UTArcNeg", "UTArcPos_UTArcNeg",
"UTEgr1Pos_UTEgr1Neg", "UTEgr1Pos_UTEgr1Neg", "UTEgr1Pos_UTEgr1Neg",
"UTEgr1Pos_UTEgr1Neg", "UTEgr1Pos_UTEgr1Neg", "UTEgr1Pos_UTEgr1Neg",
"UTEgr1Pos_UTEgr1Neg", "UTEgr1Pos_UTEgr1Neg", "UTEgr1Pos_UTEgr1Neg",
"UTEgr1Pos_UTEgr1Neg", "UTEgr1Pos_UTEgr1Neg", "UTEgr1Pos_UTEgr1Neg",
"UTJunPos_UTJunNeg", "UTJunPos_UTJunNeg", "UTJunPos_UTJunNeg",
"UTJunPos_UTJunNeg", "UTJunPos_UTJunNeg", "UTJunPos_UTJunNeg",
"UTJunPos_UTJunNeg", "UTJunPos_UTJunNeg", "UTJunPos_UTJunNeg",
"UTJunPos_UTJunNeg", "UTJunPos_UTJunNeg"), reg = c("UP", "UP",
"UP", "DOWN", "UP", "DOWN", "UP", "DOWN", "DOWN", "DOWN", "UP",
"UP", "UP", "UP", "DOWN", "UP", "DOWN", "DOWN", "DOWN", "UP",
"UP", "UP", "UP", "UP", "DOWN", "UP", "UP", "UP", "UP", "DOWN",
"DOWN", "UP", "DOWN", "DOWN", "DOWN", "UP", "DOWN", "UP", "UP",
"UP", "UP", "DOWN", "DOWN", "DOWN", "UP", "DOWN", "DOWN", "DOWN",
"UP", "DOWN", "DOWN", "UP", "UP", "DOWN", "UP", "UP", "DOWN",
"UP", "DOWN", "DOWN"), ieg = c("Arc", "Arc", "Arc", "Arc", "Arc",
"Arc", "Arc", "Arc", "Arc", "Arc", "Egr1", "Egr1", "Egr1", "Egr1",
"Egr1", "Egr1", "Egr1", "Egr1", "Egr1", "Jun", "Jun", "Jun",
"Jun", "Jun", "Jun", "Arc", "Arc", "Arc", "Arc", "Arc", "Arc",
"Arc", "Arc", "Arc", "Arc", "Arc", "Arc", "Egr1", "Egr1", "Egr1",
"Egr1", "Egr1", "Egr1", "Egr1", "Egr1", "Egr1", "Egr1", "Egr1",
"Egr1", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun",
"Jun", "Jun", "Jun"), train = c("TR", "TR", "TR", "TR", "TR",
"TR", "TR", "TR", "TR", "TR", "TR", "TR", "TR", "TR", "TR", "TR",
"TR", "TR", "TR", "TR", "TR", "TR", "TR", "TR", "TR", "UT", "UT",
"UT", "UT", "UT", "UT", "UT", "UT", "UT", "UT", "UT", "UT", "UT",
"UT", "UT", "UT", "UT", "UT", "UT", "UT", "UT", "UT", "UT", "UT",
"UT", "UT", "UT", "UT", "UT", "UT", "UT", "UT", "UT", "UT", "UT"
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA,
-60L), groups = structure(list(region = c("TRArcPos_TRArcNeg",
"TREgr1Pos_TREgr1Neg", "TRJunPos_TRJunNeg", "UTArcPos_UTArcNeg",
"UTEgr1Pos_UTEgr1Neg", "UTJunPos_UTJunNeg"), .rows = structure(list(
1:10, 11:19, 20:25, 26:37, 38:49, 50:60), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L), .drop = TRUE))
GGPLOT代码:
library(tidyverse)
library(ggplot2)
library(viridisLite)
library(viridis)
library(gghx4)
ggplot(data = min_repro_filt %>% filter(FDR < 0.01),
aes(x = region, y = description, color = -log10(FDR), size = enrichmentRatio)) +
geom_point() +
scale_color_viridis()+
theme_bw() +
ylab("") +
xlab("") +
scale_y_discrete(limits = rev)+
ggtitle("GO enrichment analysis")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
facet_nested(. ~ train + reg, scales = "free", space = "free") +
scale_x_discrete(labels=c("UTArcPos_UTArcNeg" = expression("Untrained"^italic("Arc+")*"vs. Untrained"^italic("Arc-")),
"TRArcPos_TRArcNeg" = expression("Trained"^italic("Arc+")*"vs. Trained"^italic("Arc-")),
"UTEgr1Pos_UTEgr1Neg" = expression("Untrained"^italic("Egr1+")*"vs. Untrained"^italic("Egr1-")),
"TREgr1Pos_TREgr1Neg" = expression("Trained"^italic("Egr1+")*"vs. Trained"^italic("Egr1-")),
"UTJunPos_UTJunNeg" = expression("Untrained"^italic("Jun+")*"vs. Untrained"^italic("Jun-")),
"TRJunPos_TRJunNeg" = expression("Trained"^italic("Jun+")*"vs. Trained"^italic("Jun-"))))
会话信息
R version 4.1.1 (2021-08-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.6.3
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] grid stats4 stats graphics grDevices utils datasets methods
[9] base
other attached packages:
[1] ggh4x_0.2.6 ggthemes_4.2.4 SeuratObject_4.1.3
[4] Seurat_4.3.0 hrbrthemes_0.8.0 dendsort_0.3.4
[7] ComplexHeatmap_2.11.1 broom_1.0.5 ggpubr_0.6.0
[10] SuperExactTest_1.1.0 org.Mm.eg.db_3.13.0 AnnotationDbi_1.54.1
[13] IRanges_2.28.0 S4Vectors_0.32.3 Biobase_2.52.0
[16] BiocGenerics_0.40.0 BiocManager_1.30.22 clusterProfiler_4.0.5
[19] viridis_0.6.4 viridisLite_0.4.2 WebGestaltR_0.4.5
[22] patchwork_1.1.3 ggbreak_0.1.2 EnhancedVolcano_1.10.0
[25] ggrepel_0.9.3 lubridate_1.9.2 forcats_1.0.0
[28] stringr_1.5.0 dplyr_1.1.2 purrr_1.0.1
[31] readr_2.1.4 tidyr_1.3.0 tibble_3.2.1
[34] tidyverse_2.0.0 ggplot2_3.4.3 GeneOverlap_1.28.0
loaded via a namespace (and not attached):
[1] scattermore_0.8 knitr_1.44 bit64_4.0.5
[4] irlba_2.3.5.1 data.table_1.14.8 KEGGREST_1.32.0
[7] RCurl_1.98-1.12 doParallel_1.0.17 generics_0.1.3
[10] callr_3.7.3 cowplot_1.1.1 usethis_2.2.2
[13] RSQLite_2.3.1 shadowtext_0.1.2 RANN_2.6.1
[16] future_1.33.0 bit_4.0.5 tzdb_0.3.0
[19] enrichplot_1.12.3 spatstat.data_3.0-1 httpuv_1.6.10
[22] xfun_0.39 hms_1.1.3 evaluate_0.22
[25] promises_1.2.0.1 fansi_1.0.4 caTools_1.18.2
[28] igraph_1.4.2 DBI_1.1.3 htmlwidgets_1.6.2
[31] apcluster_1.4.10 spatstat.geom_3.2-1 ellipsis_0.3.2
[34] fontLiberation_0.1.0 backports_1.4.1 fontBitstreamVera_0.1.1
[37] deldir_1.0-6 vctrs_0.6.2 remotes_2.4.2.1
[40] ROCR_1.0-11 abind_1.4-5 cachem_1.0.8
[43] withr_2.5.1 ggforce_0.4.1 RVenn_1.1.0
[46] progressr_0.14.0 vroom_1.6.3 sctransform_0.3.5
[49] treeio_1.16.2 prettyunits_1.2.0 goftest_1.2-3
[52] svglite_2.1.1 cluster_2.1.4 DOSE_3.18.3
[55] ape_5.7-1 lazyeval_0.2.2 crayon_1.5.2
[58] crul_1.4.0 spatstat.explore_3.1-0 pkgconfig_2.0.3
[61] labeling_0.4.3 tweenr_2.0.2 GenomeInfoDb_1.28.4
[64] pkgload_1.3.3 nlme_3.1-162 vipor_0.4.5
[67] devtools_2.4.5 rlang_1.1.1 globals_0.16.2
[70] lifecycle_1.0.3 miniUI_0.1.1.1 downloader_0.4
[73] fontquiver_0.2.1 httpcode_0.3.0 extrafontdb_1.0
[76] ggrastr_1.0.2 polyclip_1.10-4 matrixStats_0.63.0
[79] lmtest_0.9-40 rngtools_1.5.2 Matrix_1.5-1
[82] aplot_0.2.1 carData_3.0-5 zoo_1.8-12
[85] beeswarm_0.4.0 processx_3.8.1 GlobalOptions_0.1.2
[88] whisker_0.4.1 ggridges_0.5.4 rjson_0.2.21
[91] png_0.1-8 bitops_1.0-7 KernSmooth_2.23-21
[94] Biostrings_2.60.2 blob_1.2.4 shape_1.4.6
[97] doRNG_1.8.6 qvalue_2.24.0 parallelly_1.36.0
[100] spatstat.random_3.1-4 rstatix_0.7.2 gridGraphics_0.5-1
[103] ggsignif_0.6.4 scales_1.2.1 memoise_2.0.1
[106] magrittr_2.0.3 plyr_1.8.8 ica_1.0-3
[109] gplots_3.1.3 zlibbioc_1.38.0 compiler_4.1.1
[112] scatterpie_0.2.1 RColorBrewer_1.1-3 ash_1.0-15
[115] clue_0.3-64 fitdistrplus_1.1-11 cli_3.6.1
[118] urlchecker_1.0.1 XVector_0.32.0 listenv_0.9.0
[121] ps_1.7.5 pbapply_1.7-2 mgcv_1.8-42
[124] MASS_7.3-60 tidyselect_1.2.0 stringi_1.7.12
[127] proj4_1.0-12 GOSemSim_2.18.1 fastmatch_1.1-3
[130] tools_4.1.1 timechange_0.2.0 future.apply_1.11.0
[133] parallel_4.1.1 circlize_0.4.15 rstudioapi_0.15.0
[136] foreach_1.5.2 gridExtra_2.3 farver_2.1.1
[139] Rtsne_0.16 ggraph_2.1.0 digest_0.6.31
[142] shiny_1.7.5 gfonts_0.2.0 Rcpp_1.0.10
[145] car_3.1-2 ggalt_0.4.0 later_1.3.1
[148] RcppAnnoy_0.0.20 gdtools_0.3.3 httr_1.4.7
[151] colorspace_2.1-0 fs_1.6.2 tensor_1.5
[154] reticulate_1.28 splines_4.1.1 uwot_0.1.14
[157] yulab.utils_0.1.0 ggVennDiagram_1.2.3 tidytree_0.4.5
[160] spatstat.utils_3.0-3 graphlayouts_1.0.0 sp_1.6-0
[163] ggplotify_0.1.2 sessioninfo_1.2.2 plotly_4.10.2
[166] systemfonts_1.0.4 xtable_1.8-4 jsonlite_1.8.4
[169] ggtree_3.0.4 tidygraph_1.2.3 ggfun_0.1.3
[172] R6_2.5.1 profvis_0.3.8 pillar_1.9.0
[175] htmltools_0.5.5 mime_0.12 glue_1.6.2
[178] fastmap_1.1.1 BiocParallel_1.26.2 codetools_0.2-19
[181] maps_3.4.1 fgsea_1.18.0 pkgbuild_1.4.2
[184] utf8_1.2.3 lattice_0.21-8 spatstat.sparse_3.0-1
[187] curl_5.0.0 ggbeeswarm_0.7.2 leiden_0.4.3
[190] gtools_3.9.4 GO.db_3.13.0 Rttf2pt1_1.3.12
[193] limma_3.48.3 survival_3.5-5 rmarkdown_2.25
[196] munsell_0.5.0 GetoptLong_1.0.5 DO.db_2.9
[199] GenomeInfoDbData_1.2.6 iterators_1.0.14 reshape2_1.4.4
[202] gtable_0.3.4 extrafont_0.19
希望此编辑有所帮助。我试图在所有 4 个方面获得这三列
答:
我找到了一个答案,使用用户 stefan 建议。具体来说,我用作表达式的补充gghx4
+ gghx4::facetted_pos_scales(x = scales)
ggplot2
这感觉像是一种冗长的蛮力(但主要是因为我的可变标题的长度太长了),如下所示:
scales <- list(
scale_x_discrete(
limits = c("TRArcPos_TRArcNeg", "TREgr1Pos_TREgr1Neg", "TRJunPos_TRJunNeg"),
labels=c("TRArcPos_TRArcNeg" = expression("Trained"^italic("Arc+")*"vs. Trained"^italic("Arc-")),
"TREgr1Pos_TREgr1Neg" = expression("Trained"^italic("Egr1+")*"vs. Trained"^italic("Egr1-")),
"TRJunPos_TRJunNeg" = expression("Trained"^italic("Jun+")*"vs. Trained"^italic("Jun-")))),
scale_x_discrete(
limits = c("TRArcPos_TRArcNeg", "TREgr1Pos_TREgr1Neg", "TRJunPos_TRJunNeg"),
labels=c("TRArcPos_TRArcNeg" = expression("Trained"^italic("Arc+")*"vs. Trained"^italic("Arc-")),
"TREgr1Pos_TREgr1Neg" = expression("Trained"^italic("Egr1+")*"vs. Trained"^italic("Egr1-")),
"TRJunPos_TRJunNeg" = expression("Trained"^italic("Jun+")*"vs. Trained"^italic("Jun-")))),
scale_x_discrete(
limits = c("UTArcPos_UTArcNeg","UTEgr1Pos_UTEgr1Neg","UTJunPos_UTJunNeg"),
labels=c("UTArcPos_UTArcNeg" = expression("Untrained"^italic("Arc+")*"vs. Untrained"^italic("Arc-")),
"UTEgr1Pos_UTEgr1Neg" = expression("Untrained"^italic("Egr1+")*"vs. Untrained"^italic("Egr1-")),
"UTJunPos_UTJunNeg" = expression("Untrained"^italic("Jun+")*"vs. Untrained"^italic("Jun-")))),
scale_x_discrete(
limits = c("UTArcPos_UTArcNeg","UTEgr1Pos_UTEgr1Neg","UTJunPos_UTJunNeg"),
labels=c("UTArcPos_UTArcNeg" = expression("Untrained"^italic("Arc+")*"vs. Untrained"^italic("Arc-")),
"UTEgr1Pos_UTEgr1Neg" = expression("Untrained"^italic("Egr1+")*"vs. Untrained"^italic("Egr1-")),
"UTJunPos_UTJunNeg" = expression("Untrained"^italic("Jun+")*"vs. Untrained"^italic("Jun-"))))
)
4 个分面的 4 个单独的离散轴表达式,按它们在图形中的显示顺序排列。
评论
scales="free"
ggh4x
dput()