Line plots by year

library(tidyverse)
library(tsibble)
library(lubridate)
# Toy Data-------------
set.seed(123)
date <- seq(ymd("20190101"), ymd("20220430"), by = "1 day")
df <- tibble(date = date) %>% 
  mutate(week = yearweek(date)) %>% 
  distinct(week) %>% 
  mutate(
    profit = row_number()*5 + rnorm(174, 100, 100),
    year = isoyear(week)
  )

How can plot profit by year i.e. generate four line plots?
I tried the following, it produces only one line plot taking all the years into account.

df %>% 
  ggplot(aes(x = week, y = profit, color = year)) +
  geom_line()

You need a week variable that goes 1:52 which I've created as week2.

library(tidyverse)
library(tsibble)
#> 
#> Attaching package: 'tsibble'
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, union
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:tsibble':
#> 
#>     interval
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
# Toy Data-------------
set.seed(123)
date <- seq(ymd("20190101"), ymd("20220430"), by = "1 day")
df <- tibble(date = date) %>% 
  mutate(week = yearweek(date)) %>% 
  distinct(week) %>% 
  mutate(
    profit = row_number()*5 + rnorm(174, 100, 100),
    year = isoyear(week),
    week2 = isoweek(week)
  )

df %>% 
  mutate(year=as.factor(year)) %>%
  ggplot(aes(x = week2, y = profit, color = year, group=year)) +
  geom_line()

Created on 2022-05-10 by the reprex package (v2.0.1)

1 Like

@StatSteph Many thanks! One question: how will your code change if I want to display all the values of week2?

It won't necessarily be pretty but you can do it like this:

library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 4.1.3
#> Warning: package 'ggplot2' was built under R version 4.1.3
#> Warning: package 'tibble' was built under R version 4.1.3
#> Warning: package 'tidyr' was built under R version 4.1.3
#> Warning: package 'readr' was built under R version 4.1.3
#> Warning: package 'dplyr' was built under R version 4.1.3
library(tsibble)
#> Warning: package 'tsibble' was built under R version 4.1.3
#> 
#> Attaching package: 'tsibble'
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, union
library(lubridate)
#> Warning: package 'lubridate' was built under R version 4.1.3
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:tsibble':
#> 
#>     interval
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

# Toy Data-------------
set.seed(123)
date <- seq(ymd("20190101"), ymd("20220430"), by = "1 day")
df <- tibble(date = date) %>% 
  mutate(week = yearweek(date)) %>% 
  distinct(week) %>% 
  mutate(
    profit = row_number()*5 + rnorm(174, 100, 100),
    year = isoyear(week),
    week2 = isoweek(week)
  )

df %>% 
  mutate(year=as.factor(year)) %>%
  ggplot(aes(x = week2, y = profit, color = year, group=year)) +
  geom_line() +
  scale_x_continuous(breaks=1:53)

Created on 2022-05-10 by the reprex package (v2.0.1)

1 Like

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.