简体   繁体   English

R热图:使用(ggplot2或按图)有条件地更改标签文本的颜色

[英]R Heatmap: conditionally change label text colours with (ggplot2 or plotly)

I am trying to produce a heatmap with ggplot2 or plotly in R, where the values associated with a block or tile are used as labels in the respective tile. 我试图生产具有GGPLOT2plotly在R,其中与块或瓦片相关联的值被用作在相应的瓦标签热图 This was not so difficult, but I have removed the legend and would like to change the colours of the labels conditional on their values to increase their visibility. 这并不是那么困难,但是我删除了图例,并希望根据标签的值更改标签的颜色以增加其可见性。

Here a reproducible examples to show what I mean. 这里有一个可重现的例子来说明我的意思。

Data (using data.table and dplyr): 数据(使用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()

The heatmap (with ggplot2): 热图(使用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")

This results in 这导致

在此处输入图片说明

The darker a block becomes the less visible the text becomes. 块越黑,文本越不可见。 To prevent this, my intention is to make the text white when a value is below 50 and black otherwise. 为防止这种情况,我的目的是在值小于50时将文本设置为白色,否则将其设置为黑色。 This is the part where I failed both with ggplot2 and plotly until now and would be grateful for help. 这是我直到现在都没有使用ggplot2和plotly失败的部分,非常感谢您的帮助。

With plotly: 与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")
         )

I found a great plotly example here , but I couldn't manage to get to work for my case. 我在这里找到了一个很好的例子,但是我无法为我的案子工作。 Here the annotation part of my code: 这是我的代码的注释部分:

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)

Here, one of numerous attempts with ggplot2: 在这里,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")

(This code produces the plot in the image above if the second to last line is removed.) (如果删除倒数第二行,此代码将在上图中生成绘图。)

I'm pretty stuck on this one... Thanks in advance for any advice! 我非常喜欢这个……谢谢您的任何建议!

With ggplot2, you can use colour in the aes of geom_text (+ scale_colour_manual ): 使用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")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM