1

Problem

I'm trying to plot a time series for responses to questions in a survey.

id is just row number

question_id is the question number (set of 11 questions) asked per survey per day

category_id is the category of the question --> this is the variable i want to visualize into facets

However in category 1, 3 and 5, there are more than 1 question asked.

I want to break the facet numbers 1, 3 an 5 into 2, 3 and 2 rows respectively, so that I can clearly see what response was given for different question_id numbers.

Reproducible data

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) 

What I've tried

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. For facet number 3 for example on the 23rd, the responses overlap and show just one value. I'd like to separate into 3 parts for each question_id

  2. I want the colours to show question_id number as well

Thanks :)

0

1 Answer 1

2

updated answer (see edits for previous versions)

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Wimpel, the second one works better for me as I have many more dates in my actual data.
@Yeshyyy, if the answer provided was the one you were looking for, then please let me know by accepting the asnwer.
may I suggest that you edit your response. If you add group = 1 in ggplot(aes()), there are no warning errors and geom_line() works. @Wimpel The colour coding doesn't work. Is there a way to have one colour for each question_id?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.