R语言 根据图中特定区域的接近程度,使用切片的渐变绘制热点图

yfwxisqw  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(118)

我的目标是使用热图绘制下面的数据。我已经获得了所需热图的布局,但我正在努力寻找一种方法来按预期对图块进行着色。我猜测下面的代码对图块的着色与图块本身内部的数字成比例。然而,这不是我所需要的。我希望:
1.在每个矩形内具有相同的颜色(左侧矩形为黄色,右侧矩形为蓝色);
1.对于其它块,从黄色到蓝色的梯度与块离蓝色区域的距离成比例。
感谢任何愿意帮忙的人!

library(tidyverse)

# First, I create the simulated dataset with 200 individuals
set.seed(1243)  # set seed for reproducibility

#I simulate test 1 scores
test1_score <- sample(c(3.5, 3.8, 4), 200, replace = TRUE)

#I simulate test 2 scores
test2_score <- round(runif(200, 0, 38))

#I create diagnostic classes based on test 2 scores
test2_class <- ifelse(test2_score < 20, "non meet",
                      ifelse(test2_score < 30, "below 40th",
                             ifelse(test2_score < 35, "above 40th",
                                    ifelse(test2_score < 37, "meet", "master"))))

#I create exit_program variable based on test 1 scorea and test 2 classes
exit_program <- ifelse(test1_score == 4 & test2_class %in% c("above 40th", "meet", "master"), 1, 0)

#I create data frame with simulated data
df <- data.frame(ID = 1:200, test1_score, test2_class, exit_program)

#I calculate the group size for each combination of test 1 scores and test 2 classes
df2 <- df |>
  group_by(test1_score, test2_class, exit_program) |> 
  summarize(n = n()) |> 
  mutate(test2_class = factor(test2_class, levels = c("non meet", "below 40th", "above 40th", "meet", "master"))) |> arrange(test2_class)

#plot data
df2 |> 
  ggplot(aes(x = test2_class, y = as.factor(test1_score))) +
  theme(legend.position = "none", panel.background = element_rect(fill = "white"),
        axis.text = element_text(size = 12), axis.title = element_text(size = 14)) +
  geom_tile(aes(fill = n)) + 
  geom_text(aes(label = n), size = 7, family = "sans") +
  labs(x = "classification", y = "scores", size = 10) +
  scale_fill_gradient(low = "#56B4E9", high = "#F0E442", guide = "none") +
  geom_rect(aes(xmin = 2.5, xmax = 5.5, ymin = 2.5, ymax = 3.5), linewidth = 2, color = "#0072B2", fill = NA) +
  geom_rect(aes(xmin = 0.5, xmax = 1.5, ymin = 0.5, ymax = 3.5), linewidth = 2, color = "#E69F00", fill = NA)

5uzkadbs

5uzkadbs1#

也许这就是你要找的,你是对的,把nMap到fill上意味着用n的值给瓷砖上色。
就我理解的问题,你希望每一列都有相同的颜色,并且颜色从左边的黄色到右边的蓝色渐变,为此你可以添加一列,列中包含从左端或右端到数据的距离度量,然后将其Map到fill aes上。一种选择是将您的test2_class转换为数字,然后计算与最大值(对应于“主”级别)的绝对距离。

library(tidyverse)

df2 <- df2 |>
  ungroup() |>
  mutate(
    fill = as.numeric(test2_class),
    fill = abs(fill - max(fill))
  )

#plot data
df2 |> 
  ggplot(aes(x = test2_class, y = as.factor(test1_score))) +
  theme(legend.position = "none", panel.background = element_rect(fill = "white"),
        axis.text = element_text(size = 12), axis.title = element_text(size = 14)) +
  geom_tile(aes(fill = fill)) + 
  geom_text(aes(label = n), size = 7, family = "sans") +
  labs(x = "classification", y = "scores", size = 10) +
  scale_fill_gradient(low = "#56B4E9", high = "#F0E442", guide = "none") +
  geom_rect(aes(xmin = 2.5, xmax = 5.5, ymin = 2.5, ymax = 3.5), linewidth = 2, color = "#0072B2", fill = NA) +
  geom_rect(aes(xmin = 0.5, xmax = 1.5, ymin = 0.5, ymax = 3.5), linewidth = 2, color = "#E69F00", fill = NA)

相关问题