waffle
waffle copied to clipboard
error using facet_grid() with more than one faceting variable - invalid 'times' argument
While facet_wrap()
works like a charm, facet_grid()
leads to an error when used with a second (or more) faceting variable.
I attached a reprex below.
Thanks for a great package!
library(hrbrthemes)
library(waffle)
#> Loading required package: ggplot2
library(tidyverse)
tibble(
parts = factor(rep(month.abb[1:3], 3), levels=month.abb[1:3]),
values = c(10, 20, 30, 6, 14, 40, 30, 20, 10),
fct = c(rep("Thing 1", 3), rep("Thing 2", 3), rep("Thing 3", 3)),
fct2 = c(rep('a', 4), rep('b', 5))
) -> xdf
ggplot(xdf, aes(fill=parts, values=values)) +
geom_waffle(color = "white", size=1.125, n_rows = 10,
make_proportional = TRUE) +
facet_wrap(fct2~fct) +
scale_x_discrete(expand=c(0,0)) +
scale_y_discrete(expand=c(0,0)) +
ggthemes::scale_fill_tableau(name=NULL) +
coord_equal() +
labs(
title = "Faceted Waffle Geoms"
) +
theme_ipsum_rc(grid="") +
theme_enhance_waffle()
library(hrbrthemes)
library(waffle)
library(tidyverse)
tibble(
parts = factor(rep(month.abb[1:3], 3), levels=month.abb[1:3]),
values = c(10, 20, 30, 6, 14, 40, 30, 20, 10),
fct = c(rep("Thing 1", 3), rep("Thing 2", 3), rep("Thing 3", 3)),
fct2 = c(rep('a', 4), rep('b', 5))
) -> xdf
ggplot(xdf, aes(fill=parts, values=values)) +
geom_waffle(color = "white", size=1.125, n_rows = 10,
make_proportional = TRUE) +
facet_grid(fct2~fct) +
scale_x_discrete(expand=c(0,0)) +
scale_y_discrete(expand=c(0,0)) +
ggthemes::scale_fill_tableau(name=NULL) +
coord_equal() +
labs(
title = "Faceted Waffle Geoms"
) +
theme_ipsum_rc(grid="") +
theme_enhance_waffle()
#> Error in rep(as.character(.x[[use]][i]), .x[["values"]][i]): invalid 'times' argument
Created on 2019-07-24 by the reprex package (v0.3.0)
incredible reprex! #ty! will take a look (likely at the tail end of the week)
Hi Bob, I encountered the same problem while using ggfacet
package and waffle
. I guess this is because ggfacet
uses facet_grid()
from ggplot2
. I wanted to create something similar to this map from NYT:

incredible reprex! #ty! will take a look (likely at the tail end of the week)
@hrbrmstr Did you ever figure this out?
I believe this error occurs when the data does not contain every combination of the two facet_grid()
variables. For example, there is no row in xdf
with Thing 1
in fct
and a
in fct2
.
However, if you include all the missing combination rows with a value of zero you get one of two different errors depending on whether or not make_proportional
in geom_waffle()
is TRUE
or FALSE
.
library(waffle)
library(tidyverse)
xdf <- tibble(
parts = factor(rep(month.abb[1:3], 3), levels=month.abb[1:3]),
values = c(10, 20, 30, 6, 14, 40, 30, 20, 10),
fct = c(rep("Thing 1", 3), rep("Thing 2", 3), rep("Thing 3", 3)),
fct2 = c(rep('a', 4), rep('b', 5))
) %>%
bind_rows(
tribble(
~parts, ~values, ~fct, ~fct2,
"Jan", 0, "Thing 1", "b",
"Feb", 0, "Thing 1", "b",
"Mar", 0, "Thing 1", "b",
"Feb", 0, "Thing 2", "a",
"Mar", 0, "Thing 2", "a",
"Jan", 0, "Thing 3", "a",
"Feb", 0, "Thing 3", "a",
"Mar", 0, "Thing 3", "a"
)
)
ggplot(xdf, aes(fill = parts, values = values)) +
geom_waffle(color = "white", size = 1.125, n_rows = 10) +
facet_grid(fct2 ~ fct) +
scale_x_discrete(expand = c(0,0)) +
scale_y_discrete(expand = c(0,0)) +
coord_equal() +
labs(title = "Faceted Waffle Geoms") +
theme_enhance_waffle()
Error in `[[<-.data.frame`(`*tmp*`, "values", value = NA) :
replacement has 1 row, data has 0
ggplot(xdf, aes(fill = parts, values = values)) +
geom_waffle(color = "white", size = 1.125, make_proportional = TRUE, n_rows = 10) +
facet_grid(fct2 ~ fct) +
scale_x_discrete(expand = c(0,0)) +
scale_y_discrete(expand = c(0,0)) +
coord_equal() +
labs(title = "Faceted Waffle Geoms") +
theme_enhance_waffle()
Error in checkHT(n, dx <- dim(x)) :
invalid 'n' - must contain at least one non-missing element, got none.
@nsgrantham for completeness, I believe if you use an artificially small number (e.g. 1E-6) instead of zero (0), this actually works.
Just to help expand on this, here's a working sample based on @JasonAizkalns's suggestion
#library(hrbrthemes)
library(waffle)
library(tidyverse)
tibble(
parts = factor(rep(month.abb[1:3], 3), levels=month.abb[1:3]),
values = c(10, 20, 30, 6, 14, 40, 30, 20, 10),
fct = c(rep("Thing 1", 3), rep("Thing 2", 3), rep("Thing 3", 3)),
fct2 = c(rep("Thing 1", 4), rep("Thing 2", 5))
) -> xdf
xdf <-
xdf %>%
complete(parts, fct, fct2, fill= list(values = 0.00001))
ggplot(xdf, aes(fill=parts, values=values)) +
geom_waffle(color = "white", size=1.125, n_rows = 6) +
#facet_wrap(~fct2, ncol=1) +
facet_grid(rows=vars(fct), cols=vars(fct2)) +
scale_x_discrete(expand=c(0,0)) +
scale_y_discrete(expand=c(0,0)) +
#ggthemes::scale_fill_tableau(name=NULL) +
coord_equal() +
labs(
title = "Faceted Waffle Geoms"
) +
#theme_ipsum_rc(grid="") +
theme_enhance_waffle()