Make stat_ellipse {ggplot2} outline geom_point fill color

早过忘川 提交于 2021-02-08 05:07:35

问题


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.

Thank you for your time.


回答1:


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



来源:https://stackoverflow.com/questions/61236758/make-stat-ellipse-ggplot2-outline-geom-point-fill-color

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!