Hi!
I want to add the number of observations and the mean per group to my graph. I followed this article, but nothing happens: How to add number of observations to a ggplot2 boxplot | by Dr. Gregor Scheithauer | Medium.
I'd like to have it like this:
That is my code:
library(ggplot2)
library(dplyr)
library(tidyverse)
#sample data
Vergleich <- data.frame(
mean_hh_wohnbdl = c(6,6,3,3,3,1,9,1,7,NA,2,4,9,3,3,9,2),
gleich_entf_fakt = c(16.00,66.67,50.00,14.29,NA,42.86,17.14,33.33,116.67,200.00,20.00,66.67,40.00,70.00,NA,33.33,102.00)
)
Vergleich <- mutate(Vergleich,gleich_entf_kat = cut(gleich_entf_fakt,breaks = c(0,30,50,70,90,110,130,150,170,200000),labels = c("5","4","3","2","1","2","3","4","5")))
#function for counting the number of observations and the mean
stat_box_data04.01 <- function(y,upper_limit = max(Vergleich$gleich_entf_fakt)*1.15,na.rm=TRUE){
return(
data.frame(
y=0.95*upper_limit,
label = paste('count =', format(length(y),big.mark = " "),
'\n',
'mean = ', format(round(mean(y),1),big.mark = " "))
)
)
}
#geom_point
Vergleich %>%
drop_na(gleich_entf_kat, mean_hh_wohnbdl, gleich_entf_fakt) %>%
ggplot(Vergleich, mapping = aes(x=as.numeric(mean_hh_wohnbdl),y=as.numeric(gleich_entf_fakt))) +
geom_point(aes(color = factor(gleich_entf_kat))) +
scale_color_manual(values=c("red4","red3","orange","green3","green4"),labels=c("sehr schlecht","schlecht","mäßig","gut","sehr gut")) +
scale_x_continuous(breaks = 1:9,name="Bundesland",labels = c("B","K","NÖ","OÖ","S","ST","T","V","W")) +
scale_fill_manual(breaks = 1:9,name="Bundesland",labels = c("B","K","NÖ","OÖ","S","ST","T","V","W")) +
labs(title = "Übereinstimmung nach Bundesland",
x = "Bundesland",
y = "Übereinstimmungsgrad",
color = "gleich_entf_kat") +
stat_summary(
fun.data = stat_box_data04.01,
geom = "text",
hjust = 0.5,
vjust = 0.9
)+
theme(plot.title = element_text(hjust = 0.5,face = "bold"))
That's how my graph looks like (the number of observations and the mean is missing):
Hope I was clear enough in explaining my problem.
Thanks in advance.