gghighlight `label_key` producing error

I want to use the gghighlight package to label only the maximum y-axis value in a plot. Here's my code:

library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.6.2
library(gghighlight)
#> Warning: package 'gghighlight' was built under R version 3.6.3

set.seed(47)
df <- tibble::tibble(
    a = seq(0, 100, by = 5),
    b = runif(21)
)

ggplot(df) +
    aes(x = a, y = b) + 
    geom_point() +
    geom_line() +
    gghighlight(b == max(b), label_key = a) +
    theme_minimal()
#> Error: Column `NULL` is unknown

Created on 2020-03-10 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.6.1 (2019-07-05)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  ctype    English_United States.1252  
#>  tz       America/New_York            
#>  date     2020-03-10                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 3.6.1)
#>  backports     1.1.5   2019-10-02 [1] CRAN (R 3.6.1)
#>  broom         0.5.3   2019-12-14 [1] CRAN (R 3.6.2)
#>  callr         3.4.0   2019-12-09 [1] CRAN (R 3.6.2)
#>  cellranger    1.1.0   2016-07-27 [1] CRAN (R 3.6.1)
#>  cli           2.0.1   2020-01-08 [1] CRAN (R 3.6.2)
#>  colorspace    1.4-1   2019-03-18 [1] CRAN (R 3.6.1)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 3.6.1)
#>  DBI           1.1.0   2019-12-15 [1] CRAN (R 3.6.2)
#>  dbplyr        1.4.2   2019-06-17 [1] CRAN (R 3.6.1)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 3.6.1)
#>  devtools      2.2.1   2019-09-24 [1] CRAN (R 3.6.1)
#>  digest        0.6.23  2019-11-23 [1] CRAN (R 3.6.2)
#>  dplyr       * 0.8.3   2019-07-04 [1] CRAN (R 3.6.1)
#>  ellipsis      0.3.0   2019-09-20 [1] CRAN (R 3.6.1)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 3.6.1)
#>  fansi         0.4.1   2020-01-08 [1] CRAN (R 3.6.2)
#>  forcats     * 0.4.0   2019-02-17 [1] CRAN (R 3.6.1)
#>  fs            1.3.1   2019-05-06 [1] CRAN (R 3.6.1)
#>  generics      0.0.2   2018-11-29 [1] CRAN (R 3.6.1)
#>  gghighlight * 0.2.0   2020-01-25 [1] CRAN (R 3.6.3)
#>  ggplot2     * 3.2.1   2019-08-10 [1] CRAN (R 3.6.1)
#>  glue          1.3.1   2019-03-12 [1] CRAN (R 3.6.1)
#>  gtable        0.3.0   2019-03-25 [1] CRAN (R 3.6.1)
#>  haven         2.2.0   2019-11-08 [1] CRAN (R 3.6.2)
#>  highr         0.8     2019-03-20 [1] CRAN (R 3.6.1)
#>  hms           0.5.3   2020-01-08 [1] CRAN (R 3.6.2)
#>  htmltools     0.4.0   2019-10-04 [1] CRAN (R 3.6.1)
#>  httr          1.4.1   2019-08-05 [1] CRAN (R 3.6.1)
#>  jsonlite      1.6     2018-12-07 [1] CRAN (R 3.6.1)
#>  knitr         1.26    2019-11-12 [1] CRAN (R 3.6.2)
#>  lattice       0.20-38 2018-11-04 [1] CRAN (R 3.6.1)
#>  lazyeval      0.2.2   2019-03-15 [1] CRAN (R 3.6.1)
#>  lifecycle     0.1.0   2019-08-01 [1] CRAN (R 3.6.1)
#>  lubridate     1.7.4   2018-04-11 [1] CRAN (R 3.6.1)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 3.6.1)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 3.6.1)
#>  modelr        0.1.5   2019-08-08 [1] CRAN (R 3.6.1)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 3.6.1)
#>  nlme          3.1-143 2019-12-10 [1] CRAN (R 3.6.2)
#>  pillar        1.4.3   2019-12-20 [1] CRAN (R 3.6.2)
#>  pkgbuild      1.0.6   2019-10-09 [1] CRAN (R 3.6.1)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 3.6.1)
#>  pkgload       1.0.2   2018-10-29 [1] CRAN (R 3.6.1)
#>  prettyunits   1.1.0   2020-01-09 [1] CRAN (R 3.6.2)
#>  processx      3.4.1   2019-07-18 [1] CRAN (R 3.6.1)
#>  ps            1.3.0   2018-12-21 [1] CRAN (R 3.6.1)
#>  purrr       * 0.3.3   2019-10-18 [1] CRAN (R 3.6.1)
#>  R6            2.4.1   2019-11-12 [1] CRAN (R 3.6.2)
#>  Rcpp          1.0.3   2019-11-08 [1] CRAN (R 3.6.2)
#>  readr       * 1.3.1   2018-12-21 [1] CRAN (R 3.6.1)
#>  readxl        1.3.1   2019-03-13 [1] CRAN (R 3.6.1)
#>  remotes       2.1.0   2019-06-24 [1] CRAN (R 3.6.1)
#>  reprex        0.3.0   2019-05-16 [1] CRAN (R 3.6.1)
#>  rlang         0.4.2   2019-11-23 [1] CRAN (R 3.6.2)
#>  rmarkdown     2.0     2019-12-12 [1] CRAN (R 3.6.2)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.6.1)
#>  rvest         0.3.5   2019-11-08 [1] CRAN (R 3.6.1)
#>  scales        1.1.0   2019-11-18 [1] CRAN (R 3.6.2)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.6.1)
#>  stringi       1.4.4   2020-01-09 [1] CRAN (R 3.6.2)
#>  stringr     * 1.4.0   2019-02-10 [1] CRAN (R 3.6.1)
#>  testthat      2.3.1   2019-12-01 [1] CRAN (R 3.6.2)
#>  tibble      * 2.1.3   2019-06-06 [1] CRAN (R 3.6.1)
#>  tidyr       * 1.0.0   2019-09-11 [1] CRAN (R 3.6.1)
#>  tidyselect    0.2.5   2018-10-11 [1] CRAN (R 3.6.1)
#>  tidyverse   * 1.3.0   2019-11-21 [1] CRAN (R 3.6.2)
#>  usethis       1.5.1   2019-07-04 [1] CRAN (R 3.6.1)
#>  vctrs         0.2.1   2019-12-17 [1] CRAN (R 3.6.2)
#>  withr         2.1.2   2018-03-15 [1] CRAN (R 3.6.1)
#>  xfun          0.11    2019-11-12 [1] CRAN (R 3.6.2)
#>  xml2          1.2.2   2019-08-09 [1] CRAN (R 3.6.1)
#>  yaml          2.2.0   2018-07-25 [1] CRAN (R 3.6.0)
#>  zeallot       0.1.0   2018-01-28 [1] CRAN (R 3.6.1)
#> 
#> [1] C:/Users/rgerecke/Documents/R/R-3.6.1/library

There's no problem if I run the example code in the gghighlight package, but I can't get it to work with my own data (or the iris dataset). Tips appreciated!

Hmm..., thanks for sharing the curious case. This is because gghighlight() is not smart enough to choose right way to determine the position of labels when there are several types of Geoms. I'm not sure I can come up with a nice fix for this, but l filed an issue here: gghighlight fails to label points with lines when there are no group · Issue #136 · yutannihilation/gghighlight · GitHub

library(tidyverse)
library(gghighlight)

set.seed(47)
df <- tibble::tibble(
  a = seq(0, 100, by = 5),
  b = runif(21)
)

ggplot(df) +
  aes(x = a, y = b) + 
  geom_line() +
  geom_point() +
  gghighlight(b == max(b), label_key = a) +
  theme_minimal()
#> Error: `vars` must be a character vector

ggplot(df) +
  aes(x = a, y = b) + 
  geom_line() +
  geom_point() +
  gghighlight(b == max(b), label_key = a, n = 1) +
  theme_minimal()

Created on 2020-03-11 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.