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

error using facet_grid() with more than one faceting variable - invalid 'times' argument #66

Open
konradmayer opened this issue Jul 24, 2019 · 6 comments
Assignees
Labels

Comments

@konradmayer
Copy link

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)

@hrbrmstr hrbrmstr self-assigned this Jul 24, 2019
@hrbrmstr hrbrmstr added the bug label Jul 24, 2019
@hrbrmstr
Copy link
Owner

incredible reprex! #ty! will take a look (likely at the tail end of the week)

@ashgreat
Copy link

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:

Screen Shot 2020-12-14 at 10 01 38 AM

@andrewvanleuven
Copy link

incredible reprex! #ty! will take a look (likely at the tail end of the week)

@hrbrmstr Did you ever figure this out?

@nsgrantham
Copy link

nsgrantham commented Sep 21, 2021

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.

@JasonAizkalns
Copy link

@nsgrantham for completeness, I believe if you use an artificially small number (e.g. 1E-6) instead of zero (0), this actually works.

@slodge
Copy link

slodge commented Oct 6, 2022

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()

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

No branches or pull requests

7 participants