r - 在ggplot中添加手动比例时如何修复错误?

标签 r ggplot2

当我添加如下示例中的手动比例时。我收到以下错误消息:

"Warning message: In if (self$guide == "none") return() : the condition has length > 1 and only the first element will be used"



是什么导致了此错误消息,我该如何解决?

非常感谢您的帮助!
d <- data.frame(week = seq(1, 52, 1), 
            revenue = round(runif(52, 0, 100)), 0)

p <- ggplot(data = d, aes(x = week, y = revenue, fill = 'lightskyblue')) + 
  geom_bar(stat = 'identity', colour = 'black') 


p <- p + scale_fill_identity(name = NULL,
                             guide = guide_legend(label.position = 'top'),
                             labels = c('2019'))

p

最佳答案

简答
支持独立的 MCVE,但我想强调的是,您收到的是警告消息,而不是错误。在这种特殊情况下,你不需要修复任何东西 .您可以忽略该消息,因为代码按预期工作得非常好。
更长的解释
正如@r2evans 所指出的,这是由于 scale_*_identity代码 [found here] .如 ?scale_fill_identity 中所述,默认情况下这些比例不会产生图例,但您可以通过替换 guide = "none" 来覆盖它。与其他类型的指南争论。
它应该如何工作:
此图例创建行为在 train 中编码ScaleDiscreteIdentity 的函数/ScaleContinuousIdentity ,它会检查指南参数的值是否为“none”。如果是这样,请不要创建图例。否则,使用 train函数来自 ScaleDiscrete/ScaleContinuous创建适当的图例:

ScaleDiscreteIdentity <- ggproto("ScaleDiscreteIdentity", ScaleDiscrete,
  ...,
  train = function(self, x) {
    # do nothing if no guide, otherwise train so we know what breaks to use
    if (self$guide == "none") return()
    ggproto_parent(ScaleDiscrete, self)$train(x)
  }
)

ScaleContinuousIdentity <- ggproto("ScaleContinuousIdentity", ScaleContinuous,
  ...,
  train = function(self, x) {
    # do nothing if no guide, otherwise train so we know what breaks to use
    if (self$guide == "none") return()
    ggproto_parent(ScaleContinuous, self)$train(x)
  }
)
?scale_fill_identity 中的示例表示 guide = "legend"可以用来代替 guide = "none" .在实践中,guide = guide_legend(...)也可以使用。事实上,?guides状态:

guide = "legend" in scale_* is syntactic sugar for guide = guide_legend()


实际工作原理 (至少基于我当前的版本 3.1.0):
对于 scale_*_identity ,那里 guide = "legend" 之间的细微差别和 guide = guide_legend(...) .我已经削减了 MCVE 来说明这一点:
p <- ggplot(data = d, 
            aes(x = week, y = revenue, fill = 'lightskyblue')) + 
  geom_col()

p1 <- p + scale_fill_identity(guide = guide_legend())
p2 <- p + scale_fill_identity(guide = "legend")
p3 <- p + scale_fill_identity(guide = "none")
plot
p1 和 p2 都会产生相同的图例,但 p1 会触发警告,而 p2 不会。 (当然,p3 使用默认选项并且不会触发任何警告。)造成这种差异的原因在于它们的填充比例,可以在 p<1/2/3>$scales$scales[[1]] 中找到。 :
> p1$scales$scales[[1]]$guide
$`title`
list()
attr(,"class")
[1] "waiver"

$title.position
NULL

$title.theme
NULL

... #omitted for space. it goes on for a while

> p2$scales$scales[[1]]$guide
[1] "legend"

> p3$scales$scales[[1]]$guide
[1] "none"
p1$scales$scales[[1]]$guide有 21 个项目,而 p2$scales$scales[[1]]$guidep3$scales$scales[[1]]$guide每个都包含一个字符串。因此,当 train函数检查 self$guide == "none" , p1 返回 TRUE/FALSE 值的列表,每个项目一个,而 p2 和 p3 返回单个 TRUE/FALSE。
> p1$scales$scales[[1]]$guide == "none"
         title title.position    title.theme    title.hjust    title.vjust          label 
         FALSE          FALSE          FALSE          FALSE          FALSE          FALSE 
label.position    label.theme    label.hjust    label.vjust       keywidth      keyheight 
         FALSE          FALSE          FALSE          FALSE          FALSE          FALSE 
     direction   override.aes           nrow           ncol          byrow        reverse 
         FALSE          FALSE          FALSE          FALSE          FALSE          FALSE 
         order  available_aes           name 
         FALSE          FALSE          FALSE 
> p2$scales$scales[[1]]$guide == "none"
[1] FALSE
> p3$scales$scales[[1]]$guide == "none"
[1] TRUE
train函数只期望从它的 self$guide == "none" 返回一个值检查,p1 触发条件长度 > 1 的警告消息。
有没有关系 :
并不真地。当面对来自 p1 的 21 个 TRUE/FALSE 值的列表时,train函数只查看第一个---这是错误的,因为 p1$scales$scales[[1]]$guide$title的默认值为 waiver() .所以p1的guide = guide_legend() check out 方式与 p2 的 guide = "legend" 相同.您可以忽略警告并继续。
... 只是不要在 guide_legend 中将您的图例标题命名为“无” :
p4 <- p + scale_fill_identity(guide = guide_legend(title = "none"))
# this will cause the legend to disappear, because the first value in
# p4$scales$scales[[1]]$guide == "none" will actually be TRUE
无论如何应该解决它 :
是的,虽然我会说这不是一个严重的错误,因为代码通常按预期运行(上面的 p4 是一个边缘案例)。
我想无论如何修复应该很简单,例如:
# this
if (all(self$guide == "none")) return() 

# instead of this
if (self$guide == "none") return()

关于r - 在ggplot中添加手动比例时如何修复错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54580005/

相关文章:

r - 与 mutate_at dplyr 相反

Windows 上的 R : character encoding hell

r - R中的下拉列表实现

r - 与 match.arg() 匹配的数字参数

r - 为绘图制作 2D 图例 - 双变量等值线图

r - R Markdown 中的内联 dplyr

r 将光栅堆栈或砖 block 转换为动画

r - 控制ggplot2中点的顺序?

r - 使用 scale_y_time 在 Boxplot (GGPLOT) 中将 MS 转换为分和秒

r - 在 R 中创建 "The Economist"样式图?