0

I have variables from a list (year, class size, and tuition) that I would like to join in a table and then create a time series plot using year and class size. However, when I use ggplot, an error says it doesn't know how to deal with list data. Here's my code:

univ.ann <- results1[c(as.character(1996:2015))]

size <- sapply(univ.ann, function(LIST) LIST$student$size) %>% 
  unlist()

size_df <- data.frame(classsize = size) %>% 
  rownames_to_column(var = "year") 

tuition <- sapply(univ.ann, function(LIST) LIST$cost$tuition$in_state) %>% 
  unlist()

tuition_df <- data.frame(in_state = tuition) %>%
  rownames_to_column(var = "year") 

full_join(size_df, tuition_df, by = c("year" = "year")) %>% 
  arrange(year) 

Everything at this point works, however, a plot fails to show up when I execute the following code chunk:

university_plot <- ggplot(univ.ann, aes(year)) + 
  geom_point(aes(y=size_df)) + 
  ylab("Size") + 
  xlab("Year") + 
  ggtitle("Class Size Plot")

university_plot

Is there a way to use ggplot for list variables?

alistaire
  • 42,459
  • 4
  • 77
  • 117
Jin Yu Li
  • 103
  • 3
  • 12
  • 4
    You should provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can run and test the code. ggplot exists `data.frames` or `tibbles` or things that can be coerced to either. Generic lists are not allowed (but you can probably convert it yourself with `as.data.frame()`). – MrFlick Oct 20 '17 at 18:58
  • 1
    What is `results1`?? – Prradep Oct 20 '17 at 20:17
  • @Prradep results1 is a table showing the class size and tuition from 1996 to 2015. – Jin Yu Li Oct 25 '17 at 17:41

0 Answers0