我有以下数据(temp.dat请参阅完整数据的结束注释)
Year State Capex
1 2003 VIC 5.356415
2 2004 VIC 5.765232
3 2005 VIC 5.247276
4 2006 VIC 5.579882
5 2007 VIC 5.142464
...
Run Code Online (Sandbox Code Playgroud)
我可以生成以下图表:
ggplot(temp.dat) +
geom_line(aes(x = Year, y = Capex, group = State, colour = State))
Run Code Online (Sandbox Code Playgroud)

而不是传说,我喜欢标签
我在以下链接的答案中注意到了baptiste的评论,但是当我尝试调整他的代码(geom_text(aes(label = State, colour = State, x = Inf, y = Capex), hjust = -1))时,文本没有出现.
temp.dat <- structure(list(Year = c("2003", "2004", "2005", "2006", "2007",
"2008", "2009", "2010", "2011", "2012", "2013", …Run Code Online (Sandbox Code Playgroud) 我正在使用直接标签来注释我的情节。正如你在这张图片中看到的,标签在 geom_line 之后,但我想要在 geom_smooth 之后。这是否受直接标签支持?或任何其他想法如何实现这一目标?提前致谢!
这是我的代码:
library(ggplot2)
library(directlabels)
set.seed(124234345)
# Generate data
df.2 <- data.frame("n_gram" = c("word1"),
"year" = rep(100:199),
"match_count" = runif(100 ,min = 1000 , max = 2000))
df.2 <- rbind(df.2, data.frame("n_gram" = c("word2"),
"year" = rep(100:199),
"match_count" = runif(100 ,min = 1000 , max = 2000)) )
# plot
ggplot(df.2, aes(year, match_count, group=n_gram, color=n_gram)) +
geom_line(alpha = I(7/10), color="grey", show_guide=F) +
stat_smooth(size=2, span=0.3, se=F, show_guide=F) +
geom_dl(aes(label=n_gram), method = "last.bumpup", show_guide=F) +
xlim(c(100,220))
Run Code Online (Sandbox Code Playgroud)