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’
答案 0 :(得分:2)
我注意到,通过这个练习制作一个代表传递satur_muts
,一个包含非唯一(x,y)配对的表格,导致ggplot到&#34;猜测&#34 ;在哪个配对使用,即使指定了单一美学(按照上面的代码)。
首先获取适当的输入数据以制作图块背景(satur_muts
):
genes <- structure(c(2L, 3L, 1L), .Label = c("CDKN2A", "KRAS", "TP53"), class = "factor")
ids <- structure(1:4, .Label = c("0400136_T1", "0400171_T1", "0400179_T1",
"0400195_T1"), class = "factor")
the_muts <- structure(list(id = c("0400136_T1", "0400171_T1", "0400179_T1",
"0400195_T1"), gene = c("KRAS", "KRAS", "KRAS", "KRAS"), mut_type = structure(c(1L,
1L, 1L, 1L), .Label = "Missense", class = "factor"), is_cnv = c(FALSE,
FALSE, FALSE, FALSE), cnv = c("No High-level CNV", "No High-level CNV",
"No High-level CNV", "No High-level CNV")), row.names = c(NA,
-4L), .Names = c("id", "gene", "mut_type", "is_cnv", "cnv"), class = c("tbl_df",
"tbl", "data.frame"))
制作正确的satur_muts
:
library(dplyr)
satur_muts_expand <- expand.grid(gene = genes, id = ids) %>% as_data_frame()
new_satur_muts <- left_join(satur_muts_expand, the_muts %>% filter(!is.na(mut_type), !is_cnv) %>% distinct(), by = c("gene", "id"))
不:
satur_muts <- left_join(satur_muts_expand, the_muts, by = c("gene", "id"))
你应该得到这个:
new_satur_muts <- structure(list(gene = c("KRAS", "TP53", "CDKN2A", "KRAS", "TP53",
"CDKN2A", "KRAS", "TP53", "CDKN2A", "KRAS", "TP53", "CDKN2A"),
id = c("0400136_T1", "0400136_T1", "0400136_T1", "0400171_T1",
"0400171_T1", "0400171_T1", "0400179_T1", "0400179_T1", "0400179_T1",
"0400195_T1", "0400195_T1", "0400195_T1"), mut_type = structure(c(1L,
NA, NA, 1L, NA, NA, 1L, NA, NA, 1L, NA, NA), .Label = "Missense", class = "factor")), row.names = c(NA,
-12L), .Names = c("gene", "id", "mut_type"), class = c("tbl_df", "tbl","data.frame"))
并使用来自OP和cnv_dat
的{{1}},运行与上述相同的代码可解决问题(通过一些satur_muts <- new_satur_muts
调整和theme
) < / p>
基本上,如果您尝试在同一个图块中绘制多个美学(颜色,符号等),就会出现此问题。所以在这个例子中,我必须确保每个样本 - 基因配对在用于绘制突变(颜色)的表中是唯一的,然后覆盖附加信息(cnv)是一个SEPARATE表。所以事实证明这不是一个图形问题,而是在绘图之前包含输入数据的质量。感谢大家的帮助!