Replace interactive comparison with plotly

This commit is contained in:
2026-02-07 11:31:55 +01:00
parent fdf2cf8331
commit 93123d0ff6

104
server.R
View File

@@ -278,64 +278,60 @@ server <- function(input, output, session) {
year = currently_compared_year()
)
ggplotly(ggplot(data_tibble, aes(x = Erfasst, y = reorder(Location, Erfasst, FUN = sum), group = Name, fill = Name)) + #reorder macht die höchsten Werte nach oben, und sortiert nach Gesamtwert
geom_col(position = position_dodge(width = 0.9)) +
scale_fill_brewer(palette = "Blues") +
geom_text(
# Die Text-Ästhetik soll der Wert aus der Spalte 'Erfasst' sein
aes(label = format(Erfasst, big.mark = ".", decimal.mark = ",")),
# Platzierung: Y-Wert des Textes = Wert der Spalte + Offset
# Wir verwenden den Offset, um den Text knapp über den Balken zu platzieren
# Wenn Sie den Text IN den Balken setzen möchten, setzen Sie y=Erfasst/2
position = position_dodge(width = 0.9),
hjust = -0.3, #schiebt die Zahl nach rechts
vjust = 0.5, #schiebt Zahlen mittig hinter Balken
size = 4,
fontface = "bold"
) +
labs(
title = "Polizeilich registrierte Straftaten",
x = "Anzahl erfasster Fälle",
y = NULL
) +
scale_x_continuous(expand = expansion(mult = c(0, 0.15))) +
theme_classic() + #neues theme aus ggthemes packages
# NEUE ANPASSUNG: Drehen der X-Achsen-Beschriftungen
theme(
plot.background = element_rect(
color = "darkgrey", # Farbe des Rahmens
linewidth = 0.4, # Dicke des Rahmens
fill = NA # Füllung: NA = transparent
location_totals <- data_tibble %>%
group_by(Location) %>%
summarise(Total = sum(Erfasst)) %>%
arrange(Total)
data_tibble <- data_tibble %>%
mutate(Location = factor(Location, levels = location_totals$Location))
wide_data <- data_tibble %>%
pivot_wider(names_from = Name, values_from = Erfasst, values_fill = 0)
first_loc <- levels(data_tibble$Location)[length(levels(data_tibble$Location))]
crime_order <- wide_data %>%
filter(Location == first_loc) %>%
select(-Location) %>%
t() %>%
as.data.frame() %>%
arrange(V1) %>%
rownames()
wide_data <- wide_data %>%
select(Location, all_of(crime_order))
blue_palette <- colorRampPalette(tail(RColorBrewer::brewer.pal(8, "Blues"), 6))
plot <- plot_ly() %>%
layout(
barmode = 'group',
colorway = blue_palette(6),
xaxis = list(
title = "<b>Anzahl erfasster Fälle<b>"
),
plot.margin = margin(t = 20, r = 20, b = 20, l = 20, unit = "pt"),
legend.position = "bottom",
legend.text = element_text(size = 13),
legend.title = element_blank(),
# X-Achsen-Titel (z.B. "Straftatbestand")
axis.text.x = element_text(
vjust = 1,
hjust = 1,
size = 12
),
axis.title.x = element_text(
face = "bold",
family = "sans",
# Fügt Abstand nach OBEN hinzu
margin = margin(t = 15), # t = top (oben) in Pixeln
size = 12
),
axis.text.y = element_text(
size = 13
),
# Y-Achsen-Titel (z.B. "Anzahl erfasster Fälle")
axis.title.y = element_text(
face = "bold",
family = "sans",
# Fügen Sie hier einen Abstand nach RECHTS hinzu
margin = margin(r = 15) # r = right (rechts) in Pixeln
yaxis = list(
title = "<b>Straftat<b>",
tickangle = "-90"
)
) %>%
config(
staticPlot = TRUE
)
for(i in seq_along(crime_order)) {
crime <- crime_order[i]
plot <- plot %>%
add_trace(
x = wide_data[[crime]],
y = wide_data$Location,
name = crime,
text = format(wide_data[[crime]], big.mark = ".", decimal.mark = ","),
textposition = "outside",
type = "bar",
orientation = "h",
legendrank = length(crime_order) - i
)
}
plot
})
}