【问题标题】:How can I add specific, labeled points to a ggtern plot?如何将特定的标记点添加到 ggtern 图?
【发布时间】:2018-10-30 16:43:27
【问题描述】:

使用 ggplot2 我通常希望能够像这样添加数据点,

ggtern(df, aes(X, Y, Z, value = VALUE), aes(x, y, z)) +
        geom_point(aes(fill = VALUE), size = 2, stroke = 0, shape = 21) + 
        scale_fill_gradient(low = "red",high = "yellow", guide = F) + 
        scale_color_gradient(low = "red",high = "yellow", guide = F) +
    geom_point(aes(x = 10, y = 10, z = 50), shape = 21)

但是,当使用 ggtern 包生成三元图时,它们被插入到错误的位置(参见示例图片),并出现以下警告:

Warning: Ignoring unknown aesthetics: z

这意味着 ggplot2 可能正在尝试渲染该点而不是 ggtern。如何在 ggtern 图中添加特定的标记点?

【问题讨论】:

  • 三件事,[1] 你在 ggtern 构造函数中有两个 aes 参数 [2] 你想要的点有一个加到 70 的合成,相当于x=0.143, y=0.143 z=0.714,正是这个点所在的位置放置,[3] 警告 Warning: Ignoring unknown aesthetics: z 与 ggplot2 试图处理 x、y、z 美学有关,请忽略它。
  • 另外,我是否也建议您查看最新开发版本中的 hexbin 或 tribin 几何图形,这将比像您那样绘制许多彩色点更清洁、更高效。
  • @NicholasHamilton 感谢您的提示! tribin 看起来可能适用于数据集,或者在同一数据集中的第二列中失败。

标签: r ggplot2 ggtern


【解决方案1】:

这似乎有两个要点。首先是创建一个annotation,尽管这可能并不理想,因为它不如一个点那么精确。一个例子是,

ggtern() + 
annotate(geom  = 'text',
              x     = c(0.5,1/3,0.0),
              y     = c(0.5,1/3,0.0),
              z     = c(0.0,1/3,1.0),
              angle = c(0,30,60),
              vjust = c(1.5,0.5,-0.5),
              label = paste("Point",c("A","B","C")),
              color = c("green","red",'blue')) +
  theme_dark() + 
  theme_nomask()

第二种选择是创建一个新的data frame and add that to the plot. 虽然这样做的优点是可以更好地控制点,但缺点是标记需要额外的工作。

【讨论】:

    【解决方案2】:

    一种可能性是有一个列来标识您希望标记的点,在本例中为“实验室”列,并说我想标记点 1 和 3:

    df <- data.frame(x=c(10,20,30), y=c(15,25,35), z=c(75,55,35), VALUE=c(1,2,3), lab=c("One", "", "Three"))
    

    那么geom_text或者geom_label就可以用来标注那些特定的点,例如:

    ggtern(df, aes(x, y, z, value = VALUE)) +
      geom_point(aes(fill = VALUE), size = 2, stroke = 0, shape = 21) + 
      scale_fill_gradient(low = "red",high = "yellow", guide = F) + 
      scale_color_gradient(low = "red",high = "yellow", guide = F) +
      geom_text(aes(label = lab), vjust=1)
    

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 2016-09-12
      • 2012-10-01
      • 1970-01-01
      相关资源
      最近更新 更多