change alpha, different lines

Does anyone know how to change the alpha based on the specific line if a chart has multiple lines? Here's some code I put together but scale_alpha_manual doesn't do anything. Any assistance would be greatly appreciated.

ggplot(data = socialdisorder_concentration_preFEWC, aes(x = mthyr, y = rank.x, group = blockstreet, color = rank_colour2))+
geom_line () +
geom_point() +
scale_x_discrete(name = "Month-Year") +
scale_y_reverse(name = "Rank", breaks = seq (1,10, by = 1)) +
scale_color_manual(name = "Street Block",
labels = c(socialdisorder_concentration_preFEWC$blockstreet),
values = c(red = "red", orange = "orange", green = "dark green", grey = "lightgrey"),
limits = c("red", "orange", "green")) +
scale_alpha_manual (values = c(1, 1, 1/25))

It also didn't matter if I had 4 values for changing the colour and then 4 alphas (e.g., 1, 1, 1, 1/25)

Here is a simple example of setting the alpha of each line. I made them all red to ease the comparison of the alpha.

library(ggplot2)
DF <- data.frame(X = rep(1:4, 4),
                 Y = rnorm(16),
                 GRP = rep(c("A","B","C","D"), each = 4))
ggplot(DF, aes(x = X, y = Y, group = GRP, color = GRP, alpha = GRP)) + 
  geom_line(linewidth = 2) +
  scale_color_manual(values = c(A = "red", B = "red", 
                                C = "red", D = "red")) +
  scale_alpha_manual(values = c(A = 0.1, B = 0.3, C = 0.55, D = 0.75))

Created on 2024-03-19 with reprex v2.0.2

1 Like

Thank you so much, it worked!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.