Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variable with line types to ggplot linetype

Tags:

r

ggplot2

charts

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?

example plot

like image 590
ako Avatar asked Jul 30 '12 01:07

ako


People also ask

How do I change the line type in ggplot2?

Change manually the appearance of lines The functions below can be used : scale_linetype_manual() : to change line types. scale_color_manual() : to change line colors. scale_size_manual() : to change the size of lines.

How do I change the thickness of a line in ggplot2?

To change line width, just add argument size=2 to geom_line().


1 Answers

The trick with using scale_..._manual is often to send a named vector as the value argument. The setNames function is good for this

First, some dummy data

## some dummy data 
simulations<- expand.grid(year = 2012:2020, geography = paste0('a',1:35))
library(plyr)
library(RColorBrewer)
simulation_long_index <- ddply(simulations, .(geography), mutate, 
  value = (year-2012) * runif(1,-2, 2) + rnorm(9, mean = 0, sd = runif(1, 1, 3)))
## create a manyColors function 
manyColors <- colorRampPalette(brewer.pal(name = 'Set3',n=11))

Next we create a vector that is a random sample from 1:12 (with replacement) and set the names the same as the geography variable

lty <- setNames(sample(1:12,35,T), levels(simulation_long_index$geography))

This is what it looks like

lty
## a1  a2  a3  a4  a5  a6  a7  a8  a9 a10 a11 a12 a13 a14 a15 a16 
## 7   5   8  11   2  10   3   2   5   4   6   6  11   8   2   2 
## a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 a31 a32 
## 12   7   6   8  11   5   1   1   8  12   8   1  12   2   3   5 
## a33 a34 a35 
#7   1   3 

Now you can use line_type = geography in conjunction with scale_linetype_manual(values = lty)

ggplot(data=simulation_long_index,
        aes(
          x=as.factor(year), 
          y=value, 
          colour=geography,
          group=geography,
          linetype = geography))+
            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)) +
            scale_linetype_manual(values = lty)

Which gives you

enter image description here

As an aside, do you really want to plot the years as a factor variable?

like image 194
mnel Avatar answered Sep 23 '22 04:09

mnel