I want to add a series of arrows connecting each observation in geom_point as in the graph:
I understand that geom_segment is meant to be used, but I am having issues, and have not found something quite like this after quite a bit of searching.
This is sample code that should satisfy the pattern:
Note: The labels are not important ; just the arrows
df <- data.frame(year = c(1935:1968),
y_axis_values = c( 2755,2696, 2646, 2701, 2654, 2766, 2832, 2964, 3041, 3010, 3018, 3374, 3545, 3441, 3456, 3455, 3503, 3641, 3721, 3828, 3831, 3858, 3925, 3880, 3935, 3895, 3840, 3756, 3669, 3502, 3145, 2812, 2586,2441),
x_axis_values = c(238, 240, 241, 242, 244, 245, 246, 268, 333, 335, 331, 253, 243, 241, 242, 237, 242, 240, 233, 232, 236, 245, 256, 261, 265, 278, 291, 290, 290, 307, 313, 325, 339, 338)
I have tried the general formula with many different argument variations, but cannot seem to find it.
ggplot(df, aes(x = x_axis_values, y = y_axis_values) +
geom_point() +
geom_segment()
You need the xend and yend values for each segment. Since your data frame is in order, the xend and yend value for each segment is just the next row's x and y values. You can get these by using
dplyr::lead
on the x and y aesthetics.