【问题标题】:Combining aes_string or aes_q with multiple column labels将 aes_string 或 aes_q 与多个列标签组合
【发布时间】:2016-05-02 11:02:09
【问题描述】:

我正在尝试将使用多个列的标签与用于为 ggplot2 中的标签着色的灵活变量名称组合。

我可以使用 aes 中的硬编码变量名称“ret_cumarpu_cluster”来做到这一点,如下所示:

ggplot(mergedata, aes(d2ret, d14cumarpu, col = ret_cumarpu_cluster)) +
    geom_point() +
    geom_text(aes(label=paste(device_bucket, d90installs), col = ret_cumarpu_cluster), hjust = -.15, show.legend = FALSE)

生成此图:

但是,我想用字符串对象 cluster_type 替换硬编码字符串“ret_cumarpu_cluster”以生成多个图形。我尝试了 aes_q 和 aes_string 的各种迭代,但似乎没有一个能很好地将两个列名粘贴在一起以生成标签。理想情况下,我的代码如下所示:

ggplot(mergedata, aes_string('d2ret', 'd14cumarpu', col = cluster_type)) +
    geom_point() +
    geom_text(aes_q(label=(paste(device_bucket, d90installs)), col = as.name(cluster_type)), hjust = -.15)

正如我所说,我不能完全让粘贴与 aes_q 或 aes_string 一起使用以允许我拥有多列标签。

作为一种解决方法,我显然可以在数据框中创建一个新列,将这两个字符串粘贴在一起并使用它,但我想知道这是否可能。谢谢!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我可以在粘贴前添加一个 ~ 来让 aes 将名称识别为列。

    ggplot(mergedata, aes_string('d2ret', 'd14cumarpu', col = cluster_type)) +
      geom_point() +
      geom_text(aes_q(label=~(paste(device_bucket, d90installs)), col = as.name(cluster_type)), hjust = -.15, show.legend = FALSE)
    

    【讨论】:

    • 大道具给大家推荐~贴在前面。这解决了我的问题。我从来没有见过 ~ 的这个用例,并且在以各种方式反复谷歌搜索后,仍然找不到太多解释这个用例的细节。
    猜你喜欢
    • 2015-08-30
    • 2019-10-30
    • 2023-01-18
    • 2015-06-05
    • 1970-01-01
    • 2016-06-20
    • 2019-02-10
    • 1970-01-01
    • 2020-12-31
    相关资源
    最近更新 更多