Skip to content
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

Phantom factor level being added to color aesthetic #75

Open
camille-s opened this issue Mar 6, 2020 · 0 comments
Open

Phantom factor level being added to color aesthetic #75

camille-s opened this issue Mar 6, 2020 · 0 comments

Comments

@camille-s
Copy link

First off, really appreciate this package and the thought that obviously went into it.

I've run across what I think is a bug illustrated below. I've made a waffle chart, and it's great:

library(ggplot2)
library(waffle)

race <- structure(list(group = structure(1:4, .Label = c("White", "Hispanic", "Black", "Other race"), class = "factor"), 
                           share = c(67, 16, 10, 7)), class = c("tbl_df", "tbl", "data.frame"), 
                      row.names = c(NA, -4L))

# no issue
ggplot(race) +
  geom_waffle(aes(fill = group, values = share), flip = TRUE)

However, I'm using a more polished version of this for a print document, and would like to add direct labels, one per group and with colors matching the fill of the waffle squares. I've done that by pulling out coordinates with ggplot_build and then adding a geom_text layer to the waffle plot. But when I add something with a color aesthetic—here via some dummy text—an additional level "white" gets added to the factor that makes the colors, and their levels get reset back to alphabetical order. One attempt setting color = "white" manually, which still sets the border color properly but then adds this phantom level that shows up in the legend:

p_race <- ggplot(race) +
  geom_waffle(aes(fill = group, values = share), flip = TRUE, color = "white") +
  geom_text(aes(x = 5, y = 5, label = "dummy text", color = group))
p_race

Oddly, even with falling back on the default color argument (or setting it to any other color), this level still shows up:

# still has white factor level
ggplot(race) +
  geom_waffle(aes(fill = group, values = share), flip = TRUE) +
  geom_text(aes(x = 5, y = 5, label = "dummy text", color = group))

I'm guessing something gets confused with having color arguments come from multiple places (or multiple spellings?). Here's the calculated data:

head(ggplot_build(p_race)$data[[1]])
#>      fill colour y x values PANEL group xmin xmax ymin ymax alpha  size
#> 1 #F8766D  white 1 1     NA     1     1  0.5  1.5  0.5  1.5    NA 0.125
#> 2 #F8766D  white 1 2     NA     1     2  1.5  2.5  0.5  1.5    NA 0.125
#> 3 #F8766D  white 1 3     NA     1     3  2.5  3.5  0.5  1.5    NA 0.125
#> 4 #F8766D  white 1 4     NA     1     4  3.5  4.5  0.5  1.5    NA 0.125
#> 5 #F8766D  white 1 5     NA     1     5  4.5  5.5  0.5  1.5    NA 0.125
#> 6 #F8766D  white 1 6     NA     1     6  5.5  6.5  0.5  1.5    NA 0.125
#>   linetype width height
#> 1        1    NA     NA
#> 2        1    NA     NA
#> 3        1    NA     NA
#> 4        1    NA     NA
#> 5        1    NA     NA
#> 6        1    NA     NA

Obviously I can work around this: using a named vector for colors with a dummy white = NA, or pulling the calculated data out and rebuilding the waffle squares myself. But I wanted to see if there is in fact a bug, or if I'm just trying to make it do too much.

Thanks in advance.

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

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

1 participant