Use plotly for better graphs

This commit is contained in:
2026-02-05 16:23:45 +01:00
parent d31650131b
commit dc790f554d
3 changed files with 12 additions and 10 deletions

View File

@@ -9,6 +9,7 @@ library(purrr)
library(ggplot2) library(ggplot2)
library(ggthemes) library(ggthemes)
library(stringr) library(stringr)
library(plotly)
# Json of Crime Reports # Json of Crime Reports
crime_json <- fromJSON(file="data.json") crime_json <- fromJSON(file="data.json")
get_bezirk_by_stadtteil <- function(name) { get_bezirk_by_stadtteil <- function(name) {

View File

@@ -216,14 +216,14 @@ server <- function(input, output, session) {
) )
}) })
output$grph_top3 <- renderPlot({ output$grph_top3 <- renderPlotly({
data_tibble <- map_data_to_top3_plot( data_tibble <- map_data_to_top3_plot(
bezirk = currently_selected_bezirk(), bezirk = currently_selected_bezirk(),
stadtteil = currently_selected_stadtteil(), stadtteil = currently_selected_stadtteil(),
year = "2024" year = "2024"
) )
req(nrow(data_tibble) > 0) req(nrow(data_tibble) > 0)
ggplot(data_tibble, aes(x = reorder(Name, -Erfasst), y = Erfasst, fill = Name)) + ggplotly(ggplot(data_tibble, aes(x = reorder(Name, -Erfasst), y = Erfasst, fill = Name)) +
geom_col(width = 0.7) + geom_col(width = 0.7) +
scale_x_discrete( scale_x_discrete(
labels = function(x) str_wrap(x, width = 15)) + labels = function(x) str_wrap(x, width = 15)) +
@@ -279,8 +279,8 @@ server <- function(input, output, session) {
margin = margin(r = 15) # r = right (rechts) in Pixeln margin = margin(r = 15) # r = right (rechts) in Pixeln
), ),
) )
)
}, res = 100) })
observeEvent(input$vergleich_location, { observeEvent(input$vergleich_location, {
currently_compared_location(c(input$vergleich_location)) currently_compared_location(c(input$vergleich_location))
@@ -289,14 +289,14 @@ server <- function(input, output, session) {
currently_compared_crimes(input$vergleich_straftat) currently_compared_crimes(input$vergleich_straftat)
}) })
output$vergleich_balkendiagramm <- renderPlot({ output$vergleich_balkendiagramm <- renderPlotly({
data_tibble <- map_data_to_plot( data_tibble <- map_data_to_plot(
locations = currently_compared_location(), locations = currently_compared_location(),
crimes = currently_compared_crimes(), crimes = currently_compared_crimes(),
year = currently_compared_year() year = currently_compared_year()
) )
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 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)) + geom_col(position = position_dodge(width = 0.9)) +
scale_fill_brewer(palette = "Blues") + scale_fill_brewer(palette = "Blues") +
geom_text( geom_text(
@@ -352,7 +352,8 @@ server <- function(input, output, session) {
# Fügen Sie hier einen Abstand nach RECHTS hinzu # Fügen Sie hier einen Abstand nach RECHTS hinzu
margin = margin(r = 15) # r = right (rechts) in Pixeln margin = margin(r = 15) # r = right (rechts) in Pixeln
) )
) )
}, res = 100) )
})
} }

4
ui.R
View File

@@ -227,7 +227,7 @@ ui <- function() {
uiOutput("txt_map_selection_bezirk"), uiOutput("txt_map_selection_bezirk"),
uiOutput("txt_map_selection_stadtteil"), uiOutput("txt_map_selection_stadtteil"),
), ),
plotOutput("grph_top3"), plotlyOutput("grph_top3", width = "484px", height = "667px"),
), ),
col_widths = breakpoints( col_widths = breakpoints(
sm = c(12, 12), # Auf kleinen Bildschirmen: untereinander (100% Breite) sm = c(12, 12), # Auf kleinen Bildschirmen: untereinander (100% Breite)
@@ -305,7 +305,7 @@ ui <- function() {
), ),
card( card(
card_header(uiOutput("vergleichs_titel")), # Dynamischer Titel card_header(uiOutput("vergleichs_titel")), # Dynamischer Titel
plotOutput("vergleich_balkendiagramm") plotlyOutput("vergleich_balkendiagramm")
) )
) )
), ),