Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot - change line width

Tags:

r

ggplot2

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.)

like image 234
Mark Graph Avatar asked Aug 30 '12 12:08

Mark Graph


People also ask

How do I change the line width in ggplot2?

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

How do you change the width of a line in R?

To set plot line width/thickness in R, call plot() function and along with the data to be plot, pass required thickness/line-width value for the “lwd” parameter.

How do you make a dashed line in ggplot2?

Note that, line types can be also specified using numbers : 0, 1, 2, 3, 4, 5, 6. 0 is for “blank”, 1 is for “solid”, 2 is for “dashed”, ….


1 Answers

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.

like image 165
csgillespie Avatar answered Sep 28 '22 04:09

csgillespie