【问题标题】:Error: (converted from warning) Ignoring unknown aesthetics错误:(从警告转换)忽略未知的美学
【发布时间】:2018-03-18 06:08:55
【问题描述】:

有时当我使用ggplot2 时,会出现以下错误:

> dframe <- data.frame(a=letters, b=LETTERS, x=runif(26), y=runif(26))
> p <- ggplot(dframe, aes(x,y)) + geom_point(aes(text=sprintf("letter: %s<br>LETTER: %s", a, b)))
Error: (converted from warning) Ignoring unknown aesthetics: text

然后我重新启动 R-Studio(当您的工作未完成时,这非常令人生畏)并且一切正常(直到问题再次发生):

> dframe <- data.frame(a=letters, b=LETTERS, x=runif(26), y=runif(26))
> p <- ggplot(dframe, aes(x,y)) + geom_point(aes(text=sprintf("letter: %s<br>LETTER: %s", a, b)))
Warning: Ignoring unknown aesthetics: text
> ggplotly(p)
We recommend that you use the dev version of ggplot2 with `ggplotly()`
Install it with: `devtools::install_github('hadley/ggplot2')`

有什么问题?我正在使用 Windows 10、R-studio 1.0.153 和以下 R 版本:

> R.version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          4.2                         
year           2017                        
month          09                          
day            28                          
svn rev        73368                       
language       R                           
version.string R version 3.4.2 (2017-09-28)
nickname       Short Summer           

对应的包有以下版本:

ggplot2 2.2.1
plotly  4.7.1

谢谢

【问题讨论】:

  • text 不是geom_point 的有效美学。你想要geom_text 还是geom_label?检查这些帮助页面。
  • 您的代码中是否有 options(warn=2) 或者可能通过工作区加载?
  • @AndrewGustar 我不这么认为:当我将text 更改为geom_textgeom_label 时,我分别得到了:Warning: Ignoring unknown aesthetics: geom_textWarning: Ignoring unknown aesthetics: geom_label
  • @user20650 不,我没有在我的代码中的任何地方输入options(warn=2)options()$warn 返回[1] 0
  • 好吧,这是一个猜测,但它确实重现了将警告升级为错误的行为:例如:options(warn=0) ; ggplot(dframe, aes(x,y)) + geom_point(aes(text=sprintf("letter: %s&lt;br&gt;LETTER: %s", a, b))) ; options(warn=2) ; ggplot(dframe, aes(x,y)) + geom_point(aes(text=sprintf("letter: %s&lt;br&gt;LETTER: %s", a, b)))

标签: r ggplot2


【解决方案1】:

将美学映射放在ggplot而不是geom_point下以避免警告:

## This produces an "unknown aesthetic" warning
ggplot( mtcars, aes( x = wt, y = mpg ) ) + geom_point( aes( text = cyl ) )

## This doesn't
ggplot( mtcars, aes( x = wt, y = mpg, text = cyl ) ) + geom_point()

【讨论】:

  • 非常感谢。知道为什么有时会发出警告,有时会发出错误吗?
  • 我的理解是每个geom都有一套它使用的美学。如果您向它提供一个它不知道如何处理的东西,它会发出警告。由于ggplot() 不知道它将与哪个几何图形配对,它假定所有美学映射都是有效的。
  • 就警告与错误而言,这由getOption("warn") 控制,如问题 cmets 中所述。有可能plotlywarn 选项升级为2,调用ggplot 产生警告的部分,然后将warn 降级为0。如果不跟踪源代码,就无法知道.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-26
  • 2023-04-02
相关资源
最近更新 更多