Search code examples
rggplot2plotalpha

How to create legend with differing alphas for multiple geom_line plots in ggplot2 (R)


I have the following data on school enrollment for two years. I want to highlight data from school H in my plot and in the legend by giving it a different alpha.

library(tidyverse)

schools <- c("A","B","C","D","E",
             "F","G","H","I","J")
yr2010 <- c(601,809,604,601,485,485,798,662,408,451)
yr2019 <- c(971,1056,1144,933,732,833,975,617,598,822)

data <- data.frame(schools,yr2010,yr2019)

I did some data management to get the data ready for plotting.

data2 <- data %>%
  gather(key = "year", value = "students", 2:3)

data2a <- data2 %>%
  filter(schools != "H")

data2b <- data2 %>%
  filter(schools == "H")

Then I tried to graph the data using two separate geom_line plots, one for school H with default alpha and size=1.5, and one for the remaining schools with alpha=.3 and size=1.

ggplot(data2, aes(x=year,y=students,color=schools,group=schools)) +
  theme_classic() +
  geom_line(data = data2a, alpha=.3, size=1) +
  scale_color_manual(values=c("red","orange","green","skyblue","aquamarine","purple",
                              "pink","brown","black")) +
  geom_line(data = data2b, color="blue", size=1.5)

First graph attempt

However, the school I want to highlight is not included in the legend. So I tried to include the color of school H in scale_color_manual instead of in the geom_line call.

ggplot(data2, aes(x=year,y=students,color=schools,group=schools)) +
  theme_classic() +
  geom_line(data = data2a, alpha=.3, size=1) +
  scale_color_manual(values=c("red","orange","green","skyblue","aquamarine","purple",
                              "pink","blue","brown","black")) +
  geom_line(data = data2b, size=1.5)

Second graph attempt

However, now the alphas in the legend are all the same, which doesn't highlight school H as much as I'd like.

How can I call the plot so that the legend matches the alpha of the line itself for all schools?


Solution

  • You need to put alpha and size categories in aes like you put color. Then, you can use scale_alpha_manual and scale_size_manual with respect to your need. Also, by that there is no need for creating data2a and data2b.

    See below code:

    ggplot(data2, aes(x=year,y=students,color=schools,group=schools,
    alpha=schools, size = schools)) +
      theme_classic() +
      geom_line() +
      scale_color_manual(values=c("red","orange","green","skyblue","aquamarine","purple",
                                  "pink","blue","brown","black")) +
      scale_alpha_manual(values = c(0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3,NA, 0.3, 0.3)) + 
      #for the default alpha, you can write 1 or NA
      scale_size_manual(values= c(1,1,1,1,1,1,1,1.5,1,1))
    

    The code brings this plot. Please click.

    I hope it will be useful.