Replace interactive comparison with plotly
This commit is contained in:
110
server.R
110
server.R
@@ -278,64 +278,60 @@ server <- function(input, output, session) {
|
|||||||
year = currently_compared_year()
|
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
|
location_totals <- data_tibble %>%
|
||||||
geom_col(position = position_dodge(width = 0.9)) +
|
group_by(Location) %>%
|
||||||
scale_fill_brewer(palette = "Blues") +
|
summarise(Total = sum(Erfasst)) %>%
|
||||||
geom_text(
|
arrange(Total)
|
||||||
# Die Text-Ästhetik soll der Wert aus der Spalte 'Erfasst' sein
|
|
||||||
aes(label = format(Erfasst, big.mark = ".", decimal.mark = ",")),
|
data_tibble <- data_tibble %>%
|
||||||
# Platzierung: Y-Wert des Textes = Wert der Spalte + Offset
|
mutate(Location = factor(Location, levels = location_totals$Location))
|
||||||
# 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
|
wide_data <- data_tibble %>%
|
||||||
position = position_dodge(width = 0.9),
|
pivot_wider(names_from = Name, values_from = Erfasst, values_fill = 0)
|
||||||
hjust = -0.3, #schiebt die Zahl nach rechts
|
|
||||||
vjust = 0.5, #schiebt Zahlen mittig hinter Balken
|
first_loc <- levels(data_tibble$Location)[length(levels(data_tibble$Location))]
|
||||||
size = 4,
|
crime_order <- wide_data %>%
|
||||||
fontface = "bold"
|
filter(Location == first_loc) %>%
|
||||||
) +
|
select(-Location) %>%
|
||||||
labs(
|
t() %>%
|
||||||
title = "Polizeilich registrierte Straftaten",
|
as.data.frame() %>%
|
||||||
x = "Anzahl erfasster Fälle",
|
arrange(V1) %>%
|
||||||
y = NULL
|
rownames()
|
||||||
) +
|
|
||||||
scale_x_continuous(expand = expansion(mult = c(0, 0.15))) +
|
wide_data <- wide_data %>%
|
||||||
theme_classic() + #neues theme aus ggthemes packages
|
select(Location, all_of(crime_order))
|
||||||
# NEUE ANPASSUNG: Drehen der X-Achsen-Beschriftungen
|
|
||||||
theme(
|
blue_palette <- colorRampPalette(tail(RColorBrewer::brewer.pal(8, "Blues"), 6))
|
||||||
plot.background = element_rect(
|
plot <- plot_ly() %>%
|
||||||
color = "darkgrey", # Farbe des Rahmens
|
layout(
|
||||||
linewidth = 0.4, # Dicke des Rahmens
|
barmode = 'group',
|
||||||
fill = NA # Füllung: NA = transparent
|
colorway = blue_palette(6),
|
||||||
),
|
xaxis = list(
|
||||||
plot.margin = margin(t = 20, r = 20, b = 20, l = 20, unit = "pt"),
|
title = "<b>Anzahl erfasster Fälle<b>"
|
||||||
legend.position = "bottom",
|
),
|
||||||
legend.text = element_text(size = 13),
|
yaxis = list(
|
||||||
legend.title = element_blank(),
|
title = "<b>Straftat<b>",
|
||||||
# X-Achsen-Titel (z.B. "Straftatbestand")
|
tickangle = "-90"
|
||||||
axis.text.x = element_text(
|
)
|
||||||
vjust = 1,
|
) %>%
|
||||||
hjust = 1,
|
config(
|
||||||
size = 12
|
staticPlot = TRUE
|
||||||
),
|
|
||||||
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
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
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
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user