ggplot2 透明/白色图例键与 stat_ellipse

问题描述 投票:0回答:1

我有一个图,我使用

stat_ellipse
显示某些点周围 95% 置信度的椭圆。我想摆脱图例中点后面的灰色背景,但它不起作用。我知道其他类似的帖子(例如这个),但这似乎是它自己的事情(鉴于我无法为
se=FALSE
设置
stat_ellipse
)。还有其他办法还是我运气不好?

请参阅下面的示例代码:

library(ggplot2)

ggplot(iris,aes(x=Sepal.Length,y=Petal.Length,fill=Species)) + 
  stat_ellipse(geom="polygon",level=0.95,alpha=0.1) +
  geom_point(color="black",shape=21,size=5) +
  theme(legend.key = element_blank()) # + 
  # this also doesn't work:
  # theme(legend.key = element_rect(fill="white"))

r ggplot2 colors background legend
1个回答
0
投票

如果仔细观察,背景不是灰色的:它是物种填充颜色的浅透明阴影。发生这种情况是因为您在

aes(fill = Species)
内部指定了
ggplot()
,因此填充将应用于后续的几何图形、点和椭圆。

要删除它,只需将

aes(fill = Species)
移动到
geom_point
几何体中即可。

ggplot(iris,
       aes(x = Sepal.Length, 
           y = Petal.Length)) + 
stat_ellipse(geom = "polygon",
             level = 0.95, alpha = 0.1) +
geom_point(color = "black",
           shape = 21,
           size = 5,
           aes(fill = Species))

结果:

© www.soinside.com 2019 - 2024. All rights reserved.