project4

Lia Salomon

Here is the original plot that I chose to replicate (https://ourworldindata.org/grapher/distribution-human-rights-index-vdem?time=latest) :

Load the data

I used data that shows the distribution of human rights index among countries in different continents. Higher values represent more rights.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
humanrights <- readr::read_csv("https://ourworldindata.org/grapher/distribution-human-rights-index-vdem.csv?v=1&csvType=filtered&useColumnShortNames=true&time=latest")
## New names:
## Rows: 298 Columns: 7
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (3): Entity, Code, owid_region dbl (4): Year,
## civ_libs_vdem__estimate_best__aggregate_method_average, time....
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `time` -> `time...6`
## • `time` -> `time...7`
glimpse(humanrights)
## Rows: 298
## Columns: 7
## $ Entity                                                 <chr> "Afghanistan", …
## $ Code                                                   <chr> "AFG", NA, "ALA…
## $ Year                                                   <dbl> 2023, 2023, 202…
## $ civ_libs_vdem__estimate_best__aggregate_method_average <dbl> 0.0530000, 0.59…
## $ owid_region                                            <chr> "Asia", NA, "Eu…
## $ time...6                                               <dbl> 2023, 2023, 202…
## $ time...7                                               <dbl> 2023, 2023, 202…

Load the data

We used data that shows the distribution of human rights index among countries in different continents. Higher values represent more rights.

library(tidyverse)

humanrights <- readr::read_csv("https://ourworldindata.org/grapher/distribution-human-rights-index-vdem.csv?v=1&csvType=filtered&useColumnShortNames=true&time=latest")
## New names:
## Rows: 298 Columns: 7
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (3): Entity, Code, owid_region dbl (4): Year,
## civ_libs_vdem__estimate_best__aggregate_method_average, time....
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `time` -> `time...6`
## • `time` -> `time...7`
glimpse(humanrights)
## Rows: 298
## Columns: 7
## $ Entity                                                 <chr> "Afghanistan", …
## $ Code                                                   <chr> "AFG", NA, "ALA…
## $ Year                                                   <dbl> 2023, 2023, 202…
## $ civ_libs_vdem__estimate_best__aggregate_method_average <dbl> 0.0530000, 0.59…
## $ owid_region                                            <chr> "Asia", NA, "Eu…
## $ time...6                                               <dbl> 2023, 2023, 202…
## $ time...7                                               <dbl> 2023, 2023, 202…

Interactive plot

library(ggplot2)
library(dplyr)
library(viridis)
## Loading required package: viridisLite
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
humanrights_filtered <- humanrights %>%
  filter(!is.na(civ_libs_vdem__estimate_best__aggregate_method_average), !is.na(owid_region))

humanrights_plot <- ggplot(humanrights_filtered, aes(
    y = reorder(Entity, -civ_libs_vdem__estimate_best__aggregate_method_average),
    x = civ_libs_vdem__estimate_best__aggregate_method_average,
    fill = civ_libs_vdem__estimate_best__aggregate_method_average,
    text = Entity  # Tooltip shows only country name
  )) +
  geom_col() +
  scale_x_continuous(breaks = seq(0, 1, 0.2), limits = c(0, 1)) +
  scale_fill_viridis_c(option = "mako", direction = 1, limits = c(0, 1), breaks = seq(0, 1, 0.2)) +
  labs(
    x = "Human Rights Index",
    y = "",
    fill = "Human Rights\nIndex",
    title = "Distribution of Human Rights Index, 2023"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    legend.position = "right"
  ) +
  facet_wrap(~owid_region, scales = "free_y")

humanrights_plotly <- ggplotly(humanrights_plot, tooltip = "text") %>%
  layout(dragmode = "zoom")

humanrights_plotly