From 034195a1d037d9810bdce8680351d48c4b38da8e Mon Sep 17 00:00:00 2001 From: Millicool Date: Sun, 14 Dec 2025 14:03:45 +0100 Subject: [PATCH] Add bar plot to right panel --- IT Shiny App.R | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/IT Shiny App.R b/IT Shiny App.R index 801ef76..c837626 100644 --- a/IT Shiny App.R +++ b/IT Shiny App.R @@ -6,6 +6,7 @@ library(sf) library(htmltools) library(dplyr) library(purrr) +library(ggplot2) # Json of Crime Reports crime_json <- fromJSON(file="data.json") @@ -30,6 +31,29 @@ map_data_to_table <- function(bezirk, stadtteil, year) { }) } +map_data_to_top3_plot <- function(bezirk, stadtteil, year) { + year <- as.character(trimws(year)) + req(bezirk) + req(stadtteil) + req(year) + komplettes_tibble <- map_df(names(crime_json[[bezirk]][[stadtteil]]), function(crime) { + row <- crime_json[[bezirk]][[stadtteil]][[crime]][[year]] + tibble( + Name = crime, + Erfasst = as.integer(row[["Erfasste Fälle"]]), + ) + }) + # Sortieren und Beschränken auf die Ränge 2, 3 und 4 + top_3_tibble <- komplettes_tibble %>% + # Sortieren: Absteigend nach "Erfasste Fälle". Der höchste Wert ist nun in Zeile 1. + arrange(desc(Erfasst)) %>% + # Beschränken: Wählt die Zeilen 2, 3 und 4 aus. + # Dies sind die Ränge 2, 3 und 4. + slice(2:4) + + return(top_3_tibble) +} + #GeoJson for Bezirke geo_bezirke <- st_read("geobezirke-parsed.json") geo_bezirke <- st_transform(geo_bezirke, crs = 4326) @@ -85,12 +109,12 @@ ui <- function() { div( class = "d-flex align-items-end gap-2", h5(strong("Bezirk:"), style = "margin-bottom: 0;"), - textOutput("txt_map_selection_bezirk"), + textOutput("txt_map_selection_bezirk"), ), div( class = "d-flex align-items-end gap-2", h5(strong("Stadtteil:"), style = "margin-bottom: 0;"), - textOutput("txt_map_selection_stadtteil"), + textOutput("txt_map_selection_stadtteil"), ), plotOutput("grph_top3"), #tableOutput("tbl_2024"), @@ -258,11 +282,22 @@ server <- function(input, output, session) { ) }) output$txt_map_selection_bezirk <- renderText({ - paste("Bezirk:", currently_selected_bezirk()) + currently_selected_bezirk() }) output$txt_map_selection_stadtteil <- renderText({ - paste("Stadtteil:", currently_selected_stadtteil()) + currently_selected_stadtteil() }) + output$grph_top3 <- renderPlot({ + data_tibble <- map_data_to_top3_plot( + bezirk = currently_selected_bezirk(), + stadtteil = currently_selected_stadtteil(), + year = "2024" + ) + req(nrow(data_tibble) > 0) + ggplot(data_tibble, aes(x = Name, y = Erfasst, fill = Name)) + + geom_col() + }) + output$tbl_2024 <- renderTable( map_data_to_table( bezirk = currently_selected_bezirk(),