我想通过为比例尺底部和顶部的值添加手动标签名称(低和高)来更改连续颜色图例。我怎样才能做到这一点?
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)
预期输出:

我尝试labels = c("Low", 5, 10, 25, 60, "High")在脚本中添加,但它显示了此错误:
Error in f():
! Breaks and labels are different lengths
Run rlang::last_error() to see where the error occurred.
Run Code Online (Sandbox Code Playgroud)
实现此目的的一个简单选择(我刚刚发现)是将命名向量传递给参数,limits其中名称是您想要的标签:
set.seed(123)
library(ggplot2)
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(low = 0, high = 100),
show.limits = TRUE,
guide = "colorsteps")
Run Code Online (Sandbox Code Playgroud)

编辑不是 100% 确定箭头,但添加一些箭头的一个简单选择是在标签中包含左箭头的 UTF-8 符号:
set.seed(123)
x <- 1:100
y <- runif(100) * 100
tib <- data.frame(x, y)
library(ggplot2)
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("\u2190 low" = 0, "\u2190 high" = 100),
show.limits = TRUE,
guide = "colorsteps")
Run Code Online (Sandbox Code Playgroud)
