簡體   English   中英

R熱圖:使用(ggplot2或按圖)有條件地更改標簽文本的顏色

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

我試圖生產具有GGPLOT2plotly在R,其中與塊或瓦片相關聯的值被用作在相應的瓦標簽熱圖 這並不是那么困難,但是我刪除了圖例,並希望根據標簽的值更改標簽的顏色以增加其可見性。

這里有一個可重現的例子來說明我的意思。

數據(使用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和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")
         )

我在這里找到了一個很好的例子,但是我無法為我的案子工作。 這是我的代碼的注釋部分:

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")

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM