4

I'm trying to make a waffle plot to use dark mode. This involves changing both the background color and the color of the tile grout. I can't figure out how to do the tile grout.

I'm unable to do any of the normal operations to change the color:

counts <- c(a = 701, b = 1094, c = 1756)
waffle(counts, 
   rows=50,
   size=0.75,
   legend_pos="bottom") + theme(legend.key.size=unit(3, "mm"),
                                rect=element_rect(fill='black',
                                                  color='black'),
                                plot.background=element_rect(fill='black'),
                                strip.background = element_rect(colour=NA, fill=NA),
                                panel.background=element_rect(fill='black', color='black'))

enter image description here

There was a pull request to do this, but the person who made it deleted it. It seems like the color is set as a margin color on every tile, since if you increase the waffle function argument size to size=0, you get no tile grout:

enter image description here

How do I get the tile grout to be the same black as the background?

rawr
  • 20,481
  • 4
  • 44
  • 78
brienna
  • 1,415
  • 1
  • 18
  • 45
  • you would need to change "white" [here](https://github.com/hrbrmstr/waffle/blob/master/R/waffle.R#L196) – rawr Sep 28 '20 at 19:37
  • Yes, I see that in the deleted pull request, but for some reason I can't find the file waffle.R in my local copy... I typed in RStudio `.libPaths()` which gave me `/Library/Frameworks/R.framework/Versions/4.0/Resources/library`. I then opened that directory in my Terminal, and in waffle > R is no waffle.R? – brienna Sep 28 '20 at 19:41
  • 1
    you would have a compiled package, not the source files. you could probably just do `x$layers[[1]]$aes_params$colour <- 'black'; x` where `x` is the object returned by `waffle` – rawr Sep 28 '20 at 19:46
  • That works!! Could you make that into an answer? @rawr – brienna Sep 28 '20 at 19:48

1 Answers1

5

This seems pretty hacky, but you can edit what you need in a ggplot object before printing.

library('waffle')
counts <- c(a = 701, b = 1094, c = 1756)
x <- waffle(counts, 
       rows=50,
       size=0.75,
       legend_pos="bottom") + theme(legend.key.size=unit(3, "mm"),
                                    rect=element_rect(fill='black',
                                                      color='black'),
                                    plot.background=element_rect(fill='black'),
                                    strip.background = element_rect(colour=NA, fill=NA),
                                    panel.background=element_rect(fill='black', color='black'))

x$layers[[1]]$aes_params$colour <- 'black'
x

enter image description here

A more "ggplot way" would be something like editing/replacing the layer:

x %+replace% geom_tile(inherit.aes = TRUE, color = 'black')

but that does not work. I'm not sure if you can replace a layer in-place without messing up the order. But this is essentially what the hack does.

rawr
  • 20,481
  • 4
  • 44
  • 78