This data shows the over all cases of Family Domestic Violence over 5 years. Data taken from ABS.


Break down of gender


What Offences are considered FDV

Lets take a closer look

Shiny applications not supported in static R Markdown documents

Talk about the different offences

Homocide.


https://plot.ly/ggplot2/

If you use ggplot2, ggplotly() converts your plots to an interactive, web-based version! It also provides sensible tooltips, which assists decoding of values encoded as visual properties in the plot.

plotly supports some chart types that ggplot2 doesn’t (such as 3D surface, point, and line plots). You can create these (or any other plotly) charts using plot_ly().

Escalation of Family Violence


https://hrbrmstr.github.io/metricsgraphics/

Building metricsgraphics charts follows the “piping” idiom made popular through the magrittr, ggvis and dplyr packages. This makes it possible to avoid one giant function with a ton of parameters and facilitates breaking out the chart building into logical steps.

While MetricsGraphics.js charts may not have the flexibility of ggplot2, you can build functional, interactive [multi-]line, scatterplot, bar charts & histograms and + even link charts together.

---
title: "Family Domestic Violence: 5-Year Overview"
output: 
  flexdashboard::flex_dashboard:
    storyboard: true
    social: menu
    source: embed
---


```{r setup, include=FALSE}
library(flexdashboard)
library(here)
library(readr)
library(openxlsx)
library(readxl)
library(dplyr)
library(magrittr)
library(ggplot2)
library(stringr)
library(tidyr)
library(tidyverse)
library(lubridate)
library(ozmaps)
library(sf)
library(plotly)
library(gridExtra)
```


### This data shows the over all cases of Family Domestic Violence over 5 years. Data taken from ABS. 

```{r}
FDV<- read.xlsx(here("data", "FDVa.xlsx"))
totalsFDV <- FDV %>%
  group_by(year, gender) %>%
  summarise(totalFDV = sum(defendant))
T1 <- ggplot(totalsFDV, aes(x = year, y = totalFDV)) +
  geom_bar(stat = "identity", position = "dodge", fill = "#9ebcda") +
  labs(title = "FDV Cases",
       x = "Year",
       y = "Total Cases") +
  theme_minimal()
ggplotly(T1)
```

***



- see an over all increase

- defined by police officers

- information is people who are charged with FDV does not show who was guilty

- xxxx

- xxxx

- xxxx


### Break down of gender

```{r}
T2 <- ggplot(totalsFDV, aes(x = year, y = totalFDV, fill = gender)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values = c("#8856a7","#2ca25f")) +
  labs(title = "FDV Cases",
       x = "Year",
       y = "Total Cases",
       fill = "Gender") +
  theme_minimal()

ggplotly(T2)
```

***



- much higher proportion of defendants are male

- click onto bars to see actual numbers

- 


### What Offences are considered FDV

```{r}
crime_data <- FDV %>%
  group_by(crime, year, gender) %>%
  summarise(total_incidents = sum(defendant, na.rm = TRUE))

C1 <- ggplot(crime_data, aes(x = year, y = total_incidents, fill = reorder(crime, total_incidents, FUN = sum))) +
  geom_col(position = "dodge", colour = "white") +
  labs(title = "Offences Commited",
       x = "Year",
       y = "Total Incidents",
       fill = "Offence") +
  scale_fill_discrete( labels = c("Homicide", "Abduction & Harassment", "Sexual Assault", "Stalking & Threatening behaviour", "Property Damage", "Breach of Violence Order", "Assualt")) +
  theme_minimal()
C1


```


### Lets take a closer look

```{r}
# Load necessary libraries
library(shiny)
library(plotly)
library(dplyr)

# Sample data frame (use your own data frame)
FDV <- read.xlsx(here("data", "FDVa.xlsx"))
ui <- fluidPage(
    titlePanel("FDV Cases by Crime"),
    
    # Sidebar layout
    sidebarLayout(
        sidebarPanel(
            # Dropdown menu for selecting the crime
            selectInput("crime", "Select Crime:", 
                        choices = unique(FDV$crime), 
                        selected = unique(FDV$crime)[1])
        ),
        # Main panel for displaying the plot
        mainPanel(
            plotlyOutput("stacked_bar_plot")
        )
    )
)

# Server logic
server <- function(input, output) {
    # Render the Plotly plot
    output$stacked_bar_plot <- renderPlotly({
        # Filter data based on selected crime
        filtered_data <- FDV %>%
            filter(crime == input$crime)
        
        # Create a stacked bar plot with Plotly
        plotly_chart <- plot_ly(
            data = filtered_data,
            x = ~year,
            y = ~defendant,
            color = ~gender,
            colors = c("#8856a7", "#2ca25f"),
            type = "bar",
            barmode = "stack"
        ) %>%
            layout(
                title = paste("FDV Cases -", input$crime),
                xaxis = list(title = "Year"),
                yaxis = list(title = "Number of Defendants"),
                barmode = "stack"
            )
        
        # Return the Plotly plot
        plotly_chart
    })
}

# Run the Shiny app
shinyApp(ui, server)
```

***

Talk about the different offences

- xxx

- xxx

- xxx

- xxx


### Homocide.

```{r}
homocide <- FDV %>%
  filter(crime == "homicide") %>%
  group_by(year, gender) %>%
  summarise(total_incidents = sum(defendant, na.rm =TRUE))

H1 <- ggplot(homocide, aes( x = year, y = total_incidents, fill = gender)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values = c("#8856a7","#2ca25f")) +
  labs(titles ="Homicide Cases",
       x = "Years",
       y = "Total Cases",
       fill = "Gender") +
  theme_minimal()
H1
ggplotly(H1)
```

***

https://plot.ly/ggplot2/

If you use ggplot2, `ggplotly()` converts your plots to an interactive, web-based version! It also provides sensible tooltips, which assists decoding of values encoded as visual properties in the plot.

plotly supports some chart types that ggplot2 doesn't (such as 3D surface, point, and line plots). You can create these (or any other plotly) charts using `plot_ly()`.


### Escalation of Family Violence

```{r}
assault <- FDV %>%
  filter(crime == "assault") %>%
  group_by(year, gender) %>%
  summarise(total_incidents = sum(defendant, na.rm = TRUE)) %>%
  mutate(crime_type = "Assault")

BVO <- FDV %>%
  filter(crime == "breach_of_violence_order") %>%
  group_by(year, gender) %>%
  summarise(total_incidents = sum(defendant, na.rm = TRUE)) %>%
  mutate(crime_type = "Breach of Violence Order")

# Combine the two data sets
combined_data <- rbind(assault, BVO)

# Create the faceted plot
faceted_plot <- ggplot(combined_data, aes(x = year, y = total_incidents, fill = gender)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values = c("#8856a7", "#2ca25f")) +
  labs(title = "FDV Cases",
       x = "Year",
       y = "Total Cases",
       fill = "Gender") +
  theme_minimal() +
  facet_wrap(~ crime_type, scales = "free_y")

# Print the plot
ggplotly(faceted_plot)
```

***

https://hrbrmstr.github.io/metricsgraphics/

Building metricsgraphics charts follows the “piping” idiom made popular through the magrittr, ggvis and dplyr packages. This makes it possible to avoid one giant function with a ton of parameters and facilitates breaking out the chart building into logical steps. 

While MetricsGraphics.js charts may not have the flexibility of ggplot2, you can build functional, interactive [multi-]line, scatterplot, bar charts & histograms and + even link charts together.