Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop ggplot from reducing mulitple lines down to one line

Tags:

r

ggplot2

I am trying to make a figure where data are grouped into 10-year increments (e.g., 2000-2009, 2010-2019, 2020+) and have a common color among groups. I can set the color for individual years with no problem however, when I try to group years using a common color, the individual lines representing specific years get reduced down to one line. How can I stop this from happening?

suppressWarnings(library(tidyverse))

set.seed(333)

start_date <- as.Date('1990-01-01')
end_date <- as.Date('2023-12-31')
total_days <- as.numeric((end_date-start_date)+1)

# Create data frame
dat <- data.frame(
  Date = seq(start_date, end_date,'1 day'),
  Value = rnorm(total_days, 20, 4)
)

# add Year and day of year (DoY) to data frame
dat <- dat %>%
  mutate(Year = year(Date),
         DoY = yday(Date))

dat %>%
  ggplot(aes(x = DoY, y = Value, col = factor(Year))) +
  geom_line(data = subset(dat, Year == 2020), aes(colour = "2020")) +
  geom_line(data = subset(dat, Year == 2021), aes(colour = "2021")) +
  geom_line(data = subset(dat, Year == 2022), aes(colour = "2022")) +
  geom_line(data = subset(dat, Year == 2023), aes(colour = "2023")) +
  labs(x = 'Day of Year',
       y = 'Value',
       colour = 'Year Range') +
  scale_x_continuous(breaks = seq(1, 10, 1), limits = c(1,10)) +
  scale_color_manual(values = c("2020" = "#225ea8",
                                "2021" = "#41b6c4",
                                "2022" = "#a1dab4",
                                "2023" = "red")) +
  theme_bw()

# Why do all years >= 2020 get reduced down to one line?
# Each line (as in the figure above) should be red if the Year is >= 2020

dat %>%
  ggplot(aes(x = DoY, y = Value, col = factor(Year))) +
  # geom_line() +
  # geom_line(data = subset(dat, Year >= 1990 & Year <= 1999), aes(colour = "1990-1999")) +
  # geom_line(data = subset(dat, Year >= 2000 & Year <= 2009), aes(colour = "2000-2009")) +
  # geom_line(data = subset(dat, Year >= 2010 & Year <= 2019), aes(colour = "2010-2019")) +
  geom_line(data = subset(dat, Year >= 2020), aes(colour = "2020+")) +
  labs(x = 'Day of Year',
       y = 'Value',
       colour = 'Year Range') +
  scale_x_continuous(breaks = seq(1, 10, 1), limits = c(1,10)) +
  scale_color_manual(values = c("1990-1999" = "#225ea8",
                                "2000-2009" = "#41b6c4",
                                "2010-2019" = "#a1dab4",
                                "2020+" = "red")) +
  theme_bw()

Created on 2024-02-21 with reprex v2.0.2

The ideal figure would have all the lines, as seen in the uppermost figure, be red without having to specify each year.

like image 870
tassones Avatar asked Oct 22 '25 10:10

tassones


1 Answers

If I understand the request then just add Year as a grouping variable to the aes parameter to geom_line

 geom_line(data = subset(dat, Year >= 2020), aes(colour = "2020+", group=Year))

enter image description here

like image 170
IRTFM Avatar answered Oct 23 '25 23:10

IRTFM