我有一個包含基因及其相關顏色的字符向量:
gene_colors<-c("protein_coding"="#1F78B4", "lncRNA"="#de08a0")
我正在嘗試查看另一個基因列表,并添加一個隨機顏色的基因,如果它還沒有在載體中:
library(tidyverse)
library(randomcoloR)
for(gene in other_genes){
if(!(gene %in% names(gene_colors))){
temp<-paste0(gene, '=', randomColor(1))
}
}
這是other_genes
中的內容:
[1] "IG_C_gene" "IG_C_pseudogene"
[3] "IG_J_gene" "IG_V_gene"
[5] "IG_V_pseudogene" "lncRNA"
[7] "miRNA" "misc_RNA"
[9] "Mt_rRNA" "polymorphic_pseudogene"
[11] "processed_pseudogene" "protein_coding"
正如您所看到的,我嘗試使用paste0()
,之前我嘗試使用str_c()
,但這兩種方法都給了我一個類似"IG_C_gene=#ffd4bf"
的字符串。我想在熱圖函數中使用gene_colors
向量,所以我需要等號與gene_colors
中的條目一樣分開(即不在引號內,如果它是字符串中的字符)。有沒有辦法做到這一點?
這可以通過如下方式解決:
Data: