在 geom_tile() 热图上更换刹车

Changing breaks on a geom_tile() heatmap

我正在处理以下数据框:

structure(c(NA, 52, 22, 52, 111, 0, 3, 16, 5, 0, 0, 80, NA, 14, 
243, 14, 41, 1, 177, 3, 0, 20, 26, 8, NA, 13, 21, 10, 0, 0, 0, 
0, 0, 59, 29, 7, NA, 15, 20, 0, 8, 8, 0, 0, 111, 9, 273, 1733, 
NA, 60, 3, 14, 164, 0, 8, 20, 171, 53, 932, 135, NA, 0, 33, 3, 
0, 144, 21, 0, 3, 1, 2, 0, NA, 1, 0, 0, 0, 23, 81, 14, 146, 20, 
19, 5, NA, 1, 1, 0, 11, 4, 4, 8, 11, 0, 2, 12, NA, 0, 0, 1, 0, 
4, 32, 0, 16, 0, 3, 0, NA, 0, 0, 40, 2, 1, 0, 0, 0, 1, 5, 0, 
NA), .Dim = c(11L, 11L), .Dimnames = list(c("WILL_", "WOULD_", 
"MAY_", "MIGHT_", "CAN_", "COULD_", "SHALL_", "SHOULD_", "MUST_", 
"OUGHT TO_", "USED TO_"), c("_WILL", "_WOULD", "_MAY", "_MIGHT", 
"_CAN", "_COULD", "_SHALL", "_SHOULD", "_MUST", "_OUGHT TO", 
"_USED TO")))

我有以下情节:

reshape2::melt(twitter_us_no_clit_matrix_nohave, value.name = "Freq") %>%
  mutate(label = ifelse(is.na(Freq) | Freq == 0, "", as.character(Freq))) %>%
  ggplot(aes(Var2, fct_rev(Var1))) +
  geom_tile(aes(fill = Freq), color = "black") +
  geom_text(aes(label = label), color = "black") +
  scale_fill_gradient(low = "grey", high = "purple", na.value = "black") +
  scale_x_discrete(NULL, expand = c(0, 0), position="top") +
  scale_y_discrete(NULL, expand = c(0, 0)) +
  theme(axis.text.x = element_text(angle=60,vjust = 0.5, hjust = 0)) 

一切看起来都不错,但我正在尝试弄清楚如何更改中断,以便它相应地更改热图及其“Freq”标签。

中断将是 c(0,1,5,10,50,100,500,100000) 并且会使值在色热方面看起来或多或少类似于以下内容:

我应该在我的 geom_tile() 中调整哪些确切的行?

如果我没理解错的话,你想把你的连续刻度分成几步。我 had some success 使用 scale_fill_steps() 函数:

library(tidyverse)

twitter_us_no_clit_matrix_nohave <- structure(c(NA, 52, 22, 52, 111, 0, 3, 16, 5, 0, 0, 80, NA, 14, 
            243, 14, 41, 1, 177, 3, 0, 20, 26, 8, NA, 13, 21, 10, 0, 0, 0, 
            0, 0, 59, 29, 7, NA, 15, 20, 0, 8, 8, 0, 0, 111, 9, 273, 1733, 
            NA, 60, 3, 14, 164, 0, 8, 20, 171, 53, 932, 135, NA, 0, 33, 3, 
            0, 144, 21, 0, 3, 1, 2, 0, NA, 1, 0, 0, 0, 23, 81, 14, 146, 20, 
            19, 5, NA, 1, 1, 0, 11, 4, 4, 8, 11, 0, 2, 12, NA, 0, 0, 1, 0, 
            4, 32, 0, 16, 0, 3, 0, NA, 0, 0, 40, 2, 1, 0, 0, 0, 1, 5, 0, 
            NA), .Dim = c(11L, 11L), .Dimnames = list(c("WILL_", "WOULD_", 
                                                        "MAY_", "MIGHT_", "CAN_", "COULD_", "SHALL_", "SHOULD_", "MUST_", 
                                                        "OUGHT TO_", "USED TO_"), c("_WILL", "_WOULD", "_MAY", "_MIGHT", 
                                                                                    "_CAN", "_COULD", "_SHALL", "_SHOULD", "_MUST", "_OUGHT TO", 
                                                                                    "_USED TO")))

breaks <- c(0,1,5,10,50,100,500,100000)

reshape2::melt(twitter_us_no_clit_matrix_nohave, value.name = "Freq") %>%
  mutate(label = ifelse(is.na(Freq) | Freq == 0, "", as.character(Freq))) %>%
  ggplot(aes(Var2, fct_rev(Var1))) +
  geom_tile(aes(fill = Freq), color = "black") +
  geom_text(aes(label = label), color = "black") +
  scale_fill_steps(low = "grey", high = "purple", breaks = breaks, na.value = "black") + 
  scale_x_discrete(NULL, expand = c(0, 0), position="top") +
  scale_y_discrete(NULL, expand = c(0, 0)) +
  theme(axis.text.x = element_text(angle=60,vjust = 0.5, hjust = 0)) 

但是,考虑到休息时间之间的距离不同,大部分比例尺都是灰色的。我不知道你是否想保留那个观点。

要重新调整颜色,请将行更改为

scale_fill_steps(low = "grey", high = "purple", breaks = breaks, na.value = "grey", trans = "log")

这将使它们 evenly distributed 达到您的体重秤。我没有成功地手动设置它们。我更改了 NA 值的颜色,因为由于对数转换,现在所有的零都是 NAs。