-1

So I am trying to graph enrollment rates for subgroups within race using bar charts. I have four "cohorts" - an overall cohort, and then the class of 2012, 2013, and 2014. Each grid is one of the four cohorts. I have a dataset with each of the subgroup enrollment rates for each cohort . Then, I have a dataset with the overall rate of enrollment for each cohort. I want the subgroup rates to bars but I want to plot the overall rate as a single line across all bars so you can see what the overall rate was for the overall cohort, class of 2012, 2013, and 2014. I can't seem to figure out how to plot a single line for the overall rate. Each cohort's overall rate is different so I can't just plot the same line across all four grids. Here's what I have. Test has the subgroup rates. Test2 have the overall rates

g <- ggplot(test, aes(x=Group, y= Rate, fill= Group)) +
  geom_bar(stat = "identity") + 
  geom_line(test2, mapping = (aes(x= Group, y = Rate))) +
  facet_grid(factor(test$Cohort))
vpatel
  • 43
  • 4
  • 1
    It will be easier to help if you can share some fake data that we could use to run the code. Otherwise, each person who wants to try helping will need to guess and make their own sample data. Can you share the output of running `dput(test)` into the body of your question? – Jon Spring Jul 27 '22 at 22:24
  • Does this answer your question? [line graph of four variables in r in just one plot](https://stackoverflow.com/questions/73123071/line-graph-of-four-variables-in-r-in-just-one-plot) – socialscientist Jul 28 '22 at 00:15

1 Answers1

0
test <- data.frame(Cohort = rep(2012:2014, each = 3),
                   Group = rep(LETTERS[1:3], by = 3),
                   Rate = 1:9)
test2 <- data.frame(Cohort = 2012:2014,
                    Rate = 2 + 3*(0:2))

ggplot(test, aes(x=Group, y= Rate, fill= Group)) +
  geom_bar(stat = "identity") + 
  geom_hline(data = test2, aes(yintercept = Rate)) +
  facet_grid(~Cohort)

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53