ich stehe vor dem folgenden Problem:
Ich möchte ein Säulendiagramm durch Mittelwerte und Standardabweichungen ergänzen. Dieses habe ich auch durch manuelle und individuelle Eingaben sehr aufwendig hinbekommen.
Hier mein Ergebnis:
Der Code mit reproduzierbarem Beispiel lautet wie folgt
Code: Alles auswählen
library (dplyr)
library (car)
library (ggplot2)
library (tidyr)
Auto_wide <-
tibble(
'Mobil' = c("Motorrad (n=44)", "Auto (n=44)" ),
'1' = c(0, 0.36),
'2' = c(0, 0.20),
'3' = c(0, 0.25),
'4' = c(0, 0.09),
'5' = c(0, 0.09)
)
Auto_long <- Auto_wide %>%
pivot_longer(c('1','2','3','4','5'))
Auto_long$name <- factor(Auto_long$name, c('1','2','3','4','5'))
Motorrad_wide <-
tibble(
'Mobil' = c("Motorrad (n=44)", "Auto (n=44)" ),
'1' = c(0.34, 0),
'2' = c(0.25, 0),
'3' = c(0.27, 0),
'4' = c(0.07, 0),
'5' = c(0.07, 0)
)
Motorrad_long <- Motorrad_wide %>%
pivot_longer(c('1','2','3','4','5'))
Motorrad_long$name <- factor(Motorrad_long$name, c('1','2','3','4','5'))
Auto_long$name <- as.numeric(Auto_long$name)
Motorrad_long$name <- as.numeric(Motorrad_long$name)
barwidth = 0.35
MWAuto <- data.frame(x1=2.07, x2=2.07, y1=0.42, y2=0.38)
MWMotorrad <- data.frame(x1=2.07, x2=2.07, y1=0.48, y2=0.44)
StdabwAuto <- data.frame(x1=1.04, x2=3.11, y1=0.4, y2=0.4)
StdabwAutol <- data.frame(x1=1.04, x2=1.04, y1=0.39, y2=0.41)
StdabwAutor <- data.frame(x1=3.11, x2=3.11, y1=0.39, y2=0.41)
StdabwMotorrad <- data.frame(x1=1.1, x2=3.05, y1=0.46, y2=0.46)
StdabwMotorradl <- data.frame(x1=1.1, x2=1.1, y1=0.45, y2=0.47)
StdabwMotorradr <- data.frame(x1=3.05, x2=3.05, y1=0.45, y2=0.47)
ggplot() +
geom_col(data = Auto_long,
mapping = aes(x = name + 0.2, y = value, fill = Mobil),
width = barwidth) +
geom_text(data = Auto_long,
aes(x=name + 0.2, label = ifelse(value > 0, paste(sprintf("%.0f", value*100), "%", sep=""), ""), y = value), size = 4, position = position_stack(vjust=0.5), colour="white") +
geom_col(data = Motorrad_long,
mapping = aes(x = name - 0.2, y = value, fill = Mobil),
width = barwidth) +
geom_text(data = Motorrad_long,
aes(x=name - 0.2, label = ifelse(value > 0, paste(sprintf("%.0f", value*100), "%", sep=""), ""), y = value), size = 4, position = position_stack(vjust=0.5), colour="white") +
geom_segment(data=MWfull, aes(x=x1, y=y1, xend=x2, yend=y2), size=2.5, colour="gray60") +
geom_segment(data=MWkmu, aes(x=x1, y=y1, xend=x2, yend=y2), size=2.5, colour="gray30") +
geom_segment(data=StdabwAuto, aes(x=x1, y=y1, xend=x2, yend=y2), size=1, colour="black") +
geom_segment(data=StdabwMotorrad, aes(x=x1, y=y1, xend=x2, yend=y2), size=1, colour="black") +
geom_segment(data=StdabwAutol, aes(x=x1, y=y1, xend=x2, yend=y2), size=1, colour="black") +
geom_segment(data=StdabwAutor, aes(x=x1, y=y1, xend=x2, yend=y2), size=1, colour="black") +
geom_segment(data=StdabwMotorradl, aes(x=x1, y=y1, xend=x2, yend=y2), size=1, colour="black") +
geom_segment(data=StdabwMotorradr, aes(x=x1, y=y1, xend=x2, yend=y2), size=1, colour="black") +
geom_vline(xintercept = 4.5, size=0.5) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
scale_fill_manual(values= c("gray60", "gray30")) +
theme(axis.title.x=element_blank(),
panel.background = element_rect(fill = 'white'),
panel.grid = element_line(colour = "gray80"),
axis.text.x = element_blank(),
axis.ticks.x=element_blank(),
legend.position = "bottom",
legend.title = element_blank(),
plot.title=element_text(size = 20),
panel.grid.minor.x = element_blank()) +
guides(fill = guide_legend(reverse = TRUE)) +
annotate("text", x=1, y= -0.02, label="(1)\nsehr gering") +
annotate("text", x=2, y= -0.02, label="(2)\ngering") +
annotate("text", x=3, y= -0.02, label="(3)\nmittelmäßig") +
annotate("text", x=4, y= -0.02, label="(4)\ngut") +
annotate("text", x=5, y= -0.02, label="Beurteilung\nnicht möglich") +
annotate("text", x=2.07, y= 0.35, label="MW\nAuto") +
annotate("text", x=2.07, y= 0.52, label="MW\nMotorrad")
Ich stelle mir nun die Frage, ob GGPlot hier bereits eine einfachere Lösung anbietet. Hat da jemand einen Tipp für mich?
Vielen Dank!