0
votes

My dataset has 3 columns: high school name, year, and percent enrolled in college, and it includes 104 high schools across 8 years.

school chrt_grad enrolled
Alba 2012 0.486
Alba 2013 0.593
Alba 2014 0.588
Alba 2015 0.588
Alba 2016 0.547
Alba 2017 0.613
Alba 2018 0.622
Alba 2019 0.599
Alba 2020 0.614
City 2012 0.588
City 2013 0.613

and so on...

I'm trying to produce 104 individual line plots--one for each school. I started by creating a single line plot showing every school:

ggplot(nsc_enroll,
         mapping = aes(x = chrt_grad, y = enrolled, group = school)) +
  geom_line() +
  geom_point()

How can I create an individual plot for each of the 104 schools without having to filter for each school name over and over again?

2

2 Answers

1
votes

You could use facet_wrap with ggplot,

ggplot(mtcars, aes(x = hp, y = mpg))+
        geom_point() +
        facet_wrap(~cyl)

enter image description here

In your case you would facet_wrap(~school), but it will produce a huge amount of plots.

0
votes

First of all, I suppose that where you write chrt_grad it's the same as year or do you have another variable? Anyway, it's not the point. As you may know, +facet could do multiple plots, but not individually as I know. I have a similar situation and what I would do, at least it works for me, is to:

  1. Spread (if you know gather/spread) variable school
  2. For loop to plot each column.

I am not at home now if you need it I can text the code.

Recently I saw some new dplyr tidyverse code using nest_by. It's very interesting although I haven't tried it yet.

enter image description here