── 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.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.0
✔ 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
library(gganimate)theme_set(theme_minimal())
Import data
freedom <-read_csv(file ="data/freedom.csv",# NA values are recorded as '-'na ="-" )
Rows: 205 Columns: 103
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (35): country, status_1990, status_1991, status_1992, status_1993, statu...
dbl (68): pr_1990, cl_1990, pr_1991, cl_1991, pr_1992, cl_1992, pr_1993, cl_...
ℹ 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.
freedom
# A tibble: 205 × 103
country pr_1990 cl_1990 status_1990 pr_1991 cl_1991 status_1991 pr_1992
<chr> <dbl> <dbl> <chr> <dbl> <dbl> <chr> <dbl>
1 Afghanistan 7 7 NF 7 7 NF 6
2 Albania 7 6 NF 4 4 PF 4
3 Algeria 4 4 PF 4 4 PF 7
4 Andorra NA NA <NA> NA NA <NA> NA
5 Angola 7 7 NF 6 4 PF 6
6 Antigua and … 3 2 F 3 3 PF 3
7 Argentina 1 3 F 1 3 F 2
8 Armenia NA NA <NA> 5 5 PF 4
9 Australia 1 1 F 1 1 F 1
10 Austria 1 1 F 1 1 F 1
# ℹ 195 more rows
# ℹ 95 more variables: cl_1992 <dbl>, status_1992 <chr>, pr_1993 <dbl>,
# cl_1993 <dbl>, status_1993 <chr>, pr_1994 <dbl>, cl_1994 <dbl>,
# status_1994 <chr>, pr_1995 <dbl>, cl_1995 <dbl>, status_1995 <chr>,
# pr_1996 <dbl>, cl_1996 <dbl>, status_1996 <chr>, pr_1997 <dbl>,
# cl_1997 <dbl>, status_1997 <chr>, pr_1998 <dbl>, cl_1998 <dbl>,
# status_1998 <chr>, pr_1999 <dbl>, cl_1999 <dbl>, status_1999 <chr>, …
Data is in a wide format
pr_* - political rights, scores 1-7 (1 highest degree of freedom, 7 the lowest)
cl_* - civil liberties, scores 1-7 (1 highest degree of freedom, 7 the lowest)
status_* - freedom status (Free, Partly Free, and Not Free)
Calculate the top fifteen countries whose civil liberties scores have varied the most
freedom_to_plot <- freedom |># calculate rowwise standard deviations (one row per country)rowwise() |>mutate(sd =sd(c_across(contains("cl_")), na.rm =TRUE)) |>ungroup() |># find the 15 countries with the highest standard deviationsrelocate(country, sd) |>slice_max(order_by = sd, n =15) |># only keep countries with complete observations - necessary for future plottingdrop_na()freedom_to_plot
# calculate position rankings rather than raw scoresfreedom_ranked <- freedom_to_plot |># only keep columns with civil liberties scoresselect(country, contains("cl_")) |># wrangle the data to a long formatpivot_longer(cols =-country,names_to ="year",values_to ="civil_liberty",names_prefix ="cl_",names_transform =list(year = as.numeric) ) |># calculate rank within year - larger is worse, so reverse in the rankinggroup_by(year) |>mutate(rank_in_year =rank(-civil_liberty, ties.method ="first")) |>ungroup() |># highlight Venezuelamutate(is_venezuela =if_else(country =="Venezuela", TRUE, FALSE))freedom_ranked
# A tibble: 374 × 5
country year civil_liberty rank_in_year is_venezuela
<chr> <dbl> <dbl> <int> <lgl>
1 The Gambia 1990 2 11 FALSE
2 The Gambia 1991 2 11 FALSE
3 The Gambia 1992 2 11 FALSE
4 The Gambia 1993 2 11 FALSE
5 The Gambia 1994 6 2 FALSE
6 The Gambia 1995 6 2 FALSE
7 The Gambia 1996 6 2 FALSE
8 The Gambia 1997 6 2 FALSE
9 The Gambia 1998 5 2 FALSE
10 The Gambia 1999 5 2 FALSE
# ℹ 364 more rows
Faceted plot
freedom_faceted_plot <- freedom_ranked |># civil liberty vs freedom rankggplot(aes(x = civil_liberty, y =factor(rank_in_year), fill = is_venezuela)) +geom_col(show.legend =FALSE) +# change the color palette for emphasis of Venezuelascale_fill_manual(values =c("gray", "red")) +# facet by yearfacet_wrap(vars(year)) +# create explicit labels for civil liberties score,# leaving room for country text labelsscale_x_continuous(limits =c(-5, 7),breaks =1:7 ) +geom_text(hjust ="right",aes(label = country),x =-1 ) +# remove extraneous theme/label componentstheme(panel.grid.major.y =element_blank(),panel.grid.minor =element_blank(),axis.text.y =element_blank() ) +labs(x =NULL, y =NULL)freedom_faceted_plot
Animated plot
freedom_bar_race <- freedom_faceted_plot +# remove facetingfacet_null() +# label the current year in the top corner of the plotgeom_text(x =5, y =11,hjust ="left",aes(label =as.character(year)),size =10 ) +# define group structure for transitionsaes(group = country) +# temporal transition - ensure integer value for labelingtransition_time(as.integer(year)) +labs(title ="Civil liberties rating, {frame_time}",subtitle ="1: Highest degree of freedom - 7: Lowest degree of freedom" )# basic transitionanimate(freedom_bar_race, nframes =30, fps =2)