与ggrepel共享同一个图例框的单个点

时间:2021-03-17 21:24:27

标签: r ggplot2 ggrepel

当我生成这样的图表时

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

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

谢谢!

3 个答案:

答案 0 :(得分:1)

我喜欢 #include <iostream> #include <complex> #include <math.h> using namespace std; int main() { int a, b, c, d; float x1, x2; cout << "Please input values for a, b and c to represent the variables in equation : ax^2 + bx + c" << endl; // Dont know how to raise 2 instead of using ^2 cin >> a >> b >> c; d = (b * b) - (4 * a * c); cout << d << endl; cout << (-b + sqrt(d))/(2 * a); } 几何体:

ggforce::geom_mark_*

enter image description here

答案 1 :(得分:0)

您可以使用 Jon Springstat_ellipse 完成类似于 annotate 所提供的内容。

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 年 3 月 17 日创建

答案 2 :(得分:0)

感谢乔恩和埃里克,

然而,这两种解决方案在显示我的数据时有点混乱,所以我更喜欢用线绑定的点而不是单个标签。

谢谢, 罗伯特

相关问题