我有一个看起来像这样的数据集,
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")
我和我正在创建以下情节:
plot<- df %>%
ggplot(aes(x= Year, y= score, color = varclass)) +
geom_line() +
geom_vline(xintercept = c(1992, 2004), linetype="dotted", size = 0.3)
问题是我无法按照自己的意愿标记垂直线。特别是,我想将1992年的标签命名为“ NAFTA”。而2004年被列为“ PAFTA”。这些是影响我的系列的重要事件。理想情况下,标签应显示在“ varclass”标签下方的右侧。
我尝试执行以下操作
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)
但是,图例变得与十字架混淆了。
关于如何创建带有黑色垂直事件线和清晰标签的干净图形的任何想法吗?
答案 0 :(得分:3)
也许这会有所帮助,您可以将geom_text
添加到您的第一个情节中
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)
答案 1 :(得分:1)
这不能直接回答您的问题,但我认为您可以选择在图形上将标签作为注释:
SELECT d.mid
, d.totals
FROM A d
WHERE d.totals IN ( SELECT MAX(s.totals) FROM A s )
ORDER
BY d.mid
, d.totals
答案 2 :(得分:1)
您可以将geom_text
与新的data
参数设置一起使用。但是,您还必须设置inherit.aes = FALSE
,以便几何图形不使用前一个data = df
。
此外,我将图的名称从plot
(基本R函数的名称)更改为g
。
g <- ggplot(df, aes(x= Year, y= score, color = varclass)) +
geom_line() +
geom_vline(xintercept = c(1992, 2004), linetype="dotted", size = 0.3)
g + geom_text(data = events,
mapping = aes(x = year, y = c(10.5, 10.5), label = event),
inherit.aes = FALSE,
hjust = 1)