【问题标题】:Invert continuous color scale legend in ggplot geom_tile heatmap [duplicate]反转ggplot geom_tile热图中的连续色标图例[重复]
【发布时间】:2021-04-28 16:22:44
【问题描述】:

小例子:

library(ggplot2)
x <- c(1:3)
y <- c(1:3)
data <- expand.grid(X=x, Y=y)
data$Z <- runif(9)
ggplot(data, aes(X, Y, fill=Z)) +
  geom_tile()

产生这个:

如何让右侧的 Z 比例尺从顶部的 0 运行到底部的 1?而不是从下到上?

我试图强调较小的值,如果可能的话,实际上希望它能够与 viridis 或 magma 配色方案一起使用。但是direction=-1 上的scale_fill_viridis 只会翻转色标。想要黄色 = 0,蓝色/黑色 = 1。然后 Z 比例从下到上从蓝色变为黄色。

ggplot(data, aes(X, Y, fill=Z)) +
  geom_tile() +
  scale_fill_viridis(discrete=FALSE, direction=-1)

【问题讨论】:

  • @tjebo 我无法得到任何这些答案。编辑:nvmd,想通了

标签: r ggplot2 legend geom-tile


【解决方案1】:

scale_fill_distiller你可以选择调色板和调色板的方向。

library(ggplot2)
x <- c(1:3)
y <- c(1:3)
data <- expand.grid(X=x, Y=y)
data$Z <- runif(9)


ggplot(data) +
  aes(x = X, y = Y, fill = Z) +
  geom_tile(size = 1L) +
  scale_fill_distiller(palette = "Blues", direction = 1) +
  theme_minimal()

【讨论】:

    【解决方案2】:

    我让它与trans = 'reverse'一起工作

    ggplot(data, aes(X, Y, fill=Z)) +
      geom_tile() + 
      scale_fill_viridis(trans = 'reverse', option="plasma")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 2017-01-16
      • 1970-01-01
      • 2018-05-09
      相关资源
      最近更新 更多