【问题标题】:Individual points sharing the same legend box with ggrepel单个点与 ggrepel 共享相同的图例框
【发布时间】:2021-03-17 21:24:27
【问题描述】:

当我生成这样的图表时

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species,label=Species)) + 
geom_label_repel()

每个点都有自己的标签。有没有办法创建一个只有 3 个文本框的图形(在这种情况下,因为我们有 3 个 Species),并且与该物种对应的所有点都用线绑定?

谢谢!

【问题讨论】:

标签: r ggplot2 ggrepel


【解决方案1】:

我喜欢ggforce::geom_mark_* geoms:

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species,label=Species)) +
  geom_point() +
  ggforce::geom_mark_hull()

【讨论】:

    【解决方案2】:

    您可以使用stat_ellipseannotate 完成类似于Jon Spring 提供的操作。

    library(ggplot2)
    
    
    ggplot(iris) +
      geom_point(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
      stat_ellipse(aes(x = Sepal.Length, y = Sepal.Width, color = Species), level = 0.998) +
      annotate(
        geom = "text", x = 3.9, y = 2, label = "Versicolor", hjust = "left", color = 'dark green'
        ) +
      annotate(
        geom = "text", x = 4.2, y = 4, label = "Setosa", hjust = "left", color = 'red'
        ) +
        annotate(
        geom = "text", x = 7.1, y = 4.1, label = "Virginica", hjust = "left", color = 'blue'
        )
    

    reprex package (v0.3.0) 于 2021-03-17 创建

    【讨论】:

      【解决方案3】:

      感谢乔恩和埃里克,

      但是,这两种解决方案在显示我的数据时有点令人困惑,所以我更喜欢用线条绑定的点而不是单个标签。

      谢谢, 罗伯特

      【讨论】:

        猜你喜欢
        • 2020-06-30
        • 2012-03-31
        • 2020-05-22
        • 1970-01-01
        • 1970-01-01
        • 2021-01-02
        • 1970-01-01
        • 2010-12-14
        • 1970-01-01
        相关资源
        最近更新 更多