当数据中不存在alpha(透明度)级别时,不等中断和标签长度错误(Unequal breaks and label lengths error when alpha (transparency) le

编程入门 行业动态 更新时间:2024-10-19 06:25:52
当数据中不存在alpha(透明度)级别时,不等中断和标签长度错误(Unequal breaks and label lengths error when alpha (transparency) level not present in data)

在ggplot2 2.2.0版中,例如:

tmp_df <- data.frame(x = 1:3, y = 1:3, alpha = rep(0.5, 3)) # x y alpha # 1 1 1 0.5 # 2 2 2 0.5 # 3 3 3 0.5 ggplot(tmp_df, aes(x, y, alpha = alpha)) + geom_bar(stat = 'identity') + scale_alpha(breaks = c(0.25, 0.5, 1), labels = c('a', 'b', 'c'))

产生错误:

Error in f(..., self = self) : Breaks and labels are different lengths

手动删除scale_alpha的额外alpha值scale_alpha解决问题,但肯定可以通过ggplot来处理这个问题吗?

In ggplot2 version 2.2.0, E.g.:

tmp_df <- data.frame(x = 1:3, y = 1:3, alpha = rep(0.5, 3)) # x y alpha # 1 1 1 0.5 # 2 2 2 0.5 # 3 3 3 0.5 ggplot(tmp_df, aes(x, y, alpha = alpha)) + geom_bar(stat = 'identity') + scale_alpha(breaks = c(0.25, 0.5, 1), labels = c('a', 'b', 'c'))

Produces the error:

Error in f(..., self = self) : Breaks and labels are different lengths

Manually removing the extra alpha values in scale_alpha fixes the problem, but surely this can be handled some how by ggplot?

最满意答案

您必须提供比例限制,因为tmp_df$alpha始终相同,并且ggplot不知道比例的“范围”。

library(ggplot2)
tmp_df <- data.frame(x = 1:3, y = 1:3, alpha = rep(0.5, 3))
tmp_df
#>   x y alpha
#> 1 1 1   0.5
#> 2 2 2   0.5
#> 3 3 3   0.5

ggplot(tmp_df, aes(x, y, alpha = alpha)) +
    geom_bar(stat = 'identity') +
    scale_alpha(breaks = c(0.25, 0.5, 1), labels = c('a', 'b', 'c'), limits = c(0, 1))
 

如果alpha维度本身具有范围,则不再需要限制,但请注意,在下面的示例中,将忽略第一个break ,因为它超出范围。 如果你想包括它,那么再次需要limits 。

tmp_df <- data.frame(x = 1:3, y = 1:3, alpha = seq(.5, 1.5, .5))
tmp_df
#>   x y alpha
#> 1 1 1   0.5
#> 2 2 2   1.0
#> 3 3 3   1.5

ggplot(tmp_df, aes(x, y, alpha = alpha)) +
    geom_bar(stat = 'identity') +
    scale_alpha(breaks = c(0.25, 0.5, 1), labels = c('a', 'b', 'c'))
 

You must supply the limits for the scale because tmp_df$alpha is always the same, and ggplot does not know the 'range' of the scale.

library(ggplot2)
tmp_df <- data.frame(x = 1:3, y = 1:3, alpha = rep(0.5, 3))
tmp_df
#>   x y alpha
#> 1 1 1   0.5
#> 2 2 2   0.5
#> 3 3 3   0.5

ggplot(tmp_df, aes(x, y, alpha = alpha)) +
    geom_bar(stat = 'identity') +
    scale_alpha(breaks = c(0.25, 0.5, 1), labels = c('a', 'b', 'c'), limits = c(0, 1))
 

If the alpha dimension has a range itself, limits are no longer necessary, but note the in the following example the first break is ignored, as it is outside the range. limits would again be necessary if you want to include it.

tmp_df <- data.frame(x = 1:3, y = 1:3, alpha = seq(.5, 1.5, .5))
tmp_df
#>   x y alpha
#> 1 1 1   0.5
#> 2 2 2   1.0
#> 3 3 3   1.5

ggplot(tmp_df, aes(x, y, alpha = alpha)) +
    geom_bar(stat = 'identity') +
    scale_alpha(breaks = c(0.25, 0.5, 1), labels = c('a', 'b', 'c'))
 

更多推荐

本文发布于:2023-08-06 23:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1456734.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:透明度   中不   长度   级别   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!