【问题标题】:R: how to assign colours to specific valuesR:如何将颜色分配给特定值
【发布时间】:2019-03-24 15:55:09
【问题描述】:

我有几个 10x10 矩阵,其中填充了五个值,即:0、0.5、1、1.5 和 2。并非所有矩阵都具有所有这些值。

我想绘制具有相同颜色的相同值总是。我有五种特定颜色要链接到特定值,即:

  • 0: "白色
  • 0.5:“浅蓝色”
  • 1:“蓝色”
  • 1.5:“lightpink1”
  • 2:“红色”

目前,代码如下所示:

example_mat <- matrix(sample(c(0,0.5,1,1.5,2),100, replace = TRUE), nrow = 10, ncol = 10)

# make rotate function for plot
rotate <- function(x) t(apply(x, 2, rev))

# plot 
image(rotate(example_mat), col=c("white","light blue","blue","lightpink1","red"), xaxt= "n", yaxt= "n")
grid(nx = 10, ny = 10, col = "black", lty = "solid")
box(which = "plot", lty = "solid")

这产生了以下情节:

这似乎工作得很好,除了我有很多这些图,有时矩阵中并非所有 5 个值(0、0.5、1、1.5 和 2)都存在,然后他为价值观。如何使该图对于相同的值始终具有相同的颜色?

【问题讨论】:

    标签: r colors


    【解决方案1】:

    使用基数 R,image 函数有一个 zlim 参数来完成这项工作。

    set.seed(42)
    example_mat1 <- matrix(sample(c(0,0.5,1,1.5,2),100, replace = TRUE), nrow = 10, ncol = 10)
    example_mat2 <- example_mat1
    example_mat2[example_mat2 == 2] = 0 # remove one of the values
    
    # make rotate function for plot
    rotate <- function(x) t(apply(x, 2, rev))
    
    # plot 
    image(rotate(example_mat1), col=c("white","light blue","blue","lightpink1","red"), zlim=c(0,2), xaxt= "n", yaxt= "n")
    grid(nx = 10, ny = 10, col = "black", lty = "solid")
    box(which = "plot", lty = "solid")
    
    image(rotate(example_mat2), col=c("white","light blue","blue","lightpink1","red"), zlim=c(0,2), xaxt= "n", yaxt= "n")
    grid(nx = 10, ny = 10, col = "black", lty = "solid")
    box(which = "plot", lty = "solid")
    

    这使得下面的情节......

    【讨论】:

      【解决方案2】:

      一种方法是使用ggplot 并根据每个图块的值预定义填充颜色。

      #Define colors using named vector
      mycols = c(white = 0, lightblue = 0.5, blue = 1, pink = 1.5, red = 2)
      
      #DATA
      set.seed(42)
      example_mat = matrix(sample(c(0,0.5,1,1.5,2),100, replace = TRUE), nrow = 10, ncol = 10)
      
      rotate <- function(x) t(apply(x, 2, rev))
      m1 = rotate(example_mat)
      
      library(ggplot2)
      
      #Convert to long format
      d = data.frame(Values = as.vector(m1),
                     fillcol = names(mycols[match(m1, mycols)]),
                     X = as.vector(row(m1)),
                     Y = as.vector(col(m1)))
      
      graphics.off()
      ggplot(d, aes(x = X, y = Y)) +
          geom_tile(color = "black", fill = d$fillcol) +
          theme_light() +
          coord_equal()
      

      【讨论】:

        【解决方案3】:

        我会用几个dplyrtidyr 函数来处理这个问题,以创建一个适合ggplot2 形状的数据框,然后使用ggplot2 的磁贴。我将值列视为一个字符,以使其显式离散。如果您有更多值,则可以改用cut 来分解因子水平。

        首先,我已经制作了将进入scale_fill_manual 的颜色命名向量——颜色是向量元素,对应的数字是名称,正如比例尺values 参数的预期设置一样。

        library(ggplot2)
        library(dplyr)
        library(tidyr)
        
        set.seed(324)
        example_mat <- matrix(sample(c(0,0.5,1,1.5,2),100, replace = TRUE), nrow = 10, ncol = 10)
        
        colors <- c("white", "lightblue", "blue", "lightpink1", "red") %>%
           setNames(c(0, 0.5, 1, 1.5, 2))
        

        然后,为了重塑数据以进行绘图,我制作了一个数据框,添加行号,并使用gather 转换为长形状。自动列名称为V1V2、...,因此我使用正则表达式\\d+ 提取该文本的数字部分以获取列号。

        rotate <- function(x) t(apply(x, 2, rev))
        mat_long <- rotate(example_mat) %>%
           as_tibble() %>%
           mutate(row = row_number()) %>%
           gather(key = col, value = value, -row) %>%
           mutate(col = stringr::str_extract(col, "\\d+") %>% as.numeric()) 
        
        mat_long
        #> # A tibble: 100 x 3
        #>      row   col value
        #>    <int> <dbl> <dbl>
        #>  1     1     1   1  
        #>  2     2     1   1.5
        #>  3     3     1   1.5
        #>  4     4     1   2  
        #>  5     5     1   1.5
        #>  6     6     1   0.5
        #>  7     7     1   0  
        #>  8     8     1   1  
        #>  9     9     1   0.5
        #> 10    10     1   0.5
        #> # … with 90 more rows
        

        然后绘图相当简单,将值视为字符,并为填充指定命名的调色板。

        ggplot(mat_long, aes(x = col, y = row, fill = as.character(value))) +
           geom_tile(color = "black") +
           scale_fill_manual(values = colors) +
           theme_void()
        

        reprex package (v0.2.1) 于 2019 年 3 月 24 日创建

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-08-27
          • 2021-09-18
          • 2015-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多