Jens Stach
Jens Stach

Reputation: 139

Add label with mean value of y-axis variable to geom_point in ggplot

I have the following code (see below). I want to add the mean value of the variable value as a label to the points in the chart. Any suggestions? Thanks for your help! :)

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")

Upvotes: 0

Views: 2906

Answers (1)

Axeman
Axeman

Reputation: 35307

Add another 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)

Or 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)

Upvotes: 1

Related Questions