【发布时间】:2021-05-02 22:51:18
【问题描述】:
我正在尝试为 ggplot 点图设置色标,划分区域:不合适、合适、理想、合适、不合适。在您的标准 ggplot 代码包装器中,我尝试过 gradient2
scale_colour_gradient2(low = "grey",
mid = "red",
high = "grey",
midpoint = 25) +
和gradientn;
scale_colour_gradientn(colours = rev(rainbow(5)),
breaks = seq(0, 30, by = 5),
limits = c(0, 30),
labels = as.character(seq(0, 30, by = 5))) +
stepsn,
scale_colour_stepsn(colours = c("grey", "blue", "red", "blue", "grey"),
values = scales::rescale(c(0, 22, 25, 26, 29, 40))) +
和binned
scale_colour_binned(type = "viridis",
breaks = c(22, 25, 26, 29),
limits = c(0, 40),
guide = guide_coloursteps(even.steps = FALSE,
show.limits = TRUE)) +
binned 是最接近的,但我不知道如何指定我想要的 5 种颜色(即,不是 viridis)。我可能想要的“类型”的帮助选项是:
返回连续色标的函数
但鉴于我只想说(例如)
c("red", "yellow", "green", "yellow", "red")
我感觉要么我很接近并且只是在某个地方(可能是多个地方)误解了一个步骤,要么我使用了错误的方法,而这三个功能都不是。
任何人都可以提供建议吗?
谢谢大家!
编辑:按照 Will 的建议进行更新:保持 geom_point 调用中的值不变,即
geom_point(aes(colour = MeanETemp24hU2M), size = 0.5) +
(而不是将它们重新缩放为 0:1),以下代码:
scale_colour_stepsn(colours = c("red", "yellow", "green", "yellow", "red"),
limits = c(0, 40),
guide = guide_coloursteps(even.steps = FALSE,
show.limits = TRUE),
breaks = c(0, 22, 25, 26, 29, 40)) +
生产:
很接近但是
A. colours 向量与 breaks 箱不匹配
B.颜色本身是混合色,而不是真正的 R 颜色。使用 Will 示例的修改:
x <- 1:100
y <- runif(100) * 100
tibble(x, y) %>%
ggplot() +
aes(x = x, y = y, color = y) +
geom_point() +
scale_color_stepsn(
colours = c("red", "yellow", "green", "yellow", "red"),
breaks = c(0, 0.2, 0.4, 0.6, 0.8, 1) * 100)
生产:
这些值是正确的并且与颜色箱正确对齐(IDK 为什么我的没有给出相同的配方),但是颜色是稀释的混合 - 将该图像中的红色与彩虹末端的红色进行比较第二张图片。
【问题讨论】:
-
如果您不一定想要颜色之间的渐变,您可以考虑在数据框中添加一列,根据不合适、合适、理想来定义合适性,然后根据此因素着色?
-
可爱、优雅的解决方案 Will,不错。仍然会在概念上对是否有符合我尝试的正确解决方案感兴趣,但我想当我明天回到这个问题时,我会选择你的解决方案。干杯!