Thanks a lot for your help! I replaced the na.omit() by na_drop(gleich_entf_kat, mean_hh_wohnbdl, gleich_entf_fakt), and now it's working.
Here is the final solution of the problem:
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")))
text_labels <- group_by(
Vergleich %>% drop_na(gleich_entf_kat, mean_hh_wohnbdl, gleich_entf_fakt),
mean_hh_wohnbdl
) %>% summarise(
textlabel = paste(
"count =", format(n(), big.mark = " "),
"\n",
"mean = ", format(round(mean(gleich_entf_fakt), 1), big.mark = " ")
),
#options for how to vertically place the text
y=max(Vergleich$gleich_entf_fakt,na.rm = TRUE)
)
Vergleich %>%
drop_na(gleich_entf_kat, mean_hh_wohnbdl, gleich_entf_fakt) %>%
ggplot(Vergleich, mapping = aes(
x = mean_hh_wohnbdl,
y = gleich_entf_fakt,
color = gleich_entf_kat
)) +
geom_point() +
geom_text(
data = text_labels,
aes(label = textlabel,color=NULL,
y=y),
size=3
) +
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"
) +
theme(plot.title = element_text(hjust = 0.5, face = "bold"))