将直接标签添加到geom_smooth而不是geom_line

问题描述 投票:0回答:2

我认识到这个问题与this非常相似,但是那里的解决方案不再有效(使用

method="last.qp"
),所以我再次问它。

基本问题是我想使用

directlabels
(或等效项)来标记每个组的平滑均值(来自
stat_smooth()
),而不是实际数据。下面的示例显示了我所得到的尽可能接近的结果,但标签无法识别分组,甚至无法识别平滑线。相反,我在最后一点得到了标签。我想要的是每个
stat_smooth()
末尾的颜色协调文本,而不是图右侧的图例。 这篇文章提供了一种标记最后一个数据点的方法(我看到的行为),但我正在寻找一种方法来标记自动生成的摘要(如果可能的话)。

这是一个例子:

library(ggplot2)
library(directlabels)

## Data
set.seed(10)
d <- data.frame(x=seq(1,100,1), y=rnorm(100, 3, 0.5))
d$z <- ifelse(d$y>3,1,0)

## Plot
p <- ggplot(d, aes(x=x, y=y, colour=as.factor(z))) +
  stat_smooth(inherit.aes=T, se=F, span=0.8, show.legend = T) +
  geom_line(colour="grey50") +
  scale_x_continuous(limits=c(0,110)) +
  geom_dl(label="text", method="maxvar.points", inherit.aes=T)
p

这使得这个情节:

r ggplot2 label ggrepel direct-labels
2个回答
7
投票

基于

这个答案
使用ggrepel

包的解决方案
library(tidyverse)
library(ggrepel)

set.seed(123456789)

d <- data.frame(x = seq(1, 100, 1), y = rnorm(100, 3, 0.5))
d$z <- ifelse(d$y > 3, 1, 0)

labelInfo <-
  split(d, d$z) %>%
  lapply(function(t) {
    data.frame(
      predAtMax = loess(y ~ x, span = 0.8, data = t) %>%
        predict(newdata = data.frame(x = max(t$x)))
      , max = max(t$x)
    )}) %>%
  bind_rows

labelInfo$label = levels(factor(d$z))
labelInfo

#>   predAtMax max label
#> 1  2.538433  99     0
#> 2  3.293859 100     1

ggplot(d, aes(x = x, y = y, color = factor(z))) + 
  geom_point(shape = 1) +
  geom_line(colour = "grey50") +
  stat_smooth(inherit.aes = TRUE, se = FALSE, span = 0.8, show.legend = TRUE) +
  geom_label_repel(data = labelInfo, 
                   aes(x = max, y = predAtMax, 
                       label = label, 
                       color = label), 
                   nudge_x = 5) +
  theme_classic()
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

reprex 包(v0.2.0)于 2018-06-11 创建。


6
投票

您需要告诉

geom_dl
您希望在绘图上显示什么。下面的代码应该可以简单地满足您的需求;

p <- ggplot(d, aes(x=x, y=y, colour=as.factor(z))) +
  stat_smooth(inherit.aes=T, se=F, span=0.8, method = "loess", show.legend = F) +
  geom_line(colour="grey50") +
  scale_x_continuous(limits=c(0,110)) +
  geom_dl(label=as.factor(d$z), method="maxvar.points", inherit.aes=T)

如果您想要不同的文本而不是

0
1
,您只需根据
d$z
制作它,然后将其代替
as.factor(d$z)

enter image description here

为了将标签放在

geom_smooth
的最后一个点而不是最后一个数据点旁边,我找不到
geom_dl
中的任何方法来执行此操作,因此,想出了一个解决方法:

p <- ggplot(d, aes(x=x, y=y, colour=as.factor(z))) +
  stat_smooth(inherit.aes=T, aes(label=as.factor(z)), se=F, 
              span=0.8, method = "loess", show.legend = F) +
  geom_line(colour="grey50") +
  scale_x_continuous(limits=c(0,110))


library(data.table)
smooth_dat <- setDT(ggplot_build(p)$data[[1]])
smooth_lab <- smooth_dat[smooth_dat[, .I[x == max(x)], by=group]$V1]


p + annotate("text", x = smooth_lab$x, y=smooth_lab$y, 
             label=smooth_lab$label,colour=smooth_lab$colour,
             hjust=-1)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.