r - 使 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
但是当我在点周围添加黑色轮廓时,它会使我的椭圆变黑,使它们无法解释。
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
我不想要填充颜色,因为椭圆之间有太多重叠,以至于看不到任何东西。
感谢您的时间。
解决方案
我认为你需要通过color
外部aes
for geom_point
,否则当你申请时scale_color_manual
,它会同时申请geom_point
和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
推荐阅读
- c++ - 逐字获取字符串的函数
- javascript - 长输入的更快解决方案?
- django - Djnago,是否可以使用模型创建多个状态选择?
- reactjs - React - 从 API 获取,CORS 策略错误
- php - PHP-SPO 上传大文件 - 请求消息太大
- windows-installer - MSI - 错误:无法创建 AppData 缓存文件夹
- javascript - 评估 `true && {}` 是否涉及类型强制?
- python - 纸浆:如何将决策变量输出加/减在一起
- cloudera - Impala:获取匹配分区列表
- ios - UILabel 带有颜色的特定单词和用户交互,在 Swift 中启用它们