Skip to content

Boxplot color can't have a continuous variable when faceting #6395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JacobBumgarner opened this issue Apr 3, 2025 · 3 comments
Closed

Boxplot color can't have a continuous variable when faceting #6395

JacobBumgarner opened this issue Apr 3, 2025 · 3 comments

Comments

@JacobBumgarner
Copy link

Code

For some reason the boxplot layer isn't able to have a continuous variable (other than the one used to define the group, e.g., x in the plot below):

ggplot2::ggplot(
    mtcars,
    ggplot2::aes(x = factor(cyl), y = hp, color = hp)
) + 
    ggplot2::geom_point() + 
    ggplot2::geom_boxplot(outliers = FALSE) + 
    ggplot2::facet_wrap(gear ~ am)

Image

Warning on plot

> ggplot2::ggplot(
+     mtcars,
+     ggplot2::aes(x = factor(cyl), y = hp, color = hp)
+ ) + 
+     ggplot2::geom_point() + 
+     ggplot2::geom_boxplot(outliers = FALSE) + 
+     ggplot2::facet_wrap(gear ~ am)
Warning: The following aesthetics were dropped during statistical transformation: colour.
ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into
  a factor?
Warning: The following aesthetics were dropped during statistical transformation: colour.
ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into
  a factor?
Warning: The following aesthetics were dropped during statistical transformation: colour.
ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into
  a factor?
Warning: The following aesthetics were dropped during statistical transformation: colour.
ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into
  a factor?
@Yunuuuu
Copy link
Contributor

Yunuuuu commented Apr 5, 2025

The issue arises because multiple colors are being assigned within each boxplot group, whereas each box should have a single, consistent color. To resolve this, you can summarize the color per group — for example, using the mean (or median) of hp for each group:

ggplot2::ggplot(
    mtcars,
    ggplot2::aes(x = factor(cyl), y = hp, color = hp)
) +
    ggplot2::geom_point() +
    ggplot2::geom_boxplot(
        aes(group = cyl, color = color),
        outliers = FALSE,
        data = ~ dplyr::mutate(.x, color = mean(hp), .by = cyl)
    ) +
    ggplot2::facet_wrap(gear ~ am)

Image

@teunbrand
Copy link
Collaborator

This is indeed because you're giving instructions to give singular boxplots multiple colour values. The warning message indicates that there might be a conflict between the colour aesthetic and grouping structure. In your mind, what'd be the ideal outcome in this problem?

@JacobBumgarner
Copy link
Author

I see - sorry for the confusion, and thanks for the explanation @Yunuuuu

@teunbrand I believe I was imagining the color value would automatically have taken an average of the grouped values. But after thinking about this, I realize how big of an assumption this is.

I think this was after a long night of coding, so I was assuming very poorly... please disregard!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants