stat_summary的ggplot2图例

时间:2011-03-03 09:27:32

标签: r ggplot2

如何创建一个图例,告知红十字是什么意思?

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 个答案:

答案 0 :(得分:19)

这是一种方法:

  1. 将美学映射到形状,即aes(shape =“mean”)
  2. 创建手动形状比例,即scale_shape_manual()
  3. # 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

答案 1 :(得分:0)

使其看起来像默认图例(借用@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