Add styling to Graph

This commit is contained in:
2025-12-14 15:43:48 +01:00
parent 034195a1d0
commit 5c92cb65da

View File

@@ -7,6 +7,8 @@ library(htmltools)
library(dplyr) library(dplyr)
library(purrr) library(purrr)
library(ggplot2) library(ggplot2)
library(ggthemes)
library(stringr)
# Json of Crime Reports # Json of Crime Reports
crime_json <- fromJSON(file="data.json") crime_json <- fromJSON(file="data.json")
@@ -39,7 +41,7 @@ map_data_to_top3_plot <- function(bezirk, stadtteil, year) {
komplettes_tibble <- map_df(names(crime_json[[bezirk]][[stadtteil]]), function(crime) { komplettes_tibble <- map_df(names(crime_json[[bezirk]][[stadtteil]]), function(crime) {
row <- crime_json[[bezirk]][[stadtteil]][[crime]][[year]] row <- crime_json[[bezirk]][[stadtteil]][[crime]][[year]]
tibble( tibble(
Name = crime, Name = str_wrap(crime, width = 25),
Erfasst = as.integer(row[["Erfasste Fälle"]]), Erfasst = as.integer(row[["Erfasste Fälle"]]),
) )
}) })
@@ -50,6 +52,7 @@ map_data_to_top3_plot <- function(bezirk, stadtteil, year) {
# Beschränken: Wählt die Zeilen 2, 3 und 4 aus. # Beschränken: Wählt die Zeilen 2, 3 und 4 aus.
# Dies sind die Ränge 2, 3 und 4. # Dies sind die Ränge 2, 3 und 4.
slice(2:4) slice(2:4)
#top_3_tibble$Name_wrapped <- str_wrap(top_3_tibble$Name, width = 25) #mit wrap ein umbruch bei den namen eingefügt
return(top_3_tibble) return(top_3_tibble)
} }
@@ -118,6 +121,7 @@ ui <- function() {
), ),
plotOutput("grph_top3"), plotOutput("grph_top3"),
#tableOutput("tbl_2024"), #tableOutput("tbl_2024"),
), ),
col_widths = c(8, 4), col_widths = c(8, 4),
), ),
@@ -142,7 +146,7 @@ ui <- function() {
), ),
) )
), ),
nav_panel("Statistik", "Statistik_inhalt"), nav_panel("Vergleich", "Vergleich_inhalt"),
nav_panel("Wiki", nav_panel("Wiki",
accordion( accordion(
accordion_panel( accordion_panel(
@@ -294,9 +298,53 @@ server <- function(input, output, session) {
year = "2024" year = "2024"
) )
req(nrow(data_tibble) > 0) req(nrow(data_tibble) > 0)
ggplot(data_tibble, aes(x = Name, y = Erfasst, fill = Name)) + ggplot(data_tibble, aes(x = Name, y = Erfasst)) +
geom_col() geom_col(width = 0.7, fill = "#7bb5ab") + # <-- Festlegen der Farbe direkt an allen Spalten angeknüpft nicht mehr anhand der Kategorie
}) 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
vjust = -0.5, # Vertikale Ausrichtung: Negativer Wert platziert Text über dem Punkt
size = 4,
fontface = "bold"
) +
labs(
title = "Statistisch am häufigsten polizeilich registrierte Straftaten",
x = "Straftatbestand",
y = "Anzahl erfasster Fälle"
) +
theme_pander() + #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
),
plot.margin = margin(t = 20, r = 20, b = 20, l = 20, unit = "pt"),
legend.position = "none",
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(), # vertikale grid lines entfernen
# X-Achsen-Titel (z.B. "Straftatbestand")
axis.title.x = element_text(
face = "bold",
family = "sans",
# Fügen Sie hier einen Abstand nach OBEN hinzu
margin = margin(t = 15) # t = top (oben) in Pixeln
),
# 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
),
)
}, res = 100)
output$tbl_2024 <- renderTable( output$tbl_2024 <- renderTable(
map_data_to_table( map_data_to_table(