2
votes

I am new to coding and to R and I have this data set that I would like to scatter plot with a line using the ggplot2 package. Basically, I want to show the number of tests for each grade (G3-G8) by year. I want all the grades on the "x" axes and the year on the "y" axes. I tried (tidyr/gather) and (reshape2/ melt) but I keep getting an error message.

Year    G3  G4  G5  G6  G7  G8
2003    6   10  8   4   6   8
2004    10  12  4   12  19  10
2005    11  9   13  10  13  11
2006    26  25  28  37  32  19
2007    5   1   3   4   3   1
2008    4   2   4   4   4   2
2009    6   4   8   8   8   6
2010    5   5   5   5   5   5
2011    8   8   8   8   8   8
2012    6   6   6   6   6   6
2013    3   3   3   3   3   3
2014    5   4   4   4   4   4
2015    60  60  60  60  60  60
2016    26  26  26  26  26  26
2017    6   6   6   6   6   6
2018    18  18  18  18  18  18
1
How did you try gather and melt? What error message did you get? - Gregor Thomas

1 Answers

2
votes

Here's an approach with pivot_longer from the tidyr package.

There is a slight disconnected between what you say in the first sentence "I want to show the number of tests for each grade (G3-G8) by year", and then your variable mapping in the next sentence. But perhaps you can edit it from here if this is not what you want.

library(tidyr)
library(ggplot2)
data %>%
  pivot_longer(-Year,names_to = "Grade", values_to = "NumberTests") %>%
ggplot(aes(x = Year, y = NumberTests, color = Grade)) +
  geom_line() + 
  geom_point()

enter image description here