Need help with overlay geom_line legend labels

Hello all,

Thank you for taking the time to help

ISSUE

  • Need lines in legend that read “Mason County” and “State” with color
  • Could also use help with reducing the right scale and have lines follow that scale reduction
  • (Fake data)

Example code

  • ``` r
    pacman::p_load(rio,here, skimr, janitor,epikit,lubridate,parsedate, 
                   zoo,tidylog, flextable, gtsummary,scales,ggExtra,
                   gghighlight,ggsurveillance,rstatix,readxl,reprex, datapasta,
                   forecast,BiocManager, ggplot2, tidyverse)
    
    
    
    data <- tibble::tribble(
                   ~disease, ~year,           ~geo, ~cases, ~rate,
      "HIV Diagnosis",  2019, "Mason County",      5,     7.1,
      "HIV Diagnosis",  2020, "Mason County",      7,     8.9,
      "HIV Diagnosis",  2021, "Mason County",      3,     10,
      "HIV Diagnosis",  2019, "State",             1,     6.8,
      "HIV Diagnosis",  2020, "State",             5,     7.1,
      "HIV Diagnosis",  2021, "State",             6,     6.2
      )
    
    
    HIVdx<- data %>% filter (geo == "State")
                             
    
    
    HIVdxM <- data %>% filter (geo == "Mason County")
    
    ggplot ()+
      geom_col(data=HIVdxM, aes(
        x = year,
        y = cases),
        fill = "#007777",
        width = .75)+
      
      geom_line(data=HIVdxM,aes (
        x = year,
        y = 1*rate),
        group = 1,,
        col ="#B22E0C", 
        size = 2
      )+
      
      geom_line(data=HIVdx,aes (
        x = year,
        y = 1*rate),
        group = 1,,
        col ="#002517", 
        size =2
      )+
      scale_y_continuous(sec.axis = sec_axis(~.*1, name="Case Rate per 100,000"))+
     
      labs(                                         # add plot labels, title, etc.
        x = "Year Diagnosed",
        y = "Cases",
        title = "Mason County HIV New Diagnosis 2019-2023") +
      theme_minimal()
    

Hey Daye! You’re very close! the main issue is that your line colors are being set outside aes(), so ggplot does not know they should appear in the legend.

To get legend entries for “Mason County” and “State”, map color = geo inside aes(), and combine the two line datasets into one geom_line() call.

You can also control the right axis by scaling the rate values before plotting them, then undoing that scaling in sec_axis().

pacman::p_load(dplyr,ggplot2)

data <- tibble::tribble(
  ~disease, ~year, ~geo, ~cases, ~rate,
  "HIV Diagnosis", 2019, "Mason County", 5, 7.1,
  "HIV Diagnosis", 2020, "Mason County", 7, 8.9,
  "HIV Diagnosis", 2021, "Mason County", 3, 10,
  "HIV Diagnosis", 2019, "State", 1, 6.8,
  "HIV Diagnosis", 2020, "State", 5, 7.1,
  "HIV Diagnosis", 2021, "State", 6, 6.2
)

# bars only for Mason County
HIVdxM <- data %>% 
  filter(geo == "Mason County")

# lines for both Mason County and State
HIVdx_lines <- data %>% 
  select(year, geo, rate)

# scaling factor for secondary axis
scale_factor <- 0.5

ggplot() +
  geom_col(
    data = HIVdxM,
    aes(x = year, y = cases),
    fill = "#007777",
    width = 0.75
  ) +
  geom_line(
    data = HIVdx_lines,
    aes(x = year, y = rate * scale_factor, color = geo, group = geo),
    linewidth = 1.5
  ) +
  geom_point(
    data = HIVdx_lines,
    aes(x = year, y = rate * scale_factor, color = geo),
    size = 3
  ) +
  scale_color_manual(
    values = c("Mason County" = "#B22E0C", "State" = "#002517"),
    name = NULL
  ) +
  scale_x_continuous(
    breaks = seq(
      min(c(HIVdxM$year, HIVdx_lines$year), na.rm = TRUE),
      max(c(HIVdxM$year, HIVdx_lines$year), na.rm = TRUE),
      by = 1
    )
  ) +
  scale_y_continuous(
    name = "Cases",
    sec.axis = sec_axis(~ . / scale_factor, name = "Case Rate per 100,000")
  ) +
  labs(
    x = "Year Diagnosed",
    title = "Mason County HIV New Diagnosis 2019–2021"
  ) +
  theme_minimal()

Let me know if this is the expected result!

Best,

Luis

Incredible, better than I could have imagined! This will help me with so many graphs in my report.

Thank you so very much for the help Luis!

1 Like

Your welcome! Feel free to move around the legend depending on the spacing you have available!

Best,

Luis