相关疑难解决方法(0)

在线的末端绘制标签

我有以下数据(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)

在此输入图像描述

而不是传说,我喜欢标签

  1. 颜色与系列相同
  2. 每个系列的最后一个数据点的右侧

我在以下链接的答案中注意到了baptiste的评论,但是当我尝试调整他的代码(geom_text(aes(label = State, colour = State, x = Inf, y = Capex), hjust = -1))时,文本没有出现.

ggplot2 - 在剧情之外注释

temp.dat <- structure(list(Year = c("2003", "2004", "2005", "2006", "2007", 
"2008", "2009", "2010", "2011", "2012", "2013", …
Run Code Online (Sandbox Code Playgroud)

r legend ggplot2

50
推荐指数
7
解决办法
5万
查看次数

如何在 geom_smooth 之后而不是在 geom_line 之后显示直接标签?

我正在使用直接标签来注释我的情节。正如你在这张图片中看到的,标签在 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)

r ggplot2 direct-labels

8
推荐指数
2
解决办法
2860
查看次数

标签 统计

ggplot2 ×2

r ×2

direct-labels ×1

legend ×1