I need to plot multiple lines in a connected scatterplot, but am running into issues. I can plot one line, but not the other (or more).
Code
library(dplyr)
library(ggplot2)
library(ggrepel)
library(tidyr)
year<-c(2010,2011,2012,2013,2014,2015,2016,2017,2018,2019)
variableA1<-c(56,169,313,595,797,989,934,869,824,662)
variableB1<-c(0,0,5,12,23,44,73,71,78,103)
variableA2<-c(22,58,159,342,603,1021,1589,2071,2268,2044)
variableB2<-c(1,1,0,3,7,9,33,59,84,98)
data<-data.frame(year,variableA1,variableB1,variableA2,variableB2)
data %>%
ggplot(aes(x=variableA1, y=variableB1, label=year)) +
geom_point(color="#333333") +
geom_text_repel() +
geom_segment(color="#333333",
aes(
xend=c(tail(variableA1, n=-1), NA),
yend=c(tail(variableB1, n=-1), NA)
),
arrow=arrow(length=unit(0.3,"cm"))
) +
geom_point(color="#a8a8a8") +
geom_text_repel() +
geom_segment(color="#a8a8a8",
aes(
xend=c(tail(variableA2, n=-1), NA),
yend=c(tail(variableB2, n=-1), NA)
),
arrow=arrow(length=unit(0.3,"cm"))
)
ggplot Chart

