ggplot中文本和标签之间的区别? ggplot 中的错误:计算椭圆的点太少
Differece between text and label in ggplots? Error in ggplotly : Too few points to calculate an ellipse
我不太明白ggplot2
中text
和label
的区别。
例如:
当我尝试使用 label
作为工具提示将 ggplot2
对象转换为 plotly
交互式对象时,它成功了;当我使用 text
作为工具提示时出现 Too few points to calculate an ellipse
错误。
为什么?
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, label = row.names(iris))) +
geom_point()+
stat_ellipse(aes(fill = Species), geom = "polygon", level = 0.95, alpha = 0.1, show.legend = F)
ggplotly(p)
p2 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, text = row.names(iris))) +
geom_point()+
stat_ellipse(aes(fill = Species), geom = "polygon", level = 0.95, alpha = 0.1, show.legend = F)
ggplotly(p2)
问题很可能是分组问题。我没有看过 ggplot2
源代码,但我猜想(与 label
aes 相比) text
属性(不是默认的 ggplot2 美学)用于分组数据,即当您在 text
上使用 map the rownames
时,您最终得到的组只包含一行数据。因此,您会收到关于点数太少而无法绘制椭圆的警告。
为了防止您可以或必须在 stat_ellipse
中显式设置 group
aes:
library(plotly)
p2 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, text = row.names(iris))) +
geom_point()+
stat_ellipse(aes(fill = Species, group = Species), geom = "polygon", level = 0.95, alpha = 0.1, show.legend = F)
ggplotly(p2)
文本需要显示之前的标签(据我了解文档):因此,如果转到:https://ggplot2.tidyverse.org/reference/geom_text.html; you 'll see that:
有了这个,你会发现真正的问题不是椭圆的创建,而是你的 geom_text()
在函数中的解释方式。如果你这样写,椭圆和row.names都会出现在图形中:
p2 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, label = row.names(iris))) +
geom_point()+
stat_ellipse(aes(fill = Species), geom = "polygon", level = 0.95, alpha = 0.1, show.legend = F)
p2+geom_text(check_overlap = T)
结果如下:
干杯!
我不太明白ggplot2
中text
和label
的区别。
例如:
当我尝试使用 label
作为工具提示将 ggplot2
对象转换为 plotly
交互式对象时,它成功了;当我使用 text
作为工具提示时出现 Too few points to calculate an ellipse
错误。
为什么?
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, label = row.names(iris))) +
geom_point()+
stat_ellipse(aes(fill = Species), geom = "polygon", level = 0.95, alpha = 0.1, show.legend = F)
ggplotly(p)
p2 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, text = row.names(iris))) +
geom_point()+
stat_ellipse(aes(fill = Species), geom = "polygon", level = 0.95, alpha = 0.1, show.legend = F)
ggplotly(p2)
问题很可能是分组问题。我没有看过 ggplot2
源代码,但我猜想(与 label
aes 相比) text
属性(不是默认的 ggplot2 美学)用于分组数据,即当您在 text
上使用 map the rownames
时,您最终得到的组只包含一行数据。因此,您会收到关于点数太少而无法绘制椭圆的警告。
为了防止您可以或必须在 stat_ellipse
中显式设置 group
aes:
library(plotly)
p2 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, text = row.names(iris))) +
geom_point()+
stat_ellipse(aes(fill = Species, group = Species), geom = "polygon", level = 0.95, alpha = 0.1, show.legend = F)
ggplotly(p2)
文本需要显示之前的标签(据我了解文档):因此,如果转到:https://ggplot2.tidyverse.org/reference/geom_text.html; you 'll see that:
有了这个,你会发现真正的问题不是椭圆的创建,而是你的 geom_text()
在函数中的解释方式。如果你这样写,椭圆和row.names都会出现在图形中:
p2 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, label = row.names(iris))) +
geom_point()+
stat_ellipse(aes(fill = Species), geom = "polygon", level = 0.95, alpha = 0.1, show.legend = F)
p2+geom_text(check_overlap = T)
结果如下:
干杯!