Skip to Content

Passing variable with line types to ggplot linetype

I am new to ggplot so bear with me. I am charting out growth projections for 35 small-area geographies which is an unhealthy amount for one plot even with use of the fantastic directlabels library. However I need all the series for initial screening.

The challenge is to make it readable. I found a fix by @Ben Bolker for using large numbers of distinct colors but am having trouble varying the linetype. The 35 series don't need to be unique, but I would like to use the 12 different types to make individual series easier to read.

My plan was to create a random list with 35 elements of the 12 possible types and pass that as the linetype argument, but I am having trouble getting it to work, with the error:

Error: Aesthetics must either be length one, or the same length as the dataProblems:lty

I have 35 values in the linetype list. Of course I would like for the types, colors and all to be reflected in the legend.

The melted data looks like this; 9 years' observations for each of 35 series:

> simulation_long_index[16:24,]    
      year    geography    value
16    2018    sfr_2    101.1871
17    2019    sfr_2    101.1678
18    2020    sfr_2    101.2044
19    2012    sfr_3    100.0000
20    2013    sfr_3    100.1038
21    2014    sfr_3    100.2561
22    2015    sfr_3    100.0631
23    2016    sfr_3    100.8071
24    2017    sfr_3    101.2405    

Here is my code so far:

lty <- data.frame(lty=letters[1:12][sample(1:12, 35,replace=T)])
 
g3<-ggplot(data=simulation_long_index,
   aes(
     x=as.factor(year), 
     y=value, 
     colour=geography,
     group=geography,
     linetype=lty$lty))+
       geom_line(size=.65) + 
       scale_colour_manual(values=manyColors(35)) +
     geom_point(size=2.5) +
     opts(title="growth")+
     xlab("Year") + 
     ylab(paste("Indexed Value (Rel. to 2012")) + 
     opts(axis.text.x=theme_text(angle=90, hjust=0))
 
print(g3)

adding

    scale_linetype_manual("",values=lty$lty) +

after scale_color_manual instead of the linetype argument produces the chart, but lines are all the same. How, then, do I get the lines to vary for large series counts?