为geom_smooth线创建动态标签 [英] Create dynamic labels for geom_smooth lines

查看:50
本文介绍了为geom_smooth线创建动态标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变化的df,并且我将不同的值分组为 c .使用ggplot2,我可以使用以下代码对它们进行绘制,以获得具有多个线性回归线(geom_smooth)的散点图

I have a changing df and I am grouping different values c. With ggplot2 I plot them with the following code to get a scatterplott with multiple linear regression lines (geom_smooth)

ggplot(aes(x = a, y = b, group = c)) + 
  geom_point(shape = 1, aes(color = c), alpha = alpha) +
  geom_smooth(method = "lm", aes(group = c, color = c), se = F)

现在,我想在图中的每条geom_smooth线上显示带有 group c 组的 value label .这必须是动态的,因为当df更改时我无法编写新代码.

Now I want to display on each geom_smooth line in the plot a label with the value of the group c. This has to be dynamic, because I can not write new code when my df changes.

示例:我的 df 看起来像这样

Example: my df looks like this

  a     b     c
----------------
 1.6    24   100
-1.4    43   50
 1      28   100
 4.3    11   50
-3.45   5.2  50

因此,在这种情况下,我将在图中获得3条geom_smooth线,并使用不同的颜色.

So in this case I would get 3 geom_smooth lines in the plot with different colors.

现在,我只想在带有 c = 100 组的geom_smooth旁边的"100" 地块上添加文本标签,并在上添加>"50" 到组 c = 50 的行,依此类推...随着在df中引入新组,绘制了新的geom_smooth线,需要对其进行标记

Now I simply want to add a text label to the plot with "100" next to the geom_smooth with the group c = 100 and a text label with "50"to the line for the group c = 50, and so on... as new groups get introduced in the df, new geom_smooth lines are plotted and need to be labeled.

剧情的全部代码:

 ggplot(aes(x = a, y = b, group = c), data = df, na.rm = TRUE) + 
  geom_point(aes(color = GG, size = factor(c)), alpha=0.3) +
  scale_x_continuous(limits = c(-200,2300))+
  scale_y_continuous(limits = c(-1.8,1.5))+
  geom_hline(yintercept=0, size=0.4, color="black") +
  scale_color_distiller(palette="YlGnBu", na.value="white") +
  geom_smooth(method = "lm", aes(group = factor(GG), color = GG), se = F) +
  geom_label_repel(data = labelInfo, aes(x= max, y = predAtMax, label = label, color = label))

推荐答案

如果选择要标记行的位置,则可以执行此操作.在下面,我将它们设置为每行的最右端标签,并使用 ggrepel 来避免标签重叠:

You can probably do it if you pick the location you want the lines labelled. Below, I set them to label at the far right end of each line, and used ggrepel to avoid overlapping labels:

library(ggplot2)
library(ggrepel)
library(dplyr)

set.seed(12345)

df <- 
  data.frame(
    a = rnorm(100,2,0.5)
    , b = rnorm(100, 20, 5)
    , c = factor(sample(c(50,100,150), 100, TRUE))
  )

labelInfo <-
  split(df, df$c) %>%
  lapply(function(x){
    data.frame(
      predAtMax = lm(b~a, data=x) %>%
        predict(newdata = data.frame(a = max(x$a)))
      , max = max(x$a)
    )}) %>%
  bind_rows

labelInfo$label = levels(df$c)

ggplot(
  df
  , aes(x = a, y = b, color = c)
  ) + 
  geom_point(shape = 1) +
  geom_smooth(method = "lm", se = F) +
  geom_label_repel(data = labelInfo
                   , aes(x= max
                         , y = predAtMax
                         , label = label
                         , color = label))

这篇关于为geom_smooth线创建动态标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆