Home > Software engineering >  How to manually change line size and alpha values for ggplot2 lines (separated by factor)?
How to manually change line size and alpha values for ggplot2 lines (separated by factor)?

Time:04-13

I want to create a graph where I can change the line size for each line c(1,2,3) and the alpha values for each line c(0.5,0.6,0.7). I tried to use scale_size_manual but it didn't make any difference. Any ideas on how to proceed?

var <- c("T","T","T","M","M","M","A","A","A")
val <- rnorm(12,4,5)
x <- c(1:12)

df <- data.frame(var,val,x)

ggplot(aes(x= x , y = val, color = var, group = var), data = df)   
scale_color_manual(values = c("grey","blue","black"))    geom_smooth(aes(x = x, y = val), formula = "y ~ x", method = "loess",se = FALSE, size = 1)   scale_x_continuous(breaks=seq(1, 12, 1), limits=c(1, 12))   scale_size_manual(values = c(1,2,3))

CodePudding user response:

To set the size and alpha values for your lines you have to map on aesthetics. Otherwise scale_size_manual will have no effect:

library(ggplot2)

ggplot(aes(x = x, y = val, color = var, group = var), data = df)  
  scale_color_manual(values = c("grey", "blue", "black"))  
  geom_smooth(aes(x = x, y = val, size = var, alpha = var), formula = "y ~ x", method = "loess", se = FALSE)  
  scale_x_continuous(breaks = seq(1, 12, 1), limits = c(1, 12))  
  scale_size_manual(values = c(1, 2, 3))  
  scale_alpha_manual(values = c(.5, .6, .7))

  • Related