The World’s 15 Most Crime-Prone Countries


The chart communicates a clear message: crime severity is heavily concentrated in a handful of Latin-American and Sub-Saharan countries, with conflict-zone outliers. Venezuela stand out with the highest crime index

Global Champions of Safety: Top-15 Nations


This chart lists the 15 safest countries in the dataset according to “Safety Index,” where bigger numbers mean people feel more secure and crime is low. The longer the bar, the higher the score; the colour echoes that score, going from deep blue-purple (still very safe) up through bright yellow for the very safest. Qatar tops the list with the longest, vivid-yellow bar, followed closely by the United Arab Emirates and Oman, showing a particularly strong showing for Gulf states. Switzerland, Japan, and Slovenia round out the upper tier, while Austria closes the list—but even its deep-blue bar sits around 75, which is well above the world average. Altogether, the chart highlights that these countries share a narrow, high range of scores (mid-70s to high-80s), signalling consistently low crime and strong public safety perceptions across diverse regions.

Crime vs Safety: Perfect Mirror Relationship


This bubble-plot shows that countries with high Crime Index scores (worse crime) always have low Safety Index scores, and vice-versa. Each bubble’s color shows the continent and its size shows population, but those extra details don’t break the pattern—the dots from every region hug the same line, whether they’re tiny (small nations) or huge (China, India).

Does Rising Unemployment Drive Up Crime?


This scatter-plot looks at whether countries with higher unemployment also tend to have more crime. Each bubble is a country: its position left-to-right shows the unemployment rate, its height shows the Crime Index (higher means worse), its size shows population, and its colour also echoes the crime level (yellow = high, dark blue = low). At very low unemployment (below ~5 %), crime can be anywhere from low to high; between about 5 % and 15 % unemployment, crime averages a bit lower; but beyond roughly 15 % unemployment, crime rises sharply again. So there isn’t a simple straight correlation—plenty of countries manage moderate unemployment with only moderate crime—but extremely high unemployment is usually paired with high crime, and a handful of very low-unemployment nations still keep crime remarkably low.

How Crime Scores Spread Within Each Continent


This box-and-whisker chart compares how Crime Index scores are spread across the five inhabited continents. For each continent the colored box captures the middle half of countries (from the 25th to the 75th percentile), the dark line inside marks the median, and the dots show individual countries. Europe has the lowest typical crime levels—the box sits lower and its median is about 35—while the Americas and Oceania show the highest medians, around the mid-50s. Asia’s scores stretch widest, from very safe countries (near 20) to some much higher-crime ones (near 75). Africa also spans a broad range but clusters a bit above Asia’s median. Overall, crime risk varies a lot within every continent, yet Europe tends to be safer on average, whereas the Americas and Oceania lean toward higher crime scores.

Are richer countries safer?


This bubble-plot asks whether richer countries, measured by GDP per capita, tend to feel safer, measured by Crime Index. The downward grey trend line shows the broad answer: as national income rises, reported crime usually falls. Most low-income nations crowd the left side and many of them sit high on the crime scale, while wealthier countries to the right cluster lower.

Unemployment–Crime Patterns Differ by Continent


The crime–unemployment relationship isn’t one-size-fits-all.

Africa shows a sharp risk when unemployment soars.

The Americas and Asia hint at a “sweet-spot” mid-range where crime is highest.

Europe’s crime stays comparatively low regardless.

Oceania needs more data before drawing any conclusion.

So while unemployment can matter, regional factors—economic structure, social safety nets, policing, and inequality—shape how strongly it translates into crime.

Crime and Safety Averages—A Continental Contrast


This side-by-side bar chart compares the average Crime Index (red, higher = worse) with the average Safety Index (blue, higher = safer) for each continent. Europe stands out as the safest region: its blue bar is tallest—around 65—while its red crime bar is the shortest, a bit above 35. Asia also skews safe, with high safety (~60) and relatively low crime (~40). At the other end, the Americas and Oceania show the most worrying picture—both have crime averages near 55–60 and safety averages in the low- to mid-40s. Africa sits in the middle, with moderate-high crime (~50) and moderate safety (~45).

Higher Crime, Higher Birth Rates: How Fertility Shifts Across Crime-Risk Quartiles


Countries that struggle most with crime also tend to have higher fertility rates, while the safest quartile clusters around the lowest birth rates. The inverse is true as well: very low-birth-rate societies rarely register high crime in this dataset. That pattern hints at broader socioeconomic links—higher fertility often coincides with lower income levels, faster population growth, and weaker institutions, which collectively may contribute to higher perceived crime.

---
title: "Final Project"
output: 
  flexdashboard::flex_dashboard:
    storyboard: true
    social: menu
    source: embed
---

```{r setup, include=FALSE}
# 
library(flexdashboard)
library(plotly)
library(ggplot2)
library(ggthemes)
library(tidyverse)          
library(countrycode)        
library(janitor)            
library(viridis)            
library(ggridges)
library(patchwork)

# 
library(DBI)
library(duckdb)

knitr::opts_chunk$set(echo = TRUE)

#-------------
con <- dbConnect(
  duckdb::duckdb(),
  dbdir = "C:/Users/arian/Documents/crime_dashboard.duckdb",
  read_only = FALSE
)

#------------
if (!dbExistsTable(con, "crime_merged")) {

  # Read the two CSVs
  crime_raw  <- read_csv("C:/Users/arian/Desktop/crime-rate-by-country-2023.csv",
                         show_col_types = FALSE) %>% 
               clean_names()

  world_raw  <- read_csv("C:/Users/arian/Desktop/world-data-2023.csv",
                         show_col_types = FALSE) %>% 
               clean_names()

  #tables to DuckDB
  dbWriteTable(con, "crime", crime_raw, overwrite = TRUE)
  dbWriteTable(con, "world", world_raw, overwrite = TRUE)

  # Merge
crime  <- read_csv("C:/Users/arian/Desktop/crime-rate-by-country-2023.csv",
                   show_col_types = FALSE) %>% 
  clean_names() %>%                                
  mutate(
    crime_index   = as.numeric(crime_index),
    safety_index  = as.numeric(safety_index),
    population    = as.numeric(pop2023)            
  )

world  <- read_csv("C:/Users/arian/Desktop/world-data-2023.csv",
                   show_col_types = FALSE) %>% 
  clean_names() %>% 
  mutate(
    gdp                = parse_number(gdp),
    unemployment_rate  = parse_number(unemployment_rate),
    birth_rate         = parse_number(birth_rate),
    latitude           = as.numeric(latitude),
    longitude          = as.numeric(longitude)
  )
dbWriteTable(con, "crime",  crime,  overwrite = TRUE)
dbWriteTable(con, "world",  world,  overwrite = TRUE)
dbExecute(con, "
  CREATE OR REPLACE TABLE crime_merged AS
  SELECT  c.*,                -- everything from crime
          w.gdp,
          w.unemployment_rate,
          w.birth_rate,
          w.latitude,
          w.longitude
  FROM    crime  c
  LEFT JOIN world w USING (country);
")
}

# 3 · merged table into R  --------
crime <- tbl(con, "crime_merged") %>% 
  collect() %>% 
  mutate(
    gdp_pc   = gdp / population,
    continent = countrycode(country, "country.name", "continent")
  )

# 4 · Disconnect  -------
dbDisconnect(con, shutdown = TRUE)
```

### The World’s 15 Most Crime-Prone Countries

```{r echo=FALSE}
crime %>% 
  slice_max(crime_index, n = 15) %>% 
  ggplot(aes(fct_reorder(country, crime_index),
             crime_index, fill = crime_index)) +
  geom_col(width = .75, colour = "grey25") +
  coord_flip() +
  scale_fill_viridis_c(option = "magma", direction = -1,
                       name = "Crime\nIndex") +
  labs(title = "Top 15 Countries by Crime Index (Higher = Worse)",
       x = NULL, y = "Crime Index") +
  theme_minimal(base_size = 12)
```

***
The chart communicates a clear message: crime severity is heavily concentrated in a handful of Latin-American and Sub-Saharan countries, with conflict-zone outliers.
Venezuela stand out with the highest crime index

### Global Champions of Safety: Top-15 Nations


```{r echo=FALSE}
crime %>% 
  slice_max(safety_index, n = 15) %>% 
  ggplot(aes(fct_reorder(country, safety_index),
             safety_index, fill = safety_index)) +
  geom_col(width = .75, colour = "grey25") +
  coord_flip() +
  scale_fill_viridis_c(option = "plasma",
                       name = "Safety\nIndex") +
  labs(title = "Top 15 Countries by Safety Index (Higher = Safer)",
       x = NULL, y = "Safety Index") +
  theme_minimal(base_size = 12)

```

***
This chart lists the 15 safest countries in the dataset according to “Safety Index,” where bigger numbers mean people feel more secure and crime is low. The longer the bar, the higher the score; the colour echoes that score, going from deep blue-purple (still very safe) up through bright yellow for the very safest. Qatar tops the list with the longest, vivid-yellow bar, followed closely by the United Arab Emirates and Oman, showing a particularly strong showing for Gulf states. Switzerland, Japan, and Slovenia round out the upper tier, while Austria closes the list—but even its deep-blue bar sits around 75, which is well above the world average. Altogether, the chart highlights that these countries share a narrow, high range of scores (mid-70s to high-80s), signalling consistently low crime and strong public safety perceptions across diverse regions.

### Crime vs Safety: Perfect Mirror Relationship


```{r echo=FALSE}
ggplot(crime,
             aes(crime_index, safety_index,
                 size = population / 1e6,
                 colour = continent)) +
  geom_point(alpha = .8) +
  geom_smooth(method = "lm", se = FALSE,
              colour = "grey35", linewidth = .8) +
  scale_size_continuous(range = c(2, 12),
                        name = "Pop. (millions)") +
  scale_colour_viridis_d(option = "turbo",
                         name = "Continent",
                         na.value = "grey70") +
  labs(title = "Inverse Relationship Between Crime and Safety",
       x = "Crime Index (higher = worse)",
       y = "Safety Index (higher = safer)") +
  theme_minimal(base_size = 12) +
  theme(legend.box = "vertical")

```

***
This bubble-plot shows that countries with high Crime Index scores (worse crime) always have low Safety Index scores, and vice-versa. Each bubble’s color shows the continent and its size shows population, but those extra details don’t break the pattern—the dots from every region hug the same line, whether they’re tiny (small nations) or huge (China, India).

### Does Rising Unemployment Drive Up Crime?


```{r echo=FALSE}
ggplot(crime,
             aes(unemployment_rate, crime_index,
                 size = population / 1e6,
                 fill = crime_index)) +
  geom_point(shape = 21, colour = "grey30", alpha = .7) +
  geom_smooth(method = "loess", se = FALSE,
              colour = "firebrick", linewidth = 1) +
  scale_fill_viridis_c(option = "cividis",
                       name = "Crime\nIndex") +
  scale_size_continuous(range = c(2, 10),
                        name = "Pop. (millions)") +
  labs(title = "Do Higher Unemployment Rates Coincide with Higher Crime?",
       x = "Unemployment Rate (%)",
       y = "Crime Index") +
  theme_minimal(base_size = 12)

```

***
This scatter-plot looks at whether countries with higher unemployment also tend to have more crime. Each bubble is a country: its position left-to-right shows the unemployment rate, its height shows the Crime Index (higher means worse), its size shows population, and its colour also echoes the crime level (yellow = high, dark blue = low). At very low unemployment (below ~5 %), crime can be anywhere from low to high; between about 5 % and 15 % unemployment, crime averages a bit lower; but beyond roughly 15 % unemployment, crime rises sharply again. So there isn’t a simple straight correlation—plenty of countries manage moderate unemployment with only moderate crime—but extremely high unemployment is usually paired with high crime, and a handful of very low-unemployment nations still keep crime remarkably low.

### How Crime Scores Spread Within Each Continent


```{r echo=FALSE}
ggplot(crime,
             aes(continent, crime_index, fill = continent)) +
  geom_boxplot(width = .55, alpha = .65, outlier.shape = NA) +
  geom_jitter(width = .15, alpha = .6, size = 1.6,
              colour = "grey25") +
  scale_fill_viridis_d(option = "turbo", guide = "none") +
  labs(title = "Crime Index Variation Within Continents",
       x = NULL, y = "Crime Index") +
  theme_minimal(base_size = 12)

```

***
This box-and-whisker chart compares how Crime Index scores are spread across the five inhabited continents. For each continent the colored box captures the middle half of countries (from the 25th to the 75th percentile), the dark line inside marks the median, and the dots show individual countries. Europe has the lowest typical crime levels—the box sits lower and its median is about 35—while the Americas and Oceania show the highest medians, around the mid-50s. Asia’s scores stretch widest, from very safe countries (near 20) to some much higher-crime ones (near 75). Africa also spans a broad range but clusters a bit above Asia’s median. Overall, crime risk varies a lot within every continent, yet Europe tends to be safer on average, whereas the Americas and Oceania lean toward higher crime scores.

### Are richer countries safer?

```{r echo=FALSE}
ggplot(crime, 
             aes(gdp_pc, crime_index,
                 colour = continent,
                 size   = population / 1e6)) +  
  geom_point(alpha = 0.8) +
  scale_x_log10(labels = scales::label_dollar(scale = 1/1000, suffix = " k")) +
  scale_colour_viridis_d(option = "turbo", name = "Continent") +
  scale_size_continuous(range = c(2, 12), name = "Population (millions)") +
  labs(title = "Richer Countries Tend to Have Lower Crime Indices",
       x = "GDP per capita (log scale)",
       y = "Crime Index (higher = worse)") +
  theme_clean(base_size = 12) +
  theme(legend.box = "vertical")
```

***
This bubble-plot asks whether richer countries, measured by GDP per capita, tend to feel safer, measured by Crime Index. The downward grey trend line shows the broad answer: as national income rises, reported crime usually falls. Most low-income nations crowd the left side and many of them sit high on the crime scale, while wealthier countries to the right cluster lower.

### Unemployment–Crime Patterns Differ by Continent


```{r echo=FALSE}
ggplot(crime,
             aes(unemployment_rate, crime_index)) +
  geom_point(alpha = 0.7, colour = "#2c7fb8") +
  geom_smooth(method = "loess", se = FALSE, colour = "#e6550d") +
  facet_wrap(~ continent, scales = "free") +
  labs(title = "Crime vs Unemployment — Patterns Differ by Continent",
       x = "Unemployment Rate (%)",
       y = "Crime Index") +
  theme_bw(base_size = 11)
```

***
The crime–unemployment relationship isn’t one-size-fits-all.

Africa shows a sharp risk when unemployment soars.

The Americas and Asia hint at a “sweet-spot” mid-range where crime is highest.

Europe’s crime stays comparatively low regardless.

Oceania needs more data before drawing any conclusion.

So while unemployment can matter, regional factors—economic structure, social safety nets, policing, and inequality—shape how strongly it translates into crime.

### Crime and Safety Averages—A Continental Contrast


```{r echo=FALSE}
avg_cs <- crime %>% 
  group_by(continent) %>% 
  summarise(across(c(crime_index, safety_index), mean, na.rm = TRUE)) %>% 
  pivot_longer(-continent, names_to = "metric", values_to = "value")

ggplot(avg_cs,
             aes(continent, value, fill = metric)) +
  geom_col(position = position_dodge(width = 0.6), width = 0.55) +
  scale_fill_manual(values = c("#cb181d", "#2171b5"),
                    labels = c("Crime Index", "Safety Index"),
                    name = NULL) +
  labs(title = "Average Crime vs Safety Index by Continent",
       x = NULL, y = "Index value (0 – 100)") +
  theme_minimal(base_size = 12)
```

***
This side-by-side bar chart compares the average Crime Index (red, higher = worse) with the average Safety Index (blue, higher = safer) for each continent. Europe stands out as the safest region: its blue bar is tallest—around 65—while its red crime bar is the shortest, a bit above 35. Asia also skews safe, with high safety (~60) and relatively low crime (~40). At the other end, the Americas and Oceania show the most worrying picture—both have crime averages near 55–60 and safety averages in the low- to mid-40s. Africa sits in the middle, with moderate-high crime (~50) and moderate safety (~45).

### Higher Crime, Higher Birth Rates: How Fertility Shifts Across Crime-Risk Quartiles


```{r echo=FALSE}
crime_q <- crime %>% 
  mutate(crime_q = ntile(crime_index, 4) %>% factor(labels = paste0("Q", 1:4)))

ggplot(crime_q,
             aes(birth_rate, fill = crime_q)) +
  geom_histogram(position = "identity", alpha = .7, bins = 25, colour = "white") +
  scale_fill_viridis_d(option = "plasma",
                       name = "Crime Index Quartile") +
  labs(title = "How Birth Rates Vary Across Crime-Index Quartiles",
       x = "Birth rate (per 1,000 people)",
       y = "Number of countries") +
  theme_light(base_size = 12)
```

***
Countries that struggle most with crime also tend to have higher fertility rates, while the safest quartile clusters around the lowest birth rates. The inverse is true as well: very low-birth-rate societies rarely register high crime in this dataset. That pattern hints at broader socioeconomic links—higher fertility often coincides with lower income levels, faster population growth, and weaker institutions, which collectively may contribute to higher perceived crime.