Search code examples
rggplot2aesthetics

remove the break in the line being caused by lty


I have a time series I would like to plot in ggplot. I want to show that there's a break in the data at a certain point. I believe a good way to do this is by varying the line type (ie lty). However, doing so makes an annoying break in the plot. Is there a way to connect these lines?

Here is some sample code of a time series at which there's a "break" where year == 0:

df = tibble(year = -5:5, value=runif(11))
df$lty = df$year <= 0

ggplot(data=df, aes(x=year, y=value, lty=lty)) + geom_line()

I'm having difficulty uploading the resultant image, but it makes a line graph with a jump between year == 0 and year == 1, which I don't want.

Thanks, smart people of the internet!!!


Solution

  • This quirk is caused by ggplot's implicit grouping of datapoints based on colour, linetype etc. Setting the group manually to something isn't going to solve this, because there is a 1-line-1-look rule.

    Here are two options.

    Option 1: simply copy the datapoint and modify in place:

    library(tidyverse)
    
    df = tibble(year = -5:5, value=runif(11))
    df$lty = df$year <= 0
    
    df <- rbind(df, df[df$year == 0,])
    df$lty[nrow(df)] <- FALSE
    
    ggplot(data=df, aes(x=year, y=value, lty=lty)) + geom_line()
    

    Option 2: Parameterise your data as segments instead

    df = tibble(year = -5:5, value=runif(11))
    df = cbind(head(df, -1),
               tail(df, -1)) %>%
      setNames(c("year0", "value0", "year1", "value1"))
    
    df$lty <- df$year0 <= 0 & df$year1 <= 0
    
    ggplot(df, aes(x = year0, y = value0, xend = year1, yend = value1, linetype = lty)) +
      geom_segment()
    

    Created on 2020-05-27 by the reprex package (v0.3.0)