一个方面内的R ggplot2方面

R ggplot2 facet within a facet

问题

我正在尝试绘制一个时间序列来回答调查中的问题。

id 只是行号

question_id 是每天每个调查提出的问题数量(一组 11 个问题)

category_id 是问题的类别 --> 这是我想可视化到方面的变量

但是在类别 1、3 和 5 中,提出了不止 1 个问题。

我想把facet numbers 1, 3 和 5 分别分成2, 3, 2 行,这样我可以清楚地看到不同的响应是什么 question_id个数字。

可重现的数据

id <- 1:55 

question_id <- structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 
                           2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 
                           6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
                           10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), 
                         .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"), 
                         class = "factor")


category_id <- structure(c(1L, 1L, 2L, 3L, 3L, 3L, 4L, 5L, 5L, 6L, 7L, 1L, 1L, 
                           2L, 3L, 3L, 3L, 4L, 5L, 5L, 6L, 7L, 1L, 1L, 2L, 3L, 3L, 3L, 4L, 
                           5L, 5L, 6L, 7L, 1L, 1L, 2L, 3L, 3L, 3L, 4L, 5L, 5L, 6L, 7L, 1L, 
                           1L, 2L, 3L, 3L, 3L, 4L, 5L, 5L, 6L, 7L), .Label = c("1", "2", 
                                                                               "3", "4", "5", "6", "7"), class = "factor")


date <- structure(c(24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 
                    24L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 33L, 
                    33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 36L, 36L, 36L, 
                    36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 39L, 39L, 39L, 39L, 39L, 
                    39L, 39L, 39L, 39L, 39L, 39L), .Label = c("", "1/07/2017", "1/08/2017", 
                                                              "1/09/2017", "10/07/2017", "10/08/2017", "10/09/2017", "11/07/2017", 
                                                              "11/08/2017", "12/07/2017", "12/08/2017", "13/07/2017", "13/08/2017", 
                                                              "14/07/2017", "14/08/2017", "15/07/2017", "15/08/2017", "16/07/2017", 
                                                              "16/08/2017", "17/07/2017", "17/08/2017", "18/07/2017", "18/08/2017", 
                                                              "19/06/2017", "19/07/2017", "19/08/2017", "2/07/2017", "2/08/2017", 
                                                              "2/09/2017", "20/06/2017", "20/07/2017", "20/08/2017", "21/06/2017", 
                                                              "21/07/2017", "21/08/2017", "22/06/2017", "22/07/2017", "22/08/2017", 
                                                              "23/06/2017", "23/07/2017", "23/08/2017", "24/06/2017", "24/07/2017", 
                                                              "24/08/2017", "25/06/2017", "25/07/2017", "25/08/2017", "26/06/2017", 
                                                              "26/07/2017", "26/08/2017", "27/06/2017", "27/07/2017", "27/08/2017", 
                                                              "28/06/2017", "28/07/2017", "28/08/2017", "29/06/2017", "29/07/2017", 
                                                              "29/08/2017", "3/07/2017", "3/08/2017", "3/09/2017", "30/06/2017", 
                                                              "30/07/2017", "30/08/2017", "31/07/2017", "31/08/2017", "4/07/2017", 
                                                              "4/08/2017", "4/09/2017", "5/07/2017", "5/08/2017", "5/09/2017", 
                                                              "6/08/2017", "6/09/2017", "7/08/2017", "7/09/2017", "8/08/2017", 
                                                              "8/09/2017", "9/08/2017", "9/09/2017"), class = "factor")



response <- structure(c(4L, 2L, 3L, 7L, 2L, 7L, 5L, 3L, 1L, 1L, 3L, 4L, 2L, 
                        3L, 4L, 5L, 6L, 1L, 3L, 4L, 1L, 7L, 2L, 1L, 4L, 1L, 1L, 2L, 5L, 
                        1L, 1L, 1L, 6L, 1L, 1L, 3L, 2L, 2L, 1L, 6L, 1L, 1L, 2L, 5L, 1L, 
                        1L, 4L, 1L, 1L, 1L, 7L, 1L, 1L, 1L, 6L), .Label = c("0", "1", 
                                                                            "2", "3", "4", "5", "6"), class = "factor")


df <- data.frame(id, question_id, category_id, date, response)

我试过的

library(ggplot2)
library(tidyverse)

df %>%
  ggplot(aes(date, 
             response, 
             group = 1,
             colour = factor(df$question_id))) +
  geom_point() +
  geom_line() +
  facet_wrap(~ category_id, scale = "free_y", ncol = 2) +
  theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
  labs(title = "Responses by Category",
       subtitle = "",
       color = "Question ID")

  1. 对于例如 23 日的第 3 个方面,响应重叠并且仅显示一个值。我想将每个 question_id

  2. 分成 3 个部分
  3. 我希望颜色也显示 question_id 数字

谢谢:)

更新的答案(查看以前版本的编辑)

df %>%
  ggplot(aes( x = date, 
              y = response, 
              color = question_id ) ) +
  geom_point() +
  facet_wrap( vars( category_id, question_id ), 
              scale = "free_y", 
              ncol = 2, 
              labeller = label_both ) +
  theme( axis.text.x = element_text( angle = 30, hjust = 1 ) ) +
  labs(title = "Responses by Category",
       subtitle = "",
       color = "Question ID")