R 添加自定义统计摘要fun.data的图例

R 添加自定义统计摘要fun.data的图例,r,ggplot2,violin-plot,R,Ggplot2,Violin Plot,如何为已使用stat\u summary添加到绘图中的对象添加图例 以下是一个例子: ToothGrowth$dose <- as.factor(ToothGrowth$dose) p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_violin(trim=FALSE) data_summary <- function(x) { m <- mean(x) ymin <- m-sd(x)

如何为已使用
stat\u summary
添加到绘图中的对象添加图例

以下是一个例子:

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + 
    geom_violin(trim=FALSE)
data_summary <- function(x) {
    m <- mean(x)
    ymin <- m-sd(x)
    ymax <- m+sd(x)
    return(c(y=m,ymin=ymin,ymax=ymax))
}
p + stat_summary(fun.data=data_summary)

### Code from http://www.sthda.com/english/wiki/ggplot2-violin-plot-quick-start-guide-r-software-and-data-visualization

ToothGrowth$dose听起来你已经掌握了它的工作原理,将一个常数映射到某种美学上,然后使用
scale\u*\ u manual()
清理图例

scale\u shape\u manual()。我用了
c(“Mean”,“1sd”)
但是这些可以是你想要的任何东西

所需形状的数量由图例框的数量决定,因此我给
,第二个使用
NA
,因为图例中的第二个框应该是一条没有点的线

最后,我使用
guide\u legend()
中的
override.aes()
删除第一个框中的行

p + stat_summary(fun.data=data_summary, aes(shape = "Mean")) +
     scale_shape_manual(name = NULL, 
                        limits = c("Mean", "1 SD"),
                        values = c(19, NA),
                        guide = guide_legend(override.aes = list(linetype = c(0, 1))))

我觉得你的相关链接像是重复的。也许可以在你的问题中澄清为什么这是不同的?这是因为您想向图例中添加两个元素而不是一个元素吗?如果您想创建更具描述性的图例,这可能会有所帮助。是否可以合并这两个对象(
Mean
1 SD
)为了准确地构造出小提琴情节中心的物体,帕罗普南说:“这其实更容易。遵循您提到的链接中的方法。