How to change/highlight a specific column color in ggplot - quo must be a quosure

Hello!

I've been learning the basics on time series analysis and wanted to be able to highlight specific columns, dots, bars in a plot - I've learned there is a package called gghighlight that does this job really well, but strangely it doesn't work if I plot a chart with ggblanket, which I've been using a lot lately to simplify coding.

This would be a working reproductible example for ggplot + gghighlight:

count(mtcars, carb) %>% 
  ggplot(aes(x = carb, y = n)) + 
  geom_col() + 
  scale_x_continuous(breaks = breaks_width(1)) +
  scale_y_continuous(breaks = breaks_width(1)) +
  gghighlight::gghighlight(carb == 8) +
  geom_label(aes(label = n))

This would be an attempt of doing the same with ggblanket (with fewer lines of code) but it gives me the following error

Error in quo_get_expr():
! quo must be a quosure

count(mtcars, carb) %>%
  ggblanket::gg_col(x = carb, y = n,
                    x_breaks = breaks_width(1)) +
  gghighlight::gghighlight(carb == 8)

Does anyone knows how to fix that?

This function is part of the {rlang} world. Looking at the gg_col() signature

x_breaks
A function on the limits (e.g. scales::breaks_pretty()), or a vector of breaks.

seems the likely source. Try scales::breaks_pretty() with no argument instead?

Tried that too, even keeping just the basic, it still doesn't work

count(mtcars, carb) %>% 
  ggblanket::gg_col(x = carb, y = n) + 
  gghighlight::gghighlight(carb == 8)

So I never heard of ggblanket, and I never used gghighlight. However, if I assign the plot call to a variable (omitting the gghighlight chunk) and do summary:

library(tidyverse)
library(ggblanket)
library(gghighlight)

p = count(mtcars, carb) %>% 
  ggblanket::gg_col(x = carb, y = n)
#> ℹ For further ggblanket information, see https://davidhodge931.github.io/ggblanket/
#> This message is displayed once every 8 hours.
#> Warning in ggplot2::geom_col(stat = stat, position = position, alpha = alpha, :
#> Ignoring unknown parameters: `stat`
summary(p)
#> data: carb, n [6x2]
#> mapping:  x = ~carb, y = ~n, colour = , fill = , group = NULL
#> scales:   x, xmin, xmax, xend, xintercept, xmin_final, xmax_final, xlower, xmiddle, xupper, x0, y, ymin, ymax, yend, yintercept, ymin_final, ymax_final, lower, middle, upper, y0, colour, fill 
#> faceting: <ggproto object: Class FacetNull, Facet, gg>
#>     compute_layout: function
#>     draw_back: function
#>     draw_front: function
#>     draw_labels: function
#>     draw_panels: function
#>     finish_data: function
#>     init_scales: function
#>     map_data: function
#>     params: list
#>     setup_data: function
#>     setup_params: function
#>     shrink: TRUE
#>     train_scales: function
#>     vars: function
#>     super:  <ggproto object: Class FacetNull, Facet, gg>
#> -----------------------------------
#> geom_col: just = 0.5, width = NULL, na.rm = FALSE
#> stat_identity: na.rm = FALSE
#> position_stack

This line stuck out to me. Notice the blank colour and fill. My assumption is that this is not playing well with gghighlight

#> mapping:  x = ~carb, y = ~n, colour = , fill = , group = NULL

Therefore, if we supply a variable in the col argument for gg_col, then this works. Perhaps this is a bug/enhancement for ggblanket?

library(tidyverse)
library(ggblanket)
library(gghighlight)

count(mtcars, carb) %>% 
  ggblanket::gg_col(x = carb, y = n, col = carb) +
  gghighlight::gghighlight(carb == 8)
#> Warning in ggplot2::geom_col(stat = stat, position = position, alpha = alpha, :
#> Ignoring unknown parameters: `stat`

Created on 2023-01-04 by the reprex package (v2.0.1)

1 Like

Here's what I think is going on

# this has to arise from a line in gghighlight() that calls
# enquo(label_key), which is NULL by default in gghighlight()
# label_key is supposed to be a variable name, not a value
d <- data.frame(
  H1 = c(1, 5), H2 = c(4, 7), H3 = c(12, 17), H4 = c(21, 32), H5 = c(25, 41), H6 = c(52, 64),
  F1 = c(2, 8), F2 = c(9, 10), F3 = c(18, 21), F4 = c(21, 32), F5 = c(31, 49), F6 = c(59, 68)
)
# this is what a sucessful call looks like
rlang::enquo(d)
#> <quosure>
#> expr: ^<df[,12]>
#> env:  empty

And the problem is definitely coming from something that gg_col() does.

library(dplyr)
library(ggblanket)
library(gghighlight)

ggplot(mtcars,aes(carb,gear)) + 
  geom_point() +
  gghighlight(carb == 8) +
  theme_minimal()

image

gg_col() complains about an ungroup() attempt. The function is kinda long, so it will take no little effort to run to ground. (And the attempt to get only 8 fails, too.)

1 Like

Adding an argument to col worked! the only issue is that it sometimes displays a legend on the side (so we have to remove it and change the colour aesthetic - but still better than writing a lot of lines :slight_smile:

1 Like

This topic was automatically closed 7 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.