You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

geom_mark_ellipse无法填充椭圆的问题求助

geom_mark_ellipse无法填充椭圆的问题求助

嘿,我来帮你排查这个问题!你看到的白色椭圆其实是geom_mark_ellipse的默认特性导致的——这个函数本质是用来创建带白色背景的标记型椭圆,它默认会在填充色上方叠加一层白色背景,用来突出标记效果,所以你看到的“白色椭圆”其实是这个背景层,而非填充失效了。

给你两种解决方案:

  • 改用普通椭圆图层(推荐)
    如果你只是想给每个聚类绘制填充椭圆,不需要标记功能,直接使用ggforce里的geom_ellipse就好,它是单纯的椭圆图形,不会添加额外的白色背景。修改后的代码如下:
library(ggplot2)
library(ggforce)

x1 <- runif (50, min = 10, max = 40)
y1 <- runif (50, min = 10, max = 40)
x2 <- runif (50, min = 41, max = 71)
y2 <- runif (50, min = 10, max = 40)
x3 <- runif (50, min = 10, max = 40)
y3 <- runif (50, min = -21, max = 9)
x4 <- runif (50, min = 41, max = 71)
y4 <- runif (50, min = -21, max = 9)

df <- data.frame(x = c(x1, x2, x3, x4), y = c(y1, y2, y3, y4),
                 cluster = c(rep('1', 50), rep('2', 50), rep('3', 50), rep('4', 50)))

ggplot(df, aes(x, y)) +
  geom_point(aes(colour = cluster)) +
  geom_ellipse(alpha = 0.5, aes(group = cluster, colour = cluster, fill = cluster))
  • 保留geom_mark_ellipse,移除白色背景
    如果你确实需要geom_mark_ellipse的标记功能(比如后续要添加标签),可以通过设置background.colour = NA来取消默认的白色背景,还可以调整expand参数控制椭圆的扩展空间:
ggplot(df, aes(x, y)) +
  geom_point(aes(colour = cluster)) +
  geom_mark_ellipse(alpha = 0.5, 
                    aes(group = cluster, colour = cluster, fill = cluster),
                    background.colour = NA,
                    expand = unit(0, "mm")) # 可选参数,移除椭圆外的额外空间

补充说明:

geom_mark_ellipse的设计初衷是制作带有标注的高亮标记,默认的白色背景是为了让标记在复杂图形中更醒目。而geom_ellipse是纯粹的统计椭圆图层,更贴合你当前仅需绘制聚类椭圆的需求。

备注:内容来源于stack exchange,提问作者Sasu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 08:49:38