11

I can plot each series in a different colour in ggplot2 by doing something like this ...

colours <- c('red', 'blue')
p <- ggplot(data=m, mapping=aes_string(x='Date', y='value'))
p <- p + geom_line(mapping=aes_string(group='variable', colour='variable'), size=0.8)
p <- p + scale_colour_manual(values=colours)

Is there something comparable I can do to set different line widths for each series? (Ie. I want to use a thick red line to plot the trend and a thin blue line to plot the seasonally adjusted series.)

Mark Graph
  • 4,969
  • 6
  • 25
  • 37

2 Answers2

13

I would just add a new numeric variable to your data frame

##You will need to change this to something more appropriate
##Something like: 
##m$size = as.numeric(m$variable == "seasonal")
m$size = rep(c(0, 1), each=10)

then add a size aesthetic to your plot command:

p = p + geom_line(aes(group=variable, colour=variable, size=size))
##Set the size scale
p + scale_size(range=c(0.1, 2), guide=FALSE)

Notice that I've added guide=FALSE to avoid the size legend being displayed.

csgillespie
  • 59,189
  • 14
  • 150
  • 185
8

You could do it like:

x <- 1:10
y1 <- x
y2 <- 1.5*x
df <- data.frame(x=rep(x, 2), y=c(y1, y2), id=as.factor(rep(1:2, each=10)))
ggplot(df) + geom_line(aes(x=x,y=y,group=id, colour=id, size=id)) +  
scale_size_manual(values=c(1,4))
Simon Müller
  • 368
  • 2
  • 5