I have a dataset that looks something like this,
df<-structure(list(Year = c(1985L, 1986L, 1987L, 1988L, 1989L, 1990L,
1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L,2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 1985L, 1986L, 1987L, 1988L, 1989L, 1990L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 1985L, 1986L, 1987L, 1988L, 1989L, 1990L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 1985L, 1986L, 1987L, 1988L, 1989L, 1990L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L),
varclass = structure(c(5L,5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L),
.Label = c("r_mean_dp_C_EU_PTA", "r_mean_dp_C_eu_esr_sum", "r_mean_TS_dp_C_EU_PTA", "r_mean_TS_dp_C_eu_esr_sum", "r_mean_dp_C_US_PTA", "r_mean_dp_C_us_esr_sum", "r_mean_TS_dp_C_US_PTA", "r_mean_TS_dp_C_us_esr_sum"), class = "factor"),
score = c(0, 0.0243131684738152, 0.0295348762350131, 1.24572619158458, 1.20624633452509, 1.57418568231032, 1.45479246796848, 2.38700784566208, 2.62865525326503, 2.26401361870534, 2.67319203680329, 2.64440548764366, 3.10459526464658, 3.05231530072328, 3.32660416229216, 4.14909239351474, 3.76404440984403,
3.79766644256544, 4.55279786294561, 5.57506946922008, 6.83412605593388, 8.07241989452914, 9.10370786838265, 9.51564633960853, 8.64357423479438, 9.10723202296861, 10, 9.06442082870898, 0, 0.00309681975626394, 0.00376191973176611, 0.31456863316694, 0.307467479949235, 0.393381408917437, 0.369438522116308, 2.72595849026258, 3.02912715393088, 2.68234740512793, 3.04191351961057, 3.00285602012791, 3.53353155000059, 3.56064849955087,
3.8582842780802, 4.41758875010321, 3.86352795800086, 3.87742698588076, 4.75555995695473, 5.25498063194254, 6.83035779491644, 7.94400595857329, 8.90724897806749, 9.63957864779812, 8.45971275397907, 8.90268516213906, 10, 8.67387866657026, 0, 0.0971144336795407, 0.095032354957329, 0.324563912754777, 0.309037243530711, 0.445594208900048, 0.408585357361577, 0.984622633943152, 0.885285042777509, 0.854437578777773, 0.899346941764503, 0.713835758098677, 0.923205921767153, 0.885795460138979, 0.907706909901698, 1.26691097637723, 1.38531675474906, 1.46801357426345, 2.40528143410555, 6.11127334356639, 6.87984183345601, 8.58685934192075, 9.81477059919623, 10, 8.91400803949761, 9.13396140936897, 8.91190146236915, 9.31841494169555,
0, 0.0131639497352947, 0.012881722072468, 0.0747427246588618, 0.074583753038409, 0.101240906800678, 0.0942176097616221, 1.01715224261827, 0.955422044148621, 0.930088131161646, 0.983316358580305, 0.814500687241286, 1.04433781820825, 1.00311623335088, 1.03043794235494, 1.25737311039187, 1.24242809142003, 1.30488643766672, 2.07282625012893, 5.9283639095883, 6.84735189008267, 8.44992964327563, 9.67266470058815, 10, 8.83518011553472,
9.08678510978114, 8.89558771137843, 9.22647697054944)), row.names = c(NA,-112L),
class = "data.frame")
I and I am creating the following plot:
plot<- df %>%
ggplot(aes(x= Year, y= score, color = varclass)) +
geom_line() +
geom_vline(xintercept = c(1992, 2004), linetype="dotted", size = 0.3)
The issue is that I am not able to label the vertical lines as I would like. In particular, I would like to label the 1992 line as "NAFTA". While 2004 line as "PAFTA". These are important events that affect my series. Ideally, the labels should appear on the right below the "varclass" labels.
I have tried doing the following
events<-structure(list(year = c(1992L, 2004L),
event = structure(1:2, .Names = c("a","b"),
.Label = c("NAFTA", "PAFTA"),
class = "factor")),
row.names = c("a", "b"),
class = "data.frame")
plot<- df %>%
ggplot(aes(x= Year, y= score, color = varclass)) +
geom_line() +
geom_vline(aes(xintercept=year, color=events),data=tst)
However, the legend becomes confusing with crosses.
Any idea on how to have a clean graph, with black vertical event lines and a clear labelling?
Maybe this will help, you can add geom_text to your first plot
plot +
geom_text(aes(x=2004, label="\nPAFTA", y=9), colour="red", angle=90) +
geom_text(aes(x=1992, label="\nNAFTA", y=9), colour="red", angle=90)

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With