Ggplot2 легенда для stat_summary
Как создать легенду о том, что красный крест является средним?
ggplot(results, aes(x=factor, y=proportionPositive)) +
geom_boxplot() +
stat_summary(fun.data = "mean_cl_normal", colour = "red", shape=4)
![enter image description here]()
Ответы
Ответ 1
Вот один из способов сделать это:
- Составить эстетическую форму, т.е. aes (shape = "mean" )
- Создайте масштаб формы вручную, то есть scale_shape_manual()
# Create dummy data
results <- data.frame(
factor=factor(rep(1:10, 100)),
proportionPositive=rnorm(1000))
# Plot results
ggplot(results, aes(x=factor, y=proportionPositive)) +
geom_boxplot() +
stat_summary(fun.data = "mean_cl_normal",
aes(shape="mean"),
colour = "red",
geom="point") +
scale_shape_manual("", values=c("mean"="x"))
![enter image description here]()
Ответ 2
Чтобы он отображался как легенда по умолчанию (заимствование из кода @Andrie):
ggplot(results, aes(x=factor, y=proportionPositive)) +
geom_boxplot() +
stat_summary(fun.data = "mean_cl_normal",
aes(shape=""), # Leave empty
colour = "red",
geom="point") +
scale_shape_manual("mean", values= "") # Will show mean on top of the line