R heatmap:有条件地使用(ggplot2或ploly)更改标签文本颜色 [英] R Heatmap: conditionally change label text colours with (ggplot2 or plotly)

查看:18
本文介绍了R heatmap:有条件地使用(ggplot2或ploly)更改标签文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在R中使用ggplot2ploly生成热图,其中与块或磁贴相关联的值用作各自磁贴中的标签。这并不难,但我已经删除了图例,并希望根据标签的值更改标签的颜色,以增加其可见性。

这里有一些可重复使用的例子来说明我的意思。

data(使用data.table和dplyr):

sig <-  rep(c("sig1", "sig2", "sig3"), 100, replace = TRUE, prob = c(0.4, 0.35, 0.25))
date <- c("2019-11-01", "2019-11-02", "2019-11-03")

another <- as.data.table(expand.grid(sig, date))

test_dat_numerics <- another[, number_ok := sample(0:100, 900, replace = TRUE)]
setnames(test_dat_numerics, c("Var1", "Var2"), c("sig", "date"))

test_dat_numerics <- test_dat_numerics[, avg := mean(number_ok), by = .(date, sig)] %>%
  dplyr::select(-number_ok) %>%
  dplyr::rename(number_ok = avg) %>%
  dplyr::mutate(prop = ifelse(number_ok > 50, 1, 0))
  dplyr::distinct()

热图(带ggplot2):

ggp <- ggplot(test_dat_numerics, aes(date, sig, fill = number_ok)) +
  geom_tile() +
  geom_text(aes(label = test_dat_numerics$number_ok)) +
  theme(legend.position="none")

这将导致

块变得越暗,文本就越不明显。为了防止出现这种情况,我的目的是当值低于50时将文本设置为白色,否则将文本设置为黑色。到目前为止,这就是我在ggplot2和PLOTHY方面都失败的地方,如果能得到帮助,我将不胜感激。

与PLOTLY:

p <- test_dat_numerics %>%
  plot_ly(type = "heatmap",
          x = ~date,
          y = ~sig,
          z = ~number_ok,
          # zmax = 100,
          # zmin = 0,
          showscale = FALSE,
          colorscale = "Blues") %>%
  add_annotations(text = as.character(test_dat_numerics$number_ok),
                  showarrow = FALSE,
                  color = list(if (test_dat_numerics$number_ok[i] > 50) {"black"} else {"white"})) %>%
  layout(title = "Test Heatmap",
         # titlefont = t,
         xaxis = list(title = "Datum"), yaxis = list(title = "Signal")
         )

我找到了一个很好的情节示例here,但我无法为我的案例工作。下面是我代码的注释部分:

ann <- list()

    for (i in 1:length(unique(test_dat_numerics$sig))) {
      for (j in 1:length(unique(test_dat_numerics$date))) {
        for (k in 1:(length(unique(test_dat_numerics$sig))*length(unique(test_dat_numerics$date)))) {
          ann[[k]] <- list(
          x = i,
          y = j,
          font = list(color = if (test_dat_numerics$number_ok[i] > 50) {"black"} else {"white"}),
          text = as.character(test_dat_numerics$number_ok[[k]]),
          xref = "x", 
          yref = "y", 
          showarrow = FALSE )
        }
      }
    }

p_test_num_heat <- layout(p, annotations = ann)

此处是ggplot2的众多尝试之一:

ggp <- ggplot(test_dat_numerics, aes(date, sig, fill = number_ok)) +
  geom_tile() +
  geom_text(aes(label = test_dat_numerics$number_ok)) +
  geom_label(aes(colour = factor(test_dat_numerics$prop))) +
  theme(legend.position="none")

(如果删除倒数第二行,此代码将生成上图中的绘图。)

我对这件事很纠结……提前感谢您的建议!

推荐答案

使用ggplot2可以在geom_text(+scale_colour_manual)的aes中使用colour

ggplot(test_dat_numerics, aes(date, sig, fill = number_ok)) +
  geom_tile() +
  geom_text(aes(label = number_ok, colour =ifelse(number_ok>50, "black", "white"))) +
  scale_colour_manual(values=c("white"="white", "black"="black")) +
  theme(legend.position="none")

这篇关于R heatmap:有条件地使用(ggplot2或ploly)更改标签文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆