【问题标题】:How do I limit the range of the viridis colour scale?如何限制 viridis 色标的范围?
【发布时间】:2018-01-24 14:20:29
【问题描述】:

我有两组数据,我想使用带有 viridis 色标的热图来呈现它们。对于第一个数据集,我的值范围从 0 到 1.2,我可以很容易地看到我想看到的差异。但是我的第二个数据集有一些异常值,导致范围从 0 到 2。现在更难看到 0 和 1 之间有趣范围内的差异,直接比较这两个图像更加困难。是否有可能使用 viridis 色阶显示从 0 到 1.2 的数据,同时以黄色显示更高的值(viridis 色阶的“最高”颜色)? 这是一个例子:

library(viridis)

#Create Data
DataSet1 <- expand.grid(x = 0:5, y = 0:5)
DataSet1$z <- runif(36, 0, 1.2)

DataSet2 <- expand.grid(x = 0:5, y = 0:5)
DataSet2$z <- runif(36, 0, 2)

#Plot Data
ggplot(DataSet1, aes(x, y, fill = z)) + 
  geom_tile() +
  scale_fill_viridis() +
  geom_text(aes(label = round(z, 2)), size = 2)

DataSet1:0.5 和 0.7 的区别很容易看出

ggplot(DataSet2, aes(x, y, fill = z)) + 
  geom_tile() +
  scale_fill_viridis() +
  geom_text(aes(label = round(z, 2)), size = 2)

DataSet2:0.5 和 0.7 之间的差异很难看出

【问题讨论】:

  • ggplot2::scale_fill_gradient中设置限制怎么样?

标签: r ggplot2 viridis


【解决方案1】:

@ClausWilke 的解决方案更好,因为它显示在图例中,但有时只需要快速解决方案而无需编写太多特定代码。这个也依赖于scales

ggplot(DataSet2, aes(x, y, fill = z)) + 
  geom_tile() +
  scale_fill_viridis(limits = c(0.2, 1), oob = scales::squish) +
  geom_text(aes(label = round(z, 2)), size = 2)

【讨论】:

    【解决方案2】:

    您可以定义任意重新缩放功能。不确定这看起来有多棒,可能需要对图例进行一些处理,但原则上,这种机制允许您以任何您想要的方式将数据值映射到比例上。

    ggplot(DataSet2, aes(x, y, fill = z)) + 
      geom_tile() +
      scale_fill_viridis(rescaler = function(x, to = c(0, 1), from = NULL) {
        ifelse(x<1.2, 
               scales::rescale(x,
                               to = to,
                               from = c(min(x, na.rm = TRUE), 1.2)),
               1)}) +
      geom_text(aes(label = round(z, 2)), size = 2)
    

    【讨论】:

      【解决方案3】:

      你在寻找这样的东西吗?

      ggplot(DataSet2, aes(x, y, fill = z)) + 
        geom_tile() +
        scale_fill_gradient(low="green", high="red", limits=c(0, 1.2), 
                            na.value = "yellow") +
        geom_text(aes(label = round(z, 2)), size = 2)
      

      使用 viridis 颜色,根据 jazzurro 的建议。

      ggplot(DataSet2, aes(x, y, fill = z)) + 
        geom_tile() + 
        scale_fill_gradientn(colors = viridis_pal()(9), limits=c(0, 1.2), 
                             na.value = "#FDE725FF") + 
        geom_text(aes(label = round(z, 2)), size = 2)
      

      【讨论】:

      • 你想在你的答案中使用 viridis 颜色。我认为这样的事情会做。 ggplot(DataSet2, aes(x, y, fill = z)) + geom_tile() + scale_fill_gradientn(colors = viridis_pal()(9), limits=c(0, 1.2), na.value = "#FDE725FF") + geom_text(aes(label = round(z, 2)), size = 2)
      • 这适用于我在这里提出的问题。不幸的是,我的数据还包含以前以灰色显示的其他 NA 值。现在它们也将显示为黄色。
      【解决方案4】:

      这不一定是一种改进,但您可以这样做以用黄色显示较高的值:

      DataSet2A <- DataSet2 %>% filter(z <= 1.2)
      DataSet2B <- DataSet2 %>% filter(z > 1.2)
      
      ggplot(DataSet2A, aes(x, y, fill = z)) +
        geom_tile() +
        scale_fill_viridis(begin = 0, end = .75) +
        geom_text(aes(label = round(z, 2)), size = 2) +
        geom_tile(data = DataSet2B, aes(x, y), fill = "yellow")
      

      也许如果您使用截止以及音阶中的begin=end= 参数来控制您正在使用的viridis 音阶的部分,您可以获得您想要的结果. (请注意,每个绘图只能有一个填充比例,但您可以设置额外的恒定填充,就像我在此处使用黄色所做的那样。)

      【讨论】:

        猜你喜欢
        • 2017-08-01
        • 2012-09-20
        • 2019-09-22
        • 1970-01-01
        • 2020-10-29
        • 1970-01-01
        • 2011-04-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多