0

Sometimes when I use ggplot2 I get following error:

> 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

Then I restart R-Studio (which is pretty daunting when you have your work unfinished) and everything works fine (until the problem happens again):

> 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')`

What is the problem? I'm using Windows 10, R-studio 1.0.153, and following R version:

> 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           

Corresponding packages are following version:

ggplot2 2.2.1
plotly  4.7.1

Thanks

Wakan Tanka
  • 6,662
  • 12
  • 58
  • 109
  • 1
    `text` is not a valid aesthetic for `geom_point`. Do you want `geom_text` or `geom_label` instead? Check the help pages for these. – Andrew Gustar Oct 06 '17 at 12:28
  • do you have `options(warn=2)` anywhere in your code or perhaps loaded via workspace? – user20650 Oct 06 '17 at 12:38
  • @AndrewGustar I do not think so: when I changed `text` to `geom_text` or `geom_label` I got: `Warning: Ignoring unknown aesthetics: geom_text` or `Warning: Ignoring unknown aesthetics: geom_label` respectively. – Wakan Tanka Oct 06 '17 at 12:38
  • @user20650 no I haven't typed `options(warn=2)` anywhere in my code. `options()$warn` returned `[1] 0` – Wakan Tanka Oct 06 '17 at 12:40
  • okay, it was a guess, but it does reproduce the behaviour of escalting warnings to errors: example: `options(warn=0) ; ggplot(dframe, aes(x,y)) + geom_point(aes(text=sprintf("letter: %s
    LETTER: %s", a, b))) ; options(warn=2) ; ggplot(dframe, aes(x,y)) + geom_point(aes(text=sprintf("letter: %s
    LETTER: %s", a, b)))`
    – user20650 Oct 06 '17 at 12:42
  • @WakanTanka - no, you need to replace `geom_point` with a `geom_text` - something like this... `ggplot(dframe, aes(x,y)) + geom_text(aes(label=sprintf("letter: %s
    LETTER: %s", a, b)))`
    – Andrew Gustar Oct 06 '17 at 12:43
  • @AndrewGustar I guess that `geom_label` and `geom_text` does something different that I want. My goal is to achieve tool tips over mouse hover https://stackoverflow.com/questions/44569551/date-format-in-hover-for-ggplot2-and-plotly I was also inspired by this question. – Wakan Tanka Oct 06 '17 at 12:58

1 Answers1

2

Place the aesthetic mapping under ggplot instead of geom_point to avoid the warning:

## 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()
Artem Sokolov
  • 12,492
  • 4
  • 38
  • 69
  • Thank you very much. any idea why sometimes warning and sometimes error was issued? – Wakan Tanka Oct 07 '17 at 12:56
  • My understanding is that every geom has a set of aesthetics it uses. If you provide it with one it doesn't know what to do with, it issues a warning. Since `ggplot()` doesn't know which geom it will be paired with, it assumes that all aesthetic mappings are valid. – Artem Sokolov Oct 07 '17 at 16:03
  • As far as warnings vs. errors, that's controlled by `getOption("warn")` as stated in the question comments. It's possible that `plotly` escalates the `warn` option to 2, calls the portion of `ggplot` that produces the warning, then de-escalates `warn` back to 0. Without tracing through the source code, there's no way to know. – Artem Sokolov Oct 07 '17 at 16:14