0

I'm trying to create a 3x4 grid of bins with a count of observations from my data in each bin. I'm halfway there:

library(ggplot2)
type <- c('daily', 'daily', 'habit', 'habit', 'habit', 'seasonal', 'seasonal', 'year-long', 'year-long', 'year-long', 'year-long', 'year-long')
status <- c('complete', 'incomplete', 'push', 'push', 'incomplete', 'complete', 'complete', 'complete', 'complete', 'push', 'push', 'incomplete')
results <- data.frame(type, status)

ggplot(results) +
  geom_dotplot(aes(x=status, y=type), binwidth=.2) +
  facet_grid(vars(type))

result

Clearly the y-axis is meaningless right now - I'm not sure if geom_dotplot is the right thing to use. I can get similar results with geom_histogram or geom_bar as well.

I'd like dots to start in the upper left corner of each bin. Is this possible? I've tried to use the waffle package but can't figure it out. Ideally each dot will be a square, which isn't possible using geom_dotplot, so I'd prefer to use waffle if I can.

braden
  • 79
  • 7
  • What is the code you tried using `waffle` package? Also can you sketch desired result? – markus Jan 04 '20 at 18:41
  • ```ggplot(results, aes(fill=type, values=sum(as.integer(status)) )) + geom_waffle() + facet_wrap(status~type) + coord_equal()``` Not sure what my result was doing here...also encountered the problem with the `waffle` package mentioned below ("invalid 'times' argument with `facet_grid`) – braden Jan 04 '20 at 21:44

1 Answers1

2

There is currently an open issue with that waffle package which prevented me from producing the chart the way I wanted to (getting an error "invalid 'times' argument"), using facet_grid along both status and type.

Alternatively, we can do some data prep to create something similar.

rows <- 4
cols <- 4

results %>%
  arrange(status, type) %>%
  group_by(status, type) %>%
  mutate(num = row_number(),
         x_pos = (num - 1) %/% rows,
         y_pos = rows - (num - 1) %% rows - 1) %>%

  ggplot(aes(x = x_pos, y = y_pos)) +
  geom_tile(fill = "black", color = "white") +
  coord_equal(xlim = c(0, cols) - 0.5,
              ylim = c(0, rows) - 0.5) +
  facet_grid(vars(type), vars(status)) +
  theme_light() +
  theme(panel.grid = element_blank(),
        axis.text = element_blank())

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Thank you! Exactly what I needed. I was thinking I needed to add something to the data to make it easier but could not figure it out. – braden Jan 04 '20 at 21:48