#####Load any required packages.

knitr::opts_chunk$set(echo = TRUE, warnings = FALSE)
library(tidyverse)
## Warning: Paket 'ggplot2' wurde unter R Version 4.4.3 erstellt
## Warning: Paket 'forcats' wurde unter R Version 4.4.3 erstellt
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.1     ✔ stringr   1.5.1
## ✔ ggplot2   4.0.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ 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
library(plotly)
## Warning: Paket 'plotly' wurde unter R Version 4.4.3 erstellt
## 
## Attache Paket: 'plotly'
## 
## Das folgende Objekt ist maskiert 'package:ggplot2':
## 
##     last_plot
## 
## Das folgende Objekt ist maskiert 'package:stats':
## 
##     filter
## 
## Das folgende Objekt ist maskiert 'package:graphics':
## 
##     layout
library(ggplot2)
library(patchwork)
## Warning: Paket 'patchwork' wurde unter R Version 4.4.3 erstellt

Import Your Data

In the following code chunk my data is imported.

#### read csv files
datapath <- paste0(getwd(),'/data')
setwd(datapath)
anaemia_nonpreg <- read.csv("~/Doktorat/Vorlesungen/DashboardingR/Assignments/Course5/data/data_anaemia_nonpregnant_women.csv", sep=";", stringsAsFactors=TRUE)
anaemia_wra <- read.csv("~/Doktorat/Vorlesungen/DashboardingR/Assignments/Course5/data/data_anaemia_WRA.csv", sep=";", stringsAsFactors=TRUE)
anaemia_preg <- read.csv("~/Doktorat/Vorlesungen/DashboardingR/Assignments/Course5/data/data_anaemia_pregnant_women.csv", sep=";", stringsAsFactors=TRUE)
overweight <- read.csv("~/Doktorat/Vorlesungen/DashboardingR/Assignments/Course5/data/data_overweight.csv", sep=";", stringsAsFactors=TRUE)
underweight <- read.csv("~/Doktorat/Vorlesungen/DashboardingR/Assignments/Course5/data/data_underweight.csv", sep=";", stringsAsFactors=TRUE)
latestdat <- read.csv("~/Doktorat/Vorlesungen/DashboardingR/Assignments/Course5/data/latest_regional_global.csv", sep=";", stringsAsFactors=TRUE)


#### Make sure your data is converted into a tibble. 
anaemia_nonpreg <- as_tibble(anaemia_nonpreg)
anaemia_wra <- as_tibble(anaemia_wra)
anaemia_preg <- as_tibble(anaemia_preg)
overweight <- as_tibble(overweight)
underweight <- as_tibble(underweight)
latestdat <- as_tibble(latestdat)

Part 1

Description of the visualization and which variables/characteristics are used.

I have the following idea for the figure:

Now, the data is filtered and a subset of the data for the first visualization is created.

#filter data for all the countries
countries <- c("Morocco", "Finland", "Canada", "Ecuador", "New Zealand", "Japan")
overweight_sel <- overweight %>% filter(Countries.and.areas %in% countries)
underweight_sel <- underweight %>% filter(Countries.and.areas %in% countries)
#bind data for overweight and underweight of the selected countries into one dataset
dat1<-left_join(overweight_sel, underweight_sel, by = "ISO", suffix = c("_over", "_under"))
#filter only country names and values for point estimates
dat1 <- dat1 %>% select(Countries.and.areas_under, matches("_PE_"))
#assign year, variablename and value based on column name
dat1 <- pivot_longer(dat1, cols = -Countries.and.areas_under, names_to = c("year", "variable"), names_pattern = "^X(\\d+)_PE_(.*)$", values_to = "estimate") %>% 
  mutate(year = as.numeric(year))
fig_dat1 <- dat1 %>% filter(year == 2005) %>% rename(country = Countries.and.areas_under)
#remove datasets in between
rm("overweight_sel", "underweight_sel", "countries", "dat1")
#create factors for variable
fig_dat1$variable <- as.factor(fig_dat1$variable)
#rename levels that is is easier to plot
levels(fig_dat1$variable) <- c("overweight", "underweight")

In the following figure, the distribution of overweight and underweight women in a country from each continent will be presented as a stacked bar chart. As the figure would not be clearly understandable if data from 2000 to 2022 would be included, only data from 2005 is chosen. On the x-axis the countries will be displayed while on the y-axis the percentage of overweight and underweight women will be displayed.

p1 <- ggplot(fig_dat1, aes(x = country, y = estimate, fill = variable))+geom_bar(stat = "identity", position = "stack")+ xlab("Country") + ylab("Point Estimate of the Prevalance of Weight Status")+ ggtitle("Overweight and Underweight Women in selected Countries from each Continent")
p1

Part 2

Description of the visualization and which variables/characteristics are used.

I have the following idea for the figure:

Now, the data is filtered and a subset of the data for the second visualization is created.

gerunder <- underweight %>% filter(ISO == "DEU")
gerover <- overweight %>% filter(ISO == "DEU")
#bind data for overweight and underweight of the selected countries into one dataset
dat2<-left_join(gerover, gerunder, by = "ISO", suffix = c("_over", "_under"))
#filter values for point estimates
dat2 <- dat2 %>% select(matches("_PE_"))
#assign year, variablename and value based on column name
fig_dat2 <- pivot_longer(dat2, cols = everything(), names_to = c("year", "variable"), names_pattern = "^X(\\d+)_PE_(.*)$", values_to = "estimate") %>% 
  mutate(year = as.numeric(year))
#remove datasets in between
rm("gerover", "gerunder", "dat2")
#create factors for variable
fig_dat2$variable <- as.factor(fig_dat2$variable)
#rename levels that is is easier to plot
levels(fig_dat2$variable) <- c("overweight", "underweight")

In the following figure, the distribution of overweight and underweight women in Germany will be displayed from 2000 to 2022 with a line plot. As there is not much variability in the data, the plot will be made interactive with ggplotly to see the individual values. On the x-axis the years will be displayed while on the y-axis the percentage of overweight and underweight women will be displayed.

p2 <- ggplot(fig_dat2, aes(x = year, y = estimate, color = variable))+geom_line()+geom_point()+ xlab("Year") + ylab("Point Estimate of the Prevalance of Weight Status")+ ggtitle("Overweight and Underweight Women in Germany from 2000 to 2022")
ggplotly(p2)

Part 3

Description of the visualization and which variables/characteristics are used.

I have the following idea for the figure:

Now, the data is filtered and a subset of the data for the third visualization is created.

set.seed(456)
countrydat <- anaemia_preg %>% slice_sample(n = 10)
countrydat <- countrydat %>% select(Countries.and.areas, matches("_PE"))
#make pivot longer data to then split into years and select the appropriate ones
dat3 <- pivot_longer(countrydat, cols = -Countries.and.areas, names_to = "year", names_pattern = "^X(\\d+)_PE", values_to = "estimate") %>% mutate(year = as.numeric(year))
fig_dat3<-dat3 %>% filter(year == 2000 | year == 2010)
rm("countrydat", "dat3")
names(fig_dat3) <- c("Country", "Year", "Estimate")
fig_dat3$Year <- as.factor(fig_dat3$Year)

In the following figure, the anaemia in pregnant women from 2000 to 2010 from 10 randomly selected countries will be displayed with a dumbbell plot. On the x-axis the years will be displayed while on the y-axis the percentage of anaemia in pregnant women will be displayed.

#calculate difference in the years for each country
fig_dat3_wide <- fig_dat3 %>% pivot_wider(names_from = Year, values_from = c(Estimate))
fig_dat3_wide <- fig_dat3_wide %>% mutate(diff = `2010`-`2000`)
fig_dat3_wide <- fig_dat3_wide %>% mutate(max = pmax(`2000`, `2010`))
fig_dat3_long <- pivot_longer(fig_dat3_wide, cols = -c(Country, diff, max), names_to = "Year")
#plot to display values from 2000 and 2010
p3 <- ggplot(fig_dat3_long, aes(x = value, y = Country))+geom_line(aes(group= Country), color = "#E7E7E7", linewidth =3.5)+geom_point(aes(color = Year), size = 4)+
  #also add data and years in the legend
  geom_text(aes(label = Year, color = Year),size = 3.25, nudge_x =if_else(
    fig_dat3_long$value == fig_dat3_long$max,4, -1.5), hjust=if_else(fig_dat3_long$Year==fig_dat3_long$max,0,1),)+labs(x = "%", y = NULL)+coord_cartesian(ylim=c(1,10))+ xlab("Prevalance of Anaemia Status pf Pregnant Women") + ylab("Country")+ ggtitle("Prevalance of anaemic women during pregnancy of 10 randomly selected countries") + theme_minimal()+theme(legend.position = "none", axis.text.y = element_text(color = "black"), axis.text.x = element_text(color = "#989898"), axis.title = element_blank(), panel.grid= element_blank(), plot.title = element_text(size = 11))+scale_color_manual(values = c("#436685", "#BF2F24"))+scale_x_continuous(labels = scales::percent_format(scale = 1))

p3

Part 4

Description of the visualization and which variables/characteristics are used.

I have the following idea for the figure:

Now, the data is filtered and a subset of the data for the fourth visualization is created.

#make pivot longer data to then split into years and select the appropriate ones
dat4 <- pivot_longer(anaemia_wra, cols = -c(Countries.and.areas, World.Bank.Income.Groups, ISO, UNICEF.Regions, UN.Regions), names_to = "year", names_pattern = "^X(\\d+)_PE", values_to = "estimate") %>% 
  mutate(year = as.numeric(year))

fig_dat4<-dat4 %>% filter(year == 2022) %>% select(World.Bank.Income.Groups, estimate) 
fig_dat4 <- fig_dat4 %>% filter(World.Bank.Income.Groups != "Not Classified") %>% mutate(World.Bank.Income.Groups = fct_reorder(World.Bank.Income.Groups, estimate, .fun = 'median'))
####make sure you call the data so it will display in your report
rm("dat4")

In the following figure, the percentage of women with anaemia in the reproductive age from 2022 is shown. Moreover, the data will be grouped by the World Bank Income group and presented as a boxplot. On the x-axis the different World Bank Income Groups will be displayed while on the y-axis the prevalance of anaemia of women in the reproductive age will be displayed.

p4 <- ggplot(fig_dat4, aes(x = World.Bank.Income.Groups, y = estimate, fill = World.Bank.Income.Groups))+geom_boxplot()+ xlab("World Bank Income Group") + ylab("Prevalance of Anaemia in Women of the Reporductive Age")+ ggtitle("Prevalance of Anaemia in Women in the reproductive age in 2022") + theme_minimal()+theme(legend.position = "none")
p4

Part 5

Description of the visualization and which variables/characteristics are used.

I have the following idea for the figure:

Now, the data is filtered and a subset of the data for the fifth visualization is created.

#make pivot longer data to then split into years and select the appropriate ones
dat5 <- pivot_longer(underweight, cols = -c(Countries.and.areas, World.Bank.Income.Groups, ISO, UNICEF.Regions, UN.Regions), names_to = "year", names_pattern = "^X(\\d+)_PE", values_to = "estimate") %>% mutate(year = as.numeric(year))
fig_dat5<-dat5 %>% filter(year == 2020 & UN.Regions == "Europe") %>% select(Countries.and.areas, estimate)
rm("dat5")

# Geospatial data available at the geojson format, downloaded from
# "https://github.com/leakyMirror/map-of-europe/blob/27a335110674ae5b01a84d3501b227e661beea2b/GeoJSON/europe.geojson"
tmp_geojson <- read_file("C:/Users/miche/OneDrive/Documents/Doktorat/Vorlesungen/DashboardingR/Assignments/Course5/data/europe.geojson")

library(sf)
## Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE
my_sf <- read_sf(tmp_geojson)

#plot geodata to see if it is correct
plotv1 <- ggplot(my_sf) +
  geom_sf(fill = "white", color = "black", linewidth = 0.3) +
  theme_void()

##merge geodata and prevalance data
#transfer country names to character
fig_dat5$Countries.and.areas <- as.character(fig_dat5$Countries.and.areas)
complete_dat5 <- my_sf %>%
  left_join(fig_dat5, by = c("NAME" = "Countries.and.areas"))

In the following figure the prevalance of underweight women will be shown in a map of Europe.

p5 <- ggplot(complete_dat5) +
  geom_sf(aes(fill = estimate), linewidth = 0, alpha = 0.9) +
  theme_void() +
  scale_fill_viridis_c() +
  labs(
    title = "Prevalance of underweight women in Europe",
  ) +
   theme(
     text = element_text(color = "#22211d"),
     plot.background = element_rect(fill = "#f5f5f2", color = NA),
     panel.background = element_rect(fill = "#f5f5f2", color = NA),
     legend.background = element_rect(fill = "#f5f5f2", color = NA),
     plot.title = element_text(
       size = 11, hjust = 0.01, color = "#4e4d47",
       margin = margin(
         b = -0.1, t = 0.4, l = 2,
         unit = "cm"
       )
     ),
  )
p5

Part 6

Description of the visualization and which variables/characteristics are used.

I have the following idea for the figure:

Now, the data is filtered and a subset of the data for the sixth visualization is created.

#make pivot longer data to then split into years and select the appropriate ones
dat6 <- pivot_longer(anaemia_nonpreg, cols = -c(Countries.and.areas, World.Bank.Income.Groups, ISO, UNICEF.Regions, UN.Regions), names_to = "year", names_pattern = "^X(\\d+)_PE", values_to = "estimate") %>% mutate(year = as.numeric(year))
fig_dat6<-dat6 %>% filter(year == 2019) %>% select(estimate)
rm("dat6")

In the following figure, the distribution anaemic status of non-pregnant women in 2019 is displayed.

p6 <- ggplot(fig_dat6, aes(x = estimate))+geom_histogram(binwidth = 1)+ggtitle("Distribution of the worldwide prevalance of anaemia in non-pregnant women") + xlab("Point Estimate of Prevalance in 2019")+ylab("Count") + theme_minimal()
p6

Part 7

Description of the visualization and which variables/characteristics are used.

I have the following idea for the figure:

Now, the data is filtered and a subset of the data for the seventh visualization is created.

#make pivot longer data to then split into years and select the appropriate ones
dat7 <- pivot_longer(anaemia_preg, cols = -c(Countries.and.areas, World.Bank.Income.Groups, ISO, UNICEF.Regions, UN.Regions), names_to = "year", names_pattern = "^X(\\d+)_PE", values_to = "estimate") %>% mutate(year = as.numeric(year))
fig_dat7<-dat7 %>% filter(year == 2015 & World.Bank.Income.Groups == "Low Income") %>% select(Countries.and.areas, estimate)
####make sure you call the data so it will display in your report
rm("dat7")
fig_dat7
## # A tibble: 26 × 2
##    Countries.and.areas                   estimate
##    <fct>                                    <dbl>
##  1 Afghanistan                               32.9
##  2 Burkina Faso                              51.6
##  3 Burundi                                   39.1
##  4 Central African Republic                  48.3
##  5 Chad                                      53.3
##  6 Democratic People's Republic of Korea     32.6
##  7 Democratic Republic of the Congo          44.5
##  8 Eritrea                                   39.9
##  9 Ethiopia                                  25.1
## 10 Gambia                                    59.2
## # ℹ 16 more rows

In the following figure, the distribution of overweight and underweight women in Germany will be displayed from 2000 to 2022 with a line plot. As there is not much variability in the data, the plot will be made interactive with ggplotly to see the individual values. On the x-axis the years will be displayed while on the y-axis the percentage of overweight and underweight women will be displayed.

p7 <- ggplot(fig_dat7, aes(x = Countries.and.areas, y = estimate))+geom_col(position = "dodge")+ xlab("Country") + ylab("Prevalance of Anaemia")+ ggtitle("Anaemic status of low income countries in 2015") + theme_minimal()+theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
p7

Part 8

Description of the visualization and which variables/characteristics are used.

I have the following idea for the figure:

Now, the data is filtered and a subset of the data for the eigth visualization is created.

#make pivot longer data to then split into years and select the appropriate ones
dat8 <- pivot_longer(anaemia_nonpreg, cols = -c(Countries.and.areas, World.Bank.Income.Groups, ISO, UNICEF.Regions, UN.Regions), names_to = "year", names_pattern = "^X(\\d+)_PE", values_to = "estimate") %>% mutate(year = as.numeric(year))
fig_dat8<-dat8 %>% filter((year == 2019 | year == 2022) & World.Bank.Income.Groups == "High Income") %>% select(Countries.and.areas, year, World.Bank.Income.Groups,estimate)
# fig_dat8 <- fig_dat8 %>% rename(country = Countries.and.areas)
fig_dat8 <- fig_dat8 %>% mutate(country = fct_reorder(Countries.and.areas, estimate))
rm("dat8")

In the following figure, the anaemic status of non-pregnant women in high income countries between 2019 and 2022 will be displayed from 2019 to 2022 with a modified dumbbell chart. The figure is interactive to get a better overview. On the x-axis the years will be displayed while on the y-axis the anaemic status of nonpregnant women will be displayed. The figure will be grouped by country.