gpt4 book ai didi

r - 使用scale_fill_stepsn()进行标记, "Breaks and labels are different lengths"

转载 作者:行者123 更新时间:2023-12-02 18:36:40 25 4
gpt4 key购买 nike

这是我的情节:

enter image description here

我使用了这段代码(包括示例数据):

# dummy data
df_test <- data.frame(long = rep(447030:447050, 21),
lat = rep(5379630:5379650, each=21),
z = rnorm(21*21))

# plot
ggplot(df_test) +
geom_tile(aes(x=long, y = lat, fill = z)) +
scale_fill_stepsn(
limits = c(-3, 3), breaks = seq(-3, 3, 1), # labels = seq(-3, 3, 1),
colors = c("#ff6f69", "grey90", "#00aedb"))

我希望图例显示最大值和最小值(-3、+3)。但是当我取消注释标签代码 labels = seq(-3, 3, 1), 时,我收到错误:

Error: Breaks and labels are different lengths"

这是一个错误还是我滥用了该功能? (又名:这是一个错误还是一个功能?)无论哪种方式:你们知道这个问题的任何解决方法/解决方案吗?也许有 override.aes() 的东西(我不太擅长这个功能)?

R 版本:4.1.0 | ggplot2版本:3.3.5

(可能相关:Breaks and labels of different lengths scale_size_binned)

编辑:如果我安装 ggplot2 版本 3.3.3,图例中的最后一个框会以某种方式更大(我也不喜欢)。

enter image description here

最佳答案

这只是我认为可能存在错误的解决方法,但您可以稍微调整中断以添加/减去一个非常小的值:

library(ggplot2)

# dummy data
df_test <- data.frame(long = rep(447030:447050, 21),
lat = rep(5379630:5379650, each=21),
z = rnorm(21*21))

# plot
smallvalue <- 10 * .Machine$double.eps

ggplot(df_test) +
geom_tile(aes(x=long, y = lat, fill = z)) +
scale_fill_stepsn(
limits = c(-3, 3),
breaks = c(-3 + smallvalue, -2:2, 3 - smallvalue),
labels = seq(-3, 3, 1),
colors = c("#ff6f69", "grey90", "#00aedb")
)

reprex package 于 2021 年 8 月 6 日创建(v1.0.0)

编辑:

或者,您可以设置内部分隔符并使用 labels 参数的函数。

library(ggplot2)

# dummy data
df_test <- data.frame(long = rep(447030:447050, 21),
lat = rep(5379630:5379650, each=21),
z = rnorm(21*21))

# plot
smallvalue <- 10 * .Machine$double.eps

ggplot(df_test) +
geom_tile(aes(x=long, y = lat, fill = z)) +
scale_fill_stepsn(
limits = c(-3, 3),
breaks = -2:2,
labels = function(x) {x}, # Just display the breaks
show.limits = TRUE,
colors = c("#ff6f69", "grey90", "#00aedb")
)

reprex package 于 2021 年 8 月 6 日创建(v1.0.0)

关于r - 使用scale_fill_stepsn()进行标记, "Breaks and labels are different lengths",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68679342/

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