【问题标题】:Add label with mean value of y-axis variable to geom_point in ggplot将带有y轴变量平均值的标签添加到ggplot中的geom_point
【发布时间】:2018-09-05 19:07:45
【问题描述】:

我有以下代码(见下文)。我想将变量 value 的平均值作为标签添加到图表中的点。有什么建议? 谢谢你的帮助! :)

df_1 <- data.frame(group=rep(c("A","B","C"),3),value=rnorm(9))

ggplot(df_1,aes(x=group,y=value)) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1) +
  stat_summary(fun.y=mean, geom="point")

【问题讨论】:

    标签: r ggplot2 label mean


    【解决方案1】:

    添加另一个stat_summary

    ggplot(df_1,aes(x=group,y=value)) +
        stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1) +
        stat_summary(fun.y=mean, geom="point") +
        stat_summary(fun.y=mean, geom="label", aes(label = round(..y.., 2)), hjust = -0.1)
    

    geom_label:

    ggplot(df_1,aes(x=group,y=value)) +
        stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1) +
        stat_summary(fun.y=mean, geom="point") +
        geom_label(stat = 'summary', fun.y=mean, aes(label = round(..y.., 2)), nudge_x = 0.1, hjust = 0)
    

    【讨论】:

    • ...还有一件事。执行代码时,标签周围有一个黑框。有什么办法可以去掉那个框架?
    • 也许你想要geom_text 而不是geom_label
    猜你喜欢
    • 1970-01-01
    • 2016-06-12
    • 2014-07-25
    • 2012-04-26
    • 2015-10-06
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 2019-05-06
    相关资源
    最近更新 更多