gghighlight issue with labels and facet_wrap

I´m trying to create a function to plot some lines for just one group or for many groups, using facet_wrap. The basic plot also uses gghighlight to highlight some of the lines (in each facet) according to some criteria. Everything works fine when I plot the groups one by one. When I plot two groups together, however, the highligths are still fine, but the labels of the highlighted line only show up on the first plot. Here is the code:


# create an example dataframe
df <- crossing(product = paste0("P", seq(2)), 
               origin = paste0(LETTERS[1:3], LETTERS[1:3]), 
               year = c(2001, 2005)) 

df$qtd <- sample(10, nrow(df), replace = TRUE)
df$class <- 1
df$class[df$origin=="AA"] <- 5 

#basic plotting function
highplot <- function(df) {
  p <- df %>% 
    ggplot(aes(x = year, y = qtd, colour = class, group = origin)) + 
    geom_line(size = 2, alpha = 0.5) +
    facet_wrap(~product, scales = "free_y") + 
    gghighlight(max(class) > 4,  calculate_per_facet = TRUE, keep_scales = TRUE)

pltfcn <- function(df, flt) {
  p <- df %>% 
    filter(product %in% flt) %>%

# test it
pltfcn(df, "P1")
pltfcn(df, "P2")
pltfcn(df, c("P1", "P2"))

Here are the resulting plots:

Any help to fix this behaviour will be greatly appreciated.

For completeness, here is the output of sessionInfo():

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

[1] LC_COLLATE=Portuguese_Brazil.1252 LC_CTYPE=Portuguese_Brazil.1252 LC_MONETARY=Portuguese_Brazil.1252
[4] LC_NUMERIC=C LC_TIME=Portuguese_Brazil.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] gghighlight_0.3.0 ggplot2_3.3.2 tidyr_1.1.2 dplyr_1.0.2

loaded via a namespace (and not attached):
[1] Rcpp_1.0.5 rstudioapi_0.12 magrittr_1.5 tidyselect_1.1.0 munsell_0.5.0 colorspace_2.0-0
[7] R6_2.5.0 rlang_0.4.8 fansi_0.4.1 tools_4.0.2 grid_4.0.2 gtable_0.3.0
[13] cli_2.1.0 withr_2.3.0 ellipsis_0.3.1 assertthat_0.2.1 digest_0.6.27 tibble_3.0.4
[19] lifecycle_0.2.0 crayon_1.3.4 farver_2.0.3 purrr_0.3.4 vctrs_0.3.4 ggrepel_0.8.2
[25] glue_1.4.2 labeling_0.4.2 compiler_4.0.2 pillar_1.4.6 generics_0.1.0 scales_1.1.1
[31] pkgconfig_2.0.3

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.