Skip to contents

Add broken pointline to ggplot

Usage

add_broken_pointline(df, aes_x = "x", aes_y = "y", size = 1, ...)

Arguments

df

Data with columns aes_x and aes_y.

aes_x

Name of the x aesthetic.

aes_y

Name of the y aesthetic.

size

Size of the line and the points.

...

Other aesthetics to pass to ggplot2::aes_(). Valid aesthetics are the aesthetics for ggplot2::geom_path() and ggplot2::geom_point() (except size that is fixed). NB: if we want the colour to change with Group we would need to add colour = as.name(Group).

Value

List to add to ggplot.

Examples

library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

df1 <- tibble(x = 1:100, y = cumsum(rnorm(100))) %>%
  slice_sample(prop = .8) %>%
  arrange(x)

ggplot() +
  add_broken_pointline(df1) +
  theme_bw(base_size = 15)
#> Warning: Removed 20 rows containing missing values (`geom_point()`).


df2 <- mutate(df1, Group = case_when(x < 60 ~ "A", TRUE ~ "B"))

ggplot() +
  add_broken_pointline(df2, colour = as.name("Group")) +
  scale_colour_discrete(na.translate = FALSE) +
  theme_bw(base_size = 15)
#> Warning: Removed 20 rows containing missing values (`geom_path()`).
#> Warning: Removed 20 rows containing missing values (`geom_point()`).