Struggling to present line types, colors, and legend as desired with ggplot2

Hi Rstudio community,

I'm attempting to use ggplot2 to create a chart similar to the following:

The intention of this chart is to present the most recently available vintage of values for each source with a solid line. In this case, a vintage_most_recent value of "1" indicates the most recent vintage. Older vintages should use different line types, but the line types should be the same if their vintage_most_recent values is the same (all 1's should use the same line type, all 2's the same line type, etc). The color assigned for each source should also stay the same across vintages.

The main issue is that in place of the 1's and 2's (vintage_most_recent), I would prefer to have the vintage dates. Is it possible to achieve this while meeting the requirements I mentioned above? Thanks for any insight.

Code:


library(tidyverse)
library(ggplot2)
library(plotly)


test_data <- read.csv("test_data.csv") %>% mutate(vintage_most_recent = as.character(vintage_most_recent))


test_plot <- ggplot(data = test_data, aes(x = period, y = value, color = source, linetype = vintage_most_recent)) + geom_line()

ggplotly(test_plot)

test_data:

source vintage period value vintage_most_recent
source_1 2/1/2023 2015 1 1
source_1 2/1/2023 2016 2 1
source_1 2/1/2023 2017 3 1
source_1 2/1/2023 2018 4 1
source_1 2/1/2023 2019 5 1
source_1 1/1/2023 2015 0 2
source_1 1/1/2023 2016 1 2
source_1 1/1/2023 2017 2 2
source_1 1/1/2023 2018 3 2
source_1 1/1/2023 2019 4 2
source_2 3/1/2023 2015 2 1
source_2 3/1/2023 2016 3 1
source_2 3/1/2023 2017 4 1
source_2 3/1/2023 2018 5 1
source_2 3/1/2023 2019 6 1
source_2 2/1/2023 2015 3 2
source_2 2/1/2023 2016 4 2
source_2 2/1/2023 2017 5 2
source_2 2/1/2023 2018 6 2
source_2 2/1/2023 2019 7 2

I would build it directly with plotly

library(tidyverse)
library(plotly)
library(scales)
test_data <- tibble::tribble(
     ~source,   ~vintage, ~period, ~value, ~vintage_most_recent,
  "source_1", "2/1/2023",   2015L,     1L,                   1L,
  "source_1", "2/1/2023",   2016L,     2L,                   1L,
  "source_1", "2/1/2023",   2017L,     3L,                   1L,
  "source_1", "2/1/2023",   2018L,     4L,                   1L,
  "source_1", "2/1/2023",   2019L,     5L,                   1L,
  "source_1", "1/1/2023",   2015L,     0L,                   2L,
  "source_1", "1/1/2023",   2016L,     1L,                   2L,
  "source_1", "1/1/2023",   2017L,     2L,                   2L,
  "source_1", "1/1/2023",   2018L,     3L,                   2L,
  "source_1", "1/1/2023",   2019L,     4L,                   2L,
  "source_2", "3/1/2023",   2015L,     2L,                   1L,
  "source_2", "3/1/2023",   2016L,     3L,                   1L,
  "source_2", "3/1/2023",   2017L,     4L,                   1L,
  "source_2", "3/1/2023",   2018L,     5L,                   1L,
  "source_2", "3/1/2023",   2019L,     6L,                   1L,
  "source_2", "2/1/2023",   2015L,     3L,                   2L,
  "source_2", "2/1/2023",   2016L,     4L,                   2L,
  "source_2", "2/1/2023",   2017L,     5L,                   2L,
  "source_2", "2/1/2023",   2018L,     6L,                   2L,
  "source_2", "2/1/2023",   2019L,     7L,                   2L
  ) 

plot_ly(data=test_data,
        type="scatter",
        mode="lines",
        x=~period,
        y=~value,
        color=~source,
        colors = hue_pal()(2),
        name = ~vintage,
        linetype = ~vintage_most_recent) |> layout(
          plot_bgcolor ='#e1e2e5',
          yaxis=list(gridcolor="white"),
          xaxis=list(gridcolor="white")
          
        )

Thank you for providing this example. It worked for my purposes.

-Skylar

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.