首页 > 解决方案 > 使 stat_ellipse {ggplot2} 轮廓 geom_point 填充颜色

问题描述

我有一个包含 15 个分组的散点图。我正在使用geom_point()withshape = 21以便我可以有填充和颜色(轮廓颜色)。我使用黑色作为轮廓颜色,以便在我的图例中的相似颜色之间提供更好的对比。当我添加stat_ellipse()虽然时,它会使椭圆轮廓变黑。

我想要这个,点周围有黑色轮廓:

groupings <- paste0("Group", 1:15)
iris$group <- rep(groupings, 10)

iris_plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point(aes(Sepal.Length, Sepal.Width, colour = factor(iris$group))) + stat_ellipse(data = iris, aes(color = factor(iris$group)))

iris_plot

stat_ellipse 与 geom_point 颜色相同

但是当我在点周围添加黑色轮廓时,它会使我的椭圆变黑,使它们无法解释。

library(RColorBrewer)

groupings <- paste0("Group", 1:15)
iris$group <- rep(groupings, 10)

fill_colors <- scales::hue_pal()(15)
outline_colors <- rep("black", 15)

iris_plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point(aes(Sepal.Length, Sepal.Width, colour = factor(iris$group), fill = factor(iris$group)), shape = 21) + stat_ellipse(data = iris, aes(color = factor(iris$group))) + scale_colour_manual(name = "Grouping", labels = sort(unique(factor(iris$group))), values = outline_colors) + scale_fill_manual(name = "Grouping", labels = sort(unique(factor(iris$group))), values = fill_colors)

iris_plot

stat_ellipse 采用 geom_point 轮廓颜色 我不想要填充颜色,因为椭圆之间有太多重叠,以至于看不到任何东西。

感谢您的时间。

标签: rggplot2

解决方案


我认为你需要通过color外部aesfor geom_point,否则当你申请时scale_color_manual,它会同时申请geom_pointstat_ellipse

iris_plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_point(aes(Sepal.Length, Sepal.Width,fill = group), color = "black", shape = 21) + 
  stat_ellipse(data = iris, aes(color = group)) + 
  scale_fill_manual(name = "Grouping", labels = sort(unique(factor(iris$group))), values = fill_colors)+
  scale_color_manual(name = "Grouping", values = fill_colors,  labels = sort(unique(factor(iris$group))))

iris_plot

在此处输入图像描述


推荐阅读