dez*_*000 8 r colors scale ggplot2
我正在尝试为 ggplot 点图设置色标,划分区域:不适合、适合、理想、适合、不适合。在你的标准 ggplot 代码包装器中,我尝试过gradient2
scale_colour_gradient2(low = "grey",
mid = "red",
high = "grey",
midpoint = 25) +
Run Code Online (Sandbox Code Playgroud)
和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))) +
Run Code Online (Sandbox Code Playgroud)
stepsn,
scale_colour_stepsn(colours = c("grey", "blue", "red", "blue", "grey"),
values = scales::rescale(c(0, 22, 25, 26, 29, 40))) +
Run Code Online (Sandbox Code Playgroud)
和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)) +
Run Code Online (Sandbox Code Playgroud)
binned是最接近的,但我不知道如何指定我想要的 5 种颜色(即,不是viridis)。我可能想要的“类型”的帮助选项是:
返回连续色阶的函数
但我无法在心里弄清楚如何/为什么需要创建一个创建连续颜色比例的函数,因为我只想说(例如)
c("red", "yellow", "green", "yellow", "red")
Run Code Online (Sandbox Code Playgroud)
我感觉要么我很接近,只是误解了某个地方的步骤(可能是多个地方),要么我使用了错误的方法,而且这三个功能都不是。
有机会有人可以建议吗?
谢谢大家!
编辑:更新以下威尔的建议:保持 geom_point 调用中的值不变,即
geom_point(aes(colour = MeanETemp24hU2M), size = 0.5) +
Run Code Online (Sandbox Code Playgroud)
(而不是将它们重新缩放为 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)) +
Run Code Online (Sandbox Code Playgroud)
生产:
哪个很接近但是
A.向量与bincolours不匹配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)
Run Code Online (Sandbox Code Playgroud)
生产:
这些值是正确的并且与颜色箱正确对齐(不知道为什么我的没有给出相同的配方),但颜色被稀释混合 - 将该图像中的红色与第二个图像中彩虹末端的红色进行比较。
小智 7
我正试图做到这一点,并找到了一种方法binned_scale,这就是scale_color_stepsn所谓的。我将使用与 Will 类似的示例,希望您能将其转化为您的案例。
该palette参数仍然需要是一个函数,但您可以使用一个仅返回所需颜色的函数。这里我展示了您可以使用不规则的休息,如果您想使用,请单独指定限制show.limits = TRUE(否则它会显示数据集中的限制)。
x <- 1:100
y <- runif(100) * 100
tib <- tibble(x, y)
ggplot(tib, aes(x = x, y = y, color = y)) +
geom_point() +
binned_scale(aesthetics = "color",
scale_name = "stepsn",
palette = function(x) c("red", "yellow", "green", "yellow", "red"),
breaks = c(5, 10, 25, 60),
limits = c(0, 100),
show.limits = TRUE,
guide = "colorsteps"
)
Run Code Online (Sandbox Code Playgroud)
这里,guide = "colorsteps"需要在图例中填充方块。如果您希望图例中的比例条与 一样even.steps = FALSE,那么您需要guide = "colorbar"改为使用。
看起来您已经接近了scale_color_stepsn,如果您将 rescale 参数传递给 Breaks,它可能会起作用。我认为以下内容符合您的期望吗?
library(tidyverse)
x <- 1:100
y <- runif(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)
)
Run Code Online (Sandbox Code Playgroud)
可以替换scale_color_gradientn,这样更漂亮!
scale_color_gradientn(
colours = c("red", "yellow", "green", "yellow", "red"),
breaks = c(0, 0.2, 0.4, 0.6, 0.8, 1)
Run Code Online (Sandbox Code Playgroud)
编辑:
重新缩放问题肯定会引起问题。我认为这个解决方案适用于scales_color_gradientn. 注意我稍微改变了测试数据框。
library(tidyverse)
x <- seq(0, 40, 0.25)
y <- seq(0, 40, 0.25)
tibble(x, y) %>%
ggplot() +
aes(x = x, y = y, color = y) +
geom_point() +
scale_color_gradientn(
colours = c("red", "yellow", "green", "yellow", "red"),
values = scales::rescale(c(0, 22, 25, 26, 29, 40))
)
Run Code Online (Sandbox Code Playgroud)
我尝试了几次来获得一些不错的东西scales_color_stepsn,但正如你所看到的,这个函数似乎在中断处分配“纯”颜色,并合并它们之间的值的颜色(我无法理解其他一些不寻常的颜色)不过,颜色乱序等行为)。我认为如果您想要纯红色/黄色/绿色的阶梯渐变,我最初的评论可能是最好的方法 - 在数据框中分配一个值以将颜色映射到。上面代码中的图例具有均匀间隔的标签,如果您想标记中断,请添加breaks = c(0, 22, 25, 26, 29, 40)到参数中,尽管我发现这样很难阅读标签。HTH。