Adding multiple graphs on the same plot

Hey all, I'm trying to have these three graphs on one plot and have no idea where to start. I'm using survey data that I found where quest_number is the question number that i'm looking at and i want each line to be the specified demographic. On each plot I put the response level on the x-axis and the percentage that gave that response on the y- axis. I would appreciate any guidance

  survey %>% 
  filter(quest_number == "1", demo_breakdown == "age_18_34" ) %>% 
  ggplot()+
  geom_line(mapping = aes(x = numbered_level, y = est_percentage))
  survey %>% 
    filter(quest_number == "1", demo_breakdown == "age_35_64" ) %>% 
    ggplot()+
    geom_line(mapping = aes(x = numbered_level, y = est_percentage))
  survey %>% 
    filter(quest_number == "1", demo_breakdown == "age_35_64" ) %>% 
    ggplot()+
    geom_line(mapping = aes(x = numbered_level, y = est_percentage))

I can't be sure with out a REPRoducible EXample (reprex) but I think this could work

survey %>% 
    filter(quest_number == "1") %>% 
    ggplot()+
    geom_line(mapping = aes(x = numbered_level, y = est_percentage, color = demo_breakdown))

that should work! it's just that i only want to use x/10 of the demo breakdowns. I tried to use something like c("age_18_34", "age_35_64", "age_35_64") but that didn't work

Maybe something like this?

survey %>%
    filter(quest_number == "1", demo_breakdown %in% c("age_18_34", "age_35_64", "age_35_64")) %>%
    ggplot() +
    geom_line(mapping = aes(x = numbered_level, y = est_percentage, color = demo_breakdown))

If this doesn't work please provide a proper reprex as explained in the link I gave you before, otherwise, we are going to be trapped on an endless back and forth.

1 Like

This worked! Thank you so much! You just saved final grade

I tried to follow it but I got stuck after I after I had the rendered reprex in my clipboard. But I was able to get it into a tribble:

survey_data <- tibble::tribble(
  ~quest_number, ~numbered_level,             ~response_level, ~demo_breakdown, ~est_percentage,
             7L,               6,                     "Often",     "age_18_34",              21,
             7L,               5,                 "Sometimes",     "age_18_34",              25,
             7L,               4,                    "Rarely",     "age_18_34",              19,
             7L,               3,     "Never, but open to it",     "age_18_34",              14,
             7L,               2, "Never, and not open to it",     "age_18_34",              21,
             7L,               1,                 "No answer",     "age_18_34",               0,
             8L,               6,                     "Often",     "age_18_34",              13,
             8L,               5,                 "Sometimes",     "age_18_34",              35,
             8L,               4,                    "Rarely",     "age_18_34",              30,
             8L,               3,     "Never, but open to it",     "age_18_34",               8,
             8L,               2, "Never, and not open to it",     "age_18_34",              14,
             8L,               1,                 "No answer",     "age_18_34",               0
  )

I's trying to do pretty much the same thing as before except instead of demographic levels for 1 question i want to see the same demographic for two questions. I tried just swapping where need be but then the graph melded the two lines together

  survey_data %>%
    filter(quest_number %in% c("7","8"), demo_breakdown == "age_18_34") %>%
    ggplot() +
    geom_line(mapping = aes(x = numbered_level, y = est_percentage, color = quest_number))

quest_number is a numeric variable, so you can't compare it to characters (i.e. 7 != "7"), also, I suspect you want to treat that variable as a factor so you would have to convert it, see this example (BTW this would be a proper reprex for your issue).

library(tidyverse)

survey_data <- tibble::tribble(
    ~quest_number, ~numbered_level,             ~response_level, ~demo_breakdown, ~est_percentage,
    7L,               6,                     "Often",     "age_18_34",              21,
    7L,               5,                 "Sometimes",     "age_18_34",              25,
    7L,               4,                    "Rarely",     "age_18_34",              19,
    7L,               3,     "Never, but open to it",     "age_18_34",              14,
    7L,               2, "Never, and not open to it",     "age_18_34",              21,
    7L,               1,                 "No answer",     "age_18_34",               0,
    8L,               6,                     "Often",     "age_18_34",              13,
    8L,               5,                 "Sometimes",     "age_18_34",              35,
    8L,               4,                    "Rarely",     "age_18_34",              30,
    8L,               3,     "Never, but open to it",     "age_18_34",               8,
    8L,               2, "Never, and not open to it",     "age_18_34",              14,
    8L,               1,                 "No answer",     "age_18_34",               0
)

survey_data %>%
    filter(quest_number %in% c(7, 8), demo_breakdown == "age_18_34") %>%
    ggplot() +
    geom_line(mapping = aes(x = numbered_level, y = est_percentage, color = as.factor(quest_number)))

Created on 2019-12-08 by the reprex package (v0.3.0.9000)

It seems like you need to work a little bit on your basic R skills, I would recommend this free on-line book for covering the basics

2 Likes

Thank you! I'll take a look at the book.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.