Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When creating a multiple line plot in ggplot2, how do you make one line thicker than the others?

Tags:

r

ggplot2

I have a simple melted data frame with 5 variables that I am plotting in a multiple line graph in ggplot2. I posted my code below and I feel the answer to this question should be simple, yet I can't find the answer.

If I am plotting 5 lines together on the same chart, is there a way to make one of the lines (the mean in this case) bolder/larger than the others?

As you can see in the code at the bottom, I specified that the size of the lines as 2 which makes all 5 lines the size of 2. But I was hoping to have the Mean line (the line specified as black in the scale colour function) become larger than the other lines.

I attempted setting size to size = c(2,2,2,2,3) but ggplot2 did not like that.

   FiveLineGraph <- ggplot(data= df, aes(x= Date, y=Temperature, group= model, colour= model)) +
  geom_line(size= 2) +
  scale_colour_manual(values = c("red","blue", "green", "gold","black"))

Any ideas?

I appreciate your help in advance.

Thanks.

like image 977
user3720887 Avatar asked Feb 23 '16 19:02

user3720887


1 Answers

I've added data to make it a "reproducible example". This technique would work for the color of your lines also.

library("ggplot2")
set.seed(99)
df <- data.frame(x=c(1:5, 1:5, 1:5), y=rnorm(15, 10, 2), 
                 group=c(rep("A", 5), rep("B", 5), rep("C", 5)),
                 stringsAsFactors=FALSE)
ggplot(df, aes(x=x, y=y, group=group, colour=group)) + geom_line(size=2)

enter image description here

df$mysize <- rep(2, nrow(df))
df$mysize[df$group=="B"] <- 4
ggplot(df, aes(x=x, y=y, colour=group, size=mysize)) + geom_line() + 
  scale_size(range = c(2, 4), guide="none")

enter image description here

like image 139
cory Avatar answered Oct 22 '22 14:10

cory