问题描述
我有一个有15个分组的散点图.我正在将 geom_point()
与 shape = 21
一起使用,以便可以填充和着色(轮廓颜色).我将黑色用作轮廓色,以便在图例中的相似颜色之间提供更好的对比度.但是,当我添加 stat_ellipse()
时,它将使椭圆轮廓变黑.
I have a scatterplot with 15 groupings. I am using geom_point()
with shape = 21
so that I can have fill and color (outline color). I am using black for the outline color to give better contrast between the similar colors in my legend. When I add a stat_ellipse()
though, it makes the ellipse outline black.
我要这个,要点周围有黑色轮廓:
I want this, with black outlines around the points:
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
但是,当我在这些点周围添加黑色轮廓时,它会使椭圆变成黑色,使其无法解释.
But when I add the black outlines around the points, it turns my ellipses black, making them impossible to interpret.
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
我不希望使用填充色,因为椭圆之间有太多重叠,以至于看不到任何东西.
I do not want a fill color because there is so much overlap between ellipses that it becomes impossible to see anything.
谢谢您的时间.
推荐答案
我认为您需要为 geom_point
在 aes
外传递 color
,否则,当您应用 scale_color_manual
时,它将同时应用于 geom_point
和 stat_ellipse
:
I think you need to pass color
outside aes
for geom_point
, otherwise when you are applying scale_color_manual
, it will apply both for geom_point
and stat_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
这篇关于使stat_ellipse {ggplot2}轮廓geom_point填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!