легенда ggplot2 для stat_summary - PullRequest
12 голосов
/ 03 марта 2011

Как мне создать легенду, сообщающую, что красный крест - это среднее?

ggplot(results, aes(x=factor, y=proportionPositive)) +
geom_boxplot() +
stat_summary(fun.data = "mean_cl_normal", colour = "red", shape=4)

enter image description here

Ответы [ 2 ]

19 голосов
/ 03 марта 2011

Вот один из способов сделать это:

  1. Сопоставить эстетику с формой, то есть aes (shape = "mean")
  2. Создать ручную шкалу формы, т.е. 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

0 голосов
/ 28 октября 2017

Чтобы он выглядел как легенда по умолчанию (позаимствовано из кода @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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...