2017-09-14 59 views
4

下面我構建了一個Upset plot。我使用調色板來定義條形顏色。有沒有辦法做到這一點連接點的矩陣呢?在UpSetR中爲矩陣點使用調色板

library(dplyr) 
library(RColorBrewer) 
library(UpSetR) 

movies <- read.csv(system.file("extdata", "movies.csv", 
        package = "UpSetR"), header=T, sep=";") 
movies <- select(movies, Action:Children) 

upset(movies, main.bar.color=brewer.pal(2^ncol(movies)-1, "Set1")) 

enter image description here

當試圖調色板適用於矩陣,我得到警告,只有第一個顏色,紅色,被使用。

upset(movies, main.bar.color=brewer.pal(2^ncol(movies)-1, "Set1"), 
     matrix.color=brewer.pal(2^ncol(movies)-1, "Set1")) 

enter image description here

回答

1

upset允許指定只有一種顏色爲matrix.color
一種解決方案是修改UpSetR:::Create_layout功能:

Create_layout <- function (setup, mat_color, mat_col, matrix_dot_alpha) 
{ 
    Matrix_layout <- expand.grid(y = seq(nrow(setup)), x = seq(ncol(setup))) 
    Matrix_layout <- data.frame(Matrix_layout, value = as.vector(setup)) 
    for (i in 1:nrow(Matrix_layout)) { 
     if (Matrix_layout$value[i] > as.integer(0)) { 
      # Here I propose to change Matrix_layout$color[i] <- mat_color with 
      # Matrix_layout$color[i] <- mat_color[i] 
      Matrix_layout$color[i] <- mat_color[i] 
      Matrix_layout$alpha[i] <- 1 
      Matrix_layout$Intersection[i] <- paste(Matrix_layout$x[i], 
       "yes", sep = "") 
     } 
     else { 
      Matrix_layout$color[i] <- "gray83" 
      Matrix_layout$alpha[i] <- matrix_dot_alpha 
      Matrix_layout$Intersection[i] <- paste(i, "No", sep = "") 
     } 
    } 
    if (is.null(mat_col) == F) { 
     for (i in 1:nrow(mat_col)) { 
      mat_x <- mat_col$x[i] 
      mat_color <- as.character(mat_col$color[i]) 
      for (i in 1:nrow(Matrix_layout)) { 
       if ((Matrix_layout$x[i] == mat_x) && (Matrix_layout$value[i] != 
        0)) { 
        Matrix_layout$color[i] <- mat_color 
       } 
      } 
     } 
    } 
    return(Matrix_layout) 
} 

# Replace Create_layout in UpSetR with the modified function 
assignInNamespace(x="Create_layout", value=Create_layout, ns="UpSetR") 

# Now you can set colors for the matrix of connected dots 
# The dimension of this matrix is 3 x 7 
upset(movies, main.bar.color=brewer.pal(2^ncol(movies)-1, "Set1"), 
     matrix.color=rainbow(21)) 

enter image description here