0

我正在尝试创建一个 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_histogram使用or也可以得到类似的结果geom_bar

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

4

1 回答 1

2

该华夫饼包目前存在一个未解决的问题,这使我无法在状态和类型中使用 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())

在此处输入图像描述

于 2020-01-04T19:14:18.707 回答