gpt4 book ai didi

r - 使用 ggplot2 包在颜色条上手动添加标签名称

转载 作者:行者123 更新时间:2023-12-03 08:07:32 25 4
gpt4 key购买 nike

我想通过为比例尺底部和顶部的值添加手动标签名称(低和高)来更改连续颜色图例。我怎样才能做到这一点?

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")

预期输出:

enter image description here

我尝试在脚本中添加 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.

最佳答案

实现此目的的一个简单选择(我刚刚发现)是将命名向量传递给 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")

编辑对箭头不是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")

关于r - 使用 ggplot2 包在颜色条上手动添加标签名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71716811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com