如何制作多面华夫饼图,从左上角填充每个箱子

How to make a faceted waffle chart, filling each bin from top left

我正在尝试创建一个 3x4 的 bin 网格,其中包含每个 bin 中我的数据的观察计数。我已经完成一半了:

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

显然 y 轴现在没有意义 - 我不确定 geom_dotplot 是否正确使用。我也可以用 geom_histogramgeom_bar 得到类似的结果。

我希望每个垃圾桶的左上角开始有圆点。这可能吗?我试过使用 waffle package 但无法弄清楚。理想情况下,每个点都是一个正方形,使用 geom_dotplot 是不可能的,所以如果可以的话,我更愿意使用 waffle

目前有一个打开的 issue 和那个华夫饼包,这使我无法按照我想要的方式生成图表(出现错误 "invalid 'times' argument"),同时使用 facet_grid状态和类型。

或者,我们可以做一些数据准备来创建类似的东西。

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