【问题标题】:Create dynamic labels for geom_smooth lines为 geom_smooth 线创建动态标签
【发布时间】:2021-03-23 01:52:58
【问题描述】:

我有一个不断变化的 df,我正在对不同的值进行分组 c。 使用 ggplot2,我使用以下代码绘制它们以获得具有多个线性回归线 (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 线上显示一个 labelgroup cvalue。 这必须是动态的,因为当我的 df 发生变化时,我无法编写新代码。


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

  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 线。

现在我只想在带有c = 100 组的geom_smooth 旁边添加一个带有"100" 的文本标签,并将带有"50" 的文本标签添加到组c = 50 的行中,依此类推...随着 df 中引入新的组,新的 geom_smooth 线被绘制并需要标记。


剧情的完整代码:

 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))

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    如果您选择要标记线条的位置,您可能会这样做。下面,我将它们设置为每行最右端的标签,并使用ggrepel 来避免标签重叠:

    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))
    

    【讨论】:

    • 谢谢,聪明的解决方案!我很新,但它不适用于我的 DF,因为我有一些 NAin abc。我不能在你的代码之前filter(!is.na(a)) %&gt;%
    • 我也不能level(df$c)-它返回NULL`(应该是10个值左右)
    • levels(df$c) 仅在变量是一个因素时才有效(我将我的设置为一个因素)。
    • 当您拥有NAs 时,什么不起作用?如果需要,您可以在运行任何操作之前使用df &lt;- na.omit(df) 来删除缺少数据的行。或者,在对max的每个调用中添加na.rm = TRUE
    • @Max 如果您不使用某个因素,您可以尝试使用unique 而不是levels
    【解决方案2】:

    This method 可能对你有用。它使用ggplot_build 访问实际geom_smooth 行中最右边的点以添加标签。下面是使用Mark Peterson's example的改编。

    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))
      )
    
    p <-
      ggplot(df, aes(x = a, y = b, color = c)) + 
      geom_point(shape = 1) +
      geom_smooth(method = "lm", se = F)
    
    p.smoothedmaxes <- 
      ggplot_build(p)$data[[2]] %>% 
      group_by( group) %>% 
      filter( x == max(x))
    
    p +
      geom_text_repel( data = p.smoothedmaxes, 
                 mapping = aes(x = x, y = y, label = round(y,2)), 
                 col = p.smoothedmaxes$colour,
                 inherit.aes = FALSE)
    

    【讨论】:

      猜你喜欢
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 2018-08-05
      • 1970-01-01
      • 2022-12-22
      • 2012-10-14
      相关资源
      最近更新 更多