r - 如何修复ggplot连续颜色范围

标签 r ggplot2 tidyverse

我无法根据热图的值来修复它们的颜色。相同的值应该具有相同的颜色。目标是将所有值保持在某个阈值 (0.05) 以下(恒定)灰色。对于大于此阈值的值,颜色应逐渐从“firebrick1”变为“firebrick4”。

例如,如果我使用变量 utilization2,“Plant 5”/“202004”= 70.6 为红色,如果我使用变量 utilization,则为灰色。我该如何解决这个问题?

library(tidyverse)
library(rlang)

MONTHS <- str_c("2020", sprintf("%02d", 1:12))
PLANTS <- str_c("Plant ", 1:5)

crossing(month = MONTHS, plant = PLANTS) %>%
  mutate(utilization = runif(nrow(.), 70, 100)) %>%
  mutate(utilization2 = if_else(plant == "Plant 2", utilization * 0.67, utilization)) -> d

draw_plot <- function(fill) {
  fill <- ensym(fill)
   d %>%
    ggplot(mapping = aes(x = month, y = plant, fill = !!fill)) +
    geom_tile(aes(width = 0.85, height = 0.85)) +
    geom_text(aes(label = round(!!fill, 1)), color = "white") +
    scale_x_discrete(expand=c(0,0)) +
    scale_y_discrete(expand=c(0,0)) +
    scale_fill_gradientn(colours = c("darkgray", "firebrick1", "firebrick4"), 
                         values = c(0, 0.05, 1)) +
    labs(x = "Month", y = "Production plant", title = str_c("fill = ", fill), color = "Utilization") +
    theme_light() +
    theme(legend.position = "none")
}
draw_plot(utilization)
draw_plot(utilization2)

enter image description here

enter image description here

最佳答案

library(tidyverse)
library(rlang)

MONTHS <- str_c("2020", sprintf("%02d", 1:12))
PLANTS <- str_c("Plant ", 1:5)

crossing(month = MONTHS, plant = PLANTS) %>%
    mutate(utilization = runif(nrow(.), 70, 100)) %>%
    mutate(utilization2 = if_else(plant == "Plant 2", utilization * 0.67, utilization)) -> d

draw_plot <- function(fill) {
    fill <- ensym(fill)
    d %>%
        ggplot(mapping = aes(x = month, y = plant, fill = !!fill)) +
        geom_tile(aes(width = 0.85, height = 0.85)) +
        geom_text(aes(label = round(!!fill, 1)), color = "white") +
        scale_x_discrete(expand=c(0,0)) +
        scale_y_discrete(expand=c(0,0)) +
        scale_fill_gradientn(colours = c("darkgray", "firebrick1", "firebrick4"),
                             values = c(0, 0.05, 1), limits = c(min(d$utilization, d$utilization2), max(d$utilization, d$utilization2))) +
        labs(x = "Month", y = "Production plant", title = str_c("fill = ", fill), color = "Utilization") +
        scale_color_identity() +
        theme_light() +
        theme(legend.position = "none")
}
draw_plot(utilization)
draw_plot(utilization2)

重点是,scale_fill_gradientn() 将比例限制设置为感兴趣向量的最大值和最小值。您必须手动设置它们。在本例中,我选择了两列的最大值和最小值 (limits = c(min(d$utilization, d$utilization2), max(d$utilization, d$utilization2)))。

关于r - 如何修复ggplot连续颜色范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59893606/

相关文章:

r - 如何使用 sliderInput 在 R Shiny 中输入超过 2 个值

R带有data.table的多个列的多个统计信息

r - 在图例中显示不同的几何图形以实现共同的美感

r - 如何在插值数据图的边缘绘制原始数据的直方图

r - 如何更改 ggplot2 中的轴标签颜色?

r - 选择除一列之外的所有重复行

r - 格式化自定义摘要输出以匹配 R 中的 ANOVA 输出

使用 tidyr::nest() 时保留 dplyr::group_by 列

r - 如何修复 R 函数中的 'Quosures can only be unquoted within a quasiquotation context' 错误

r - R 中是否有一个函数可以完成 C 中 atoi() 的功能?