【问题标题】:Change 'aaa' from geom_text legend to the the corresponding label text将 geom_text 图例中的 'aaa' 更改为相应的标签文本
【发布时间】:2021-02-01 09:04:21
【问题描述】:

类似于this questionthis,我的首选输出是将geom_text 的图例作为文本标签,而不是'aaa'。 (即mchild图例圆圈内的'aaa'应该是0,1,2,3,4。请看附图)

这是我的数据:

 data.frame(
            ID = c(1L, 3L, 5L, 11L, 13L, 18L, 20L, 24L, 33L, 34L, 36L),
       sum_hrs = c(12, 8, 68, 44, 16, 12, 36, 16, 4, 20, 4),
  avg_workload = c(263.1615,275.312,269.462444444444,
                   268.867666666667,276.686,257.3605,267.8695,268.3355,
                   239.409,260.230333333333,330.061),
      avg_achv = c(92.5,98,94.2222222222222,
                   94.8333333333333,92,98,94.25,93.5,98,95.3333333333333,100),
        mchild = c(1L, 0L, 1L, 2L, 3L, 0L, 4L, 0L, 2L, 0L, 1L),
       drinker = as.factor(c("No","Yes","Yes",
                             "Yes","Yes","No","Yes","Yes","No","No",
                             "Yes"))
)

我已经尝试过相关 SO 问题中的答案建议,但无法正常工作。很可能我的 grobs 索引不正确。所以我已经阅读了文档,但仍然无法完全理解。这是我到目前为止所做的:

p2 <- ggplot(data=sum_ua, aes(x=avg_achv, y=sum_hrs, size=mchild, color=drinker, alpha=sum_hrs)) +
    geom_point() +
    geom_text(data=sum_ua, aes(x=avg_achv, y=sum_hrs, label=mchild), color='#A9A9A9') +
    scale_size(range=c(4,20)) +
    scale_alpha_binned(range = c(0.01, 1), guide = 'none') +    
    new_scale_color()

g <- ggplotGrob(p2)
lbls <- unique(sort(sum_ua$mchild))
g$grobs[[15]][[1]][[1]]$grobs[[1]]$label <- lbls[1]
g$grobs[[15]][[1]][[1]]$grobs[[6]]$label <- lbls[2]
g$grobs[[15]][[1]][[1]]$grobs[[8]]$label <- lbls[3]

g$grobs[[grep("guide", g$layout$name)]]

grid.draw(g)

这是我的输出:

很抱歉这个基本问题,感谢您的帮助! :)

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    作为在情节的 gtable 中乱来的替代方法,您可以为标签美感制作自定义 scale_discrete_identity()。您只需要确保它有一个图例,并且所有信息都与尺寸图例相似(标题、中断、标签等)。

    library(ggplot2)
    library(ggnewscale)
    
    sum_ua <- data.frame(
      ID = c(1L, 3L, 5L, 11L, 13L, 18L, 20L, 24L, 33L, 34L, 36L),
      sum_hrs = c(12, 8, 68, 44, 16, 12, 36, 16, 4, 20, 4),
      avg_workload = c(263.1615,275.312,269.462444444444,
                       268.867666666667,276.686,257.3605,267.8695,268.3355,
                       239.409,260.230333333333,330.061),
      avg_achv = c(92.5,98,94.2222222222222,
                   94.8333333333333,92,98,94.25,93.5,98,95.3333333333333,100),
      mchild = c(1L, 0L, 1L, 2L, 3L, 0L, 4L, 0L, 2L, 0L, 1L),
      drinker = as.factor(c("No","Yes","Yes",
                            "Yes","Yes","No","Yes","Yes","No","No",
                            "Yes"))
    )
    
    ggplot(data=sum_ua, aes(x=avg_achv, y=sum_hrs, size=mchild, color=drinker, alpha=sum_hrs)) +
      geom_point() +
      geom_text(data=sum_ua, aes(x=avg_achv, y=sum_hrs, label=as.character(mchild)), color='#A9A9A9') +
      scale_size(range=c(4,20)) +
      scale_alpha_binned(range = c(0.01, 1), guide = 'none') +   
      scale_discrete_identity(guide = "legend", aesthetics = "label",
                              name = "mchild") +
      new_scale_color()
    

    reprex package (v0.3.0) 于 2021-02-01 创建

    【讨论】:

    • 非常感谢 teunbrand。这行得通! :) 如果我想删除/隐藏圆圈旁边的文字,我该怎么办?我尝试使用 scale_fill_identity 但无法使其工作..
    • 我认为您可以通过在大小和离散身份标度中设置 labels = NULL 或在这两个标度中设置 guide = guide_legend(label = FALSE) 来做到这一点。
    【解决方案2】:

    对于您的情况,这似乎有效

    g <- ggplotGrob(p2)
    g$grobs[[15]][[1]][[1]]$grobs[[5]]$label <- 0
    g$grobs[[15]][[1]][[1]]$grobs[[8]]$label <- 1
    g$grobs[[15]][[1]][[1]]$grobs[[11]]$label <- 2
    g$grobs[[15]][[1]][[1]]$grobs[[14]]$label <- 3
    g$grobs[[15]][[1]][[1]]$grobs[[17]]$label <- 4
    grid::grid.draw(g)
    

    或者为了避免硬编码和重复相同的代码,我们可以使用:

    labels <- 0:4
    inds <- which(colSums(sapply(g$grobs[[15]][[1]][[1]]$grobs,class) == 'text') > 0)
    g$grobs[[15]][[1]][[1]]$grobs[inds] <- Map(function(x, y) {x$label <- y;x}, 
                            g$grobs[[15]][[1]][[1]]$grobs[inds], labels)
    grid::grid.draw(g)
    

    【讨论】:

    • 谢谢你,Ronak,你的输出是我想要实现的:) 但是当我尝试使用你的解决方案时,我得到了这个错误。*tmp*[[11]] 中的错误:下标超出范围...你能帮帮我吗?
    • 奇怪的是,它对我来说对您共享的数据工作正常。您是否将其应用于同一数据集?哪一行给你错误?你跑g &lt;- ggplotGrob(p2)了吗?我没有在p2 中添加new_scale_color(),因为我不知道它来自哪个包,不确定这是否有任何区别。
    • 是的,使用相同的数据集。是的,我运行了 g
    猜你喜欢
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 2017-03-19
    • 2017-12-24
    相关资源
    最近更新 更多