【发布时间】:2017-08-30 14:34:05
【问题描述】:
TL;DR - 创建平铺图,同一个平铺中的两种美学导致平铺颜色无法呈现,不确定是我的代码还是 ggplot2 的东西,有人经历过吗?
当我尝试使用 ggplot 在同一个图块中绘制多个美学时,我遇到了一个奇怪的绘图问题(不是错误,这静默失败,我只在查看最终表示时才注意到问题)。
我的数据:
satur_muts <- structure(list(id = c("0400136_T1", "0400136_T1", "0400136_T1",
"0400136_T1", "0400171_T1", "0400171_T1", "0400171_T1", "0400171_T1",
"0400179_T1", "0400179_T1", "0400179_T1", "0400195_T1", "0400195_T1",
"0400195_T1", "0400195_T1"), gene = c("CDKN2A", "TP53", "KRAS",
"KRAS", "CDKN2A", "TP53", "KRAS", "KRAS", "CDKN2A", "TP53", "KRAS",
"CDKN2A", "TP53", "KRAS", "KRAS"), mut_type = structure(c(NA,
NA, 1L, NA, NA, NA, NA, 1L, NA, NA, 1L, NA, NA, 1L, NA), .Label = "Missense", class = "factor"),
is_cnv = c(NA, NA, FALSE, TRUE, NA, NA, TRUE, FALSE, NA,
NA, FALSE, NA, NA, FALSE, TRUE)), row.names = c(NA, -15L), .Names = c("id",
"gene", "mut_type", "is_cnv"), class = c("tbl_df", "tbl", "data.frame"
))
cnv_dat <- structure(list(id = structure(1:3, .Label = c("0400136_T1", "0400171_T1",
"0400195_T1"), class = "factor"), gene = structure(c(1L, 1L,
1L), .Label = "KRAS", class = "factor"), mut_type = structure(c(NA_integer_,
NA_integer_, NA_integer_), .Label = character(0), class = "factor"),
cnv = structure(c(1L, 1L, 1L), .Label = "High-level amplification", class = "factor"),
is_cnv = c(TRUE, TRUE, TRUE)), row.names = c(NA, -3L), .Names = c("id",
"gene", "mut_type", "cnv", "is_cnv"), class = c("tbl_df", "tbl",
"data.frame"))
cb_palette <- c("Missense" = "#095481",
"Splice site" = "#DE5423",
"Frameshift" = "#179F30",
"Nonsense" = "#F5B02C",
"Nonstop" = "#FF69B4",
"In-frame indel" = "#9C39B6")
mut_levels = c("Missense", "Splice site", "Frameshift", "Nonsense", "Nonstop", "In-frame indel")
我的代码:
library(dplyr); library(readr); library(ggplot2)
p1 <- ggplot(data = satur_muts) +
geom_tile(mapping = aes(x = id, y = gene, fill = mut_type), height=0.9, width=0.9) +
scale_fill_manual(values = cb_palette, na.value = "Grey95", drop = F,
guide = guide_legend(title = "Somatic mutations", override.aes = list(size=4.5)) ,
breaks = unique(satur_muts$mut_type)[order(mut_levels)] %>% na.omit()) +
geom_tile(data = filter(satur_muts, gene == ""),
mapping = aes(x=id, y=gene),
fill = "white",
height = 1, width = 1)
o <- levels(cnv_dat$cnv)
v_tuple <- c("High-level amplification"= 24, "Homozygous deletion" = 25)
b <- c("High-level amplification", "Homozygous deletion")
keep <- which( names(v_tuple) %in% unique(cnv_dat$cnv)[ order(o) ] )
p2 <- p1 +
geom_point(data = cnv_dat, aes(x = id, y = gene, shape = cnv),
fill = "grey", size = 3, stroke = 0.5, inherit.aes = F) +
scale_shape_manual(values = v_tuple[keep],
breaks = b[keep]) +
guides(shape = guide_legend(title = "Somatic Copy Number Aberration"), override.aes = list(size=4.5), drop = T )
p2
根据我下面的数据检查,最后一个蓝色和上三角形瓷砖左侧的两个瓷砖也应该有蓝色背景。我不明白为什么一个样本会正确绘制,而其他样本却不能。
mt <- left_join(satur_muts, cnv_dat) %>% filter(!is.na(is_cnv))
t <- mt %>% filter(gene == "KRAS")
# A tibble: 7 × 5
id gene mut_type is_cnv cnv
<chr> <chr> <chr> <lgl> <fctr>
1 0400136_T1 KRAS Missense FALSE NA
2 0400136_T1 KRAS <NA> TRUE High-level amplification
3 0400171_T1 KRAS <NA> TRUE High-level amplification
4 0400171_T1 KRAS Missense FALSE NA
5 0400179_T1 KRAS Missense FALSE NA
6 0400195_T1 KRAS Missense FALSE NA
7 0400195_T1 KRAS <NA> TRUE High-level amplification
会话信息
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12
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] parallel grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] viridis_0.3.4 RColorBrewer_1.1-2 doMC_1.3.4 iterators_1.0.8 foreach_1.4.3 cowplot_0.7.0 lattice_0.20-34 gridExtra_2.2.1 readxl_0.1.1
[10] forcats_0.2.0 stringr_1.2.0 devtools_1.12.0 dplyr_0.5.0 purrr_0.2.2 readr_1.0.0 tidyr_0.6.2 tibble_1.3.0 ggplot2_2.2.1
[19] tidyverse_1.1.1
loaded via a namespace (and not attached):
[1] Rcpp_0.12.10 plyr_1.8.4 tools_3.3.2 digest_0.6.12 jsonlite_1.4 lubridate_1.6.0 memoise_1.0.0 gtable_0.2.0 nlme_3.1-131 psych_1.6.12
[11] DBI_0.5-1 haven_1.0.0 withr_1.0.2 httr_1.2.1 xml2_1.1.1 hms_0.3 R6_2.2.0 foreign_0.8-67 reshape2_1.4.2 modelr_0.1.0
[21] magrittr_1.5 codetools_0.2-15 scales_0.4.1 assertthat_0.1 mnormt_1.5-5 rvest_0.3.2 colorspace_1.3-2 labeling_0.3 stringi_1.1.5 lazyeval_0.2.0
[31] munsell_0.4.3 broom_0.4.2
和 Rstudio 版本
$version
[1] ‘1.0.136’
【问题讨论】:
-
reproducible example 会有所帮助。否则我们只能尝试猜测您的代码如何与数据集交互。
-
@Z.Lin 我希望这会有所帮助 - 请让我知道是否有什么我可以做的更清楚
-
制作一个 minimal 可重现的示例将使这更容易实现。 (a) 不要将
rm(list = ls())放在您的问题中——这不是必需的,如果人们复制/粘贴您的代码并意外运行它,您会感到难过。 (b) 您的main_plot_sample_order.tsv和main_plot_gen_order不可复制/粘贴。您可以在其中的子集上使用dput(droplevels())以便可以复制/粘贴它们吗? (c) 您正在共享大量数据。你能分享更少的数据并仍然显示说明问题吗?调试具有 3 y 和 3 x 值的绘图通常比具有 12 和 64 x 值的绘图更容易... -
(d) 您有绘图问题,但您共享数据,然后使用代码将其转换为您绘制的数据。为什么不只分享您绘制的数据?看起来我们只需要
satur_muts和cnv_dat的子集。 (e) 你分享了近 20 行只是主题选项的代码。如果它们与问题相关,请提及否则删除它们,因为它们只会分散注意力。 (通过与问题相关,我的意思是如果你删除这些行,问题会消失吗?如果是这样,它们是重现问题所必需的,它缩小了我们应该寻找问题的范围。否则它们是噪音) -
TL;DR,您分享的代码非常长。您可以删除多少内容并仍然说明问题?代码越短,人们就越有可能运行它并回答问题。