R ggplot2构面内的构面

时间:2018-10-05 06:23:37

标签: r ggplot2 facet

问题

我正在尝试为调查中的问题绘制时间序列。

id 只是行号

question_id 是每天每次调查所问的问题编号(11个问题集)

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

但是,在类别1、3和5中,提出的问题多于1个。

我想将构面编号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")

output

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

  2. 分为3部分
  3. 我希望颜色也显示 question_id 编号

谢谢:)

1 个答案:

答案 0 :(得分:2)

更新的答案(请参阅以前版本的修改内容)

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

enter image description here