r - 改变ggplot2图例中中位数和均值的颜色
问题描述
请忽略整个图表上的随机点 - 这只是一个快速可重复的示例来说明我的意思:
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, fill = Species)) +
geom_violin(alpha = 0.5) +
stat_summary(aes(shape="mean",group=1),fun = "mean",
size = 2, geom = "point", color = "black") +
stat_summary(aes(shape = "median", group = 2), fun = "median",
size = 2, geom = "point", color = "red") +
labs(x = "Sepal Length", y = "Sepal Width",
shape = "Shape", colour = "Species") +
theme_classic()
我如何能:
一个)。更改图例,使每个分类变量的框中间没有红点?乙)。更改“形状”图例的颜色,使“平均值”为黑色,“中值”为红色?
我已经用 Google 搜索了一个多小时,但找不到答案,因此非常感谢您的帮助。谢谢!
解决方案
图例指南有一个override.aes
参数,您可以使用它来明确设置键的美感。
library(ggplot2)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, fill = Species)) +
geom_violin(alpha = 0.5) +
stat_summary(aes(shape="mean",group=1),fun = "mean",
size = 2, geom = "point", color = "black") +
stat_summary(aes(shape = "median", group = 2), fun = "median",
size = 2, geom = "point", color = "red") +
labs(x = "Sepal Length", y = "Sepal Width",
shape = "Shape", colour = "Species") +
guides(
shape = guide_legend(override.aes = list(colour = c("black", "red"))),
fill = guide_legend(override.aes = list(shape = NA))
) +
theme_classic()
#> Warning: position_dodge requires non-overlapping x intervals
推荐阅读
- python - 从一系列数组中获取一个 numy nd 数组
- python - 如何在 python 中编写文件而不将其加载到内存中?
- python - 从 MPI 中排除 Python 语法的某些部分
- javascript - 如何更新一个端点上的两个实体?
- android - android 故障:Android 10 的 xml 中的颜色
- bash - 如何在bash脚本中迭代两个变量?
- react-hooks - 如何设置对象值数组的状态并在单击时将计数切换为 1 或 2
- python - 获取一段时间的均方根误差 (RMSE)
- go - 为什么我不能像这样直接调用map成员的方法
- python - 使用 python 抓取谷歌精选片段