From 07ae5feada65168c7e1e3e84e64bf4f9b6742118 Mon Sep 17 00:00:00 2001 From: maaikewalraad <93885404+maaikewalraad@users.noreply.github.com> Date: Wed, 18 Jan 2023 16:48:25 +0100 Subject: [PATCH 1/5] Shiny app This is the Shiny app I published, [check it out here](https://mgomcb-maaikewalraad.shinyapps.io/assignment6b/) --- Practicals/Maaike/Shiny app week 6/server.R | 216 ++++++++++++++++++++ Practicals/Maaike/Shiny app week 6/ui.R | 116 +++++++++++ 2 files changed, 332 insertions(+) create mode 100644 Practicals/Maaike/Shiny app week 6/server.R create mode 100644 Practicals/Maaike/Shiny app week 6/ui.R diff --git a/Practicals/Maaike/Shiny app week 6/server.R b/Practicals/Maaike/Shiny app week 6/server.R new file mode 100644 index 0000000..56a9c81 --- /dev/null +++ b/Practicals/Maaike/Shiny app week 6/server.R @@ -0,0 +1,216 @@ +# +# This is the server logic of a Shiny web application. You can run the +# application by clicking 'Run App' above. +# +# Find out more about building applications with Shiny here: +# +# http://shiny.rstudio.com/ +# + +require(magrittr) # Special pipes +require(dplyr) # Data manipulation +require(data.table) # data manipulation +require(ggplot2) # Plots +require(readr) # Read csv +require(ggpubr) # Arranging plots +require(RColorBrewer) +library(eurostat) +library(tmap) +library(rjson) +library(RJSONIO) +library(sf) + +# Data prep + +# PLOT # +imgr <- get_eurostat("migr_imm8") +imgr %<>% filter(age == "TOTAL", sex == "T", agedef == "COMPLET") + +# Read region name with country codes +cc <- read_csv(url("https://raw.githubusercontent.com/datasets/country-codes/master/data/country-codes.csv")) + +# Some data adjustments +cc$reg <- cc$`Sub-region Name` # rename vars with lengthy names +cc$land <- cc$official_name_en +cc$land <- as.factor(cc$land) +cc$code <- cc$`ISO3166-1-Alpha-2` +# names(c)[names(cc) == "Sub-region Name"] <- "reg" +# cc <- select(cc, code, reg, land) +cc$reg[cc$code == "CY"] <- "Southern Europe" +cc$land[cc$code == "GB"] <- "Great Brittain" +cc$land[cc$code == "MK"] <- "North Macedonia" +imgr$geo[imgr$geo == "UK"] <- "GB" +imgr$geo[imgr$geo == "EL"] <- "GR" + +imgr_reg <- merge(imgr, cc, by.x = "geo", by.y = "code", all.x = T) +regions <- unique(imgr_reg$reg) + + +# MAP # + +europe <- read_sf("https://raw.githubusercontent.com/eurostat/Nuts2json/master/pub/v2/2021/3035/20M/0.json") + +# total +imgr2 <- get_eurostat("migr_imm8") # load again +imgr2 %<>% filter(age == "TOTAL", sex == "T", agedef == "COMPLET") +eu <- merge(imgr2, europe, by.x = "geo", by.y = "id", all.x = T) +eu$year <- format(as.Date(eu$time, format = "%d/%m/%Y"), "%Y") +eu <- st_as_sf(eu) + +# per capita +pop_tot <- get_eurostat("demo_gind") # obtain population totals +n <- pop_tot %>% + group_by("geo") %>% + filter(indic_de == "JAN") # on january 1st 2019 +names(n)[names(n) == "values"] <- "pop_size" # rename 'values' + +eu_n <- merge(eu, n, by = c("geo", "time"), all.x = T) # Re-use eu data created in 5c +eu_n$im_cap <- eu_n$values / eu_n$pop_size * 100 +eu_n <- st_as_sf(eu_n) +cols <- brewer.pal(n = 9, name = "GnBu")[c(4,6,8)] + +########################### THE ACTUAL APP ################################# + +# Define server +server <- function(input, output) { + + observe({ + + c <- input$selectcountry + r <- input$selectregion + + # creates plot time series + output$plot <- renderPlot({ + + if (r == "Not by region") { + + # this plot plots + + g1 <- filter(imgr_reg, land %in% c) %>% + ggplot(aes(x = time, y = values)) + + geom_line() + + ylab("Immigration count") + + facet_wrap(~ land) + + print(g1) + + } + + if (r == "By region") { + + # Western Europe + gg1 <- filter(imgr_reg, land %in% c, reg == regions[1]) %>% + ggplot(., aes(x = time, y = values , col = land)) + + geom_line() + + ylab("Immigration count") + + scale_color_manual(values = brewer.pal(n = length(unique(imgr_reg$geo[imgr_reg$reg == regions[1]])), "Set1")) + + # Eastern Europe + gg2 <- filter(imgr_reg, land %in% c, reg == regions[2]) %>% + ggplot(., aes(x = time, y = values , col = land)) + + geom_line() + + ylab("Immigration count") + + scale_color_manual(values = brewer.pal(n = length(unique(imgr_reg$geo[imgr_reg$reg == regions[2]])), "Set2")) + + # Southern Europe + gg3 <- filter(imgr_reg, land %in% c, reg == regions[3]) %>% + ggplot(., aes(x = time, y = values , col = land)) + + geom_line() + + ylab("Immigration count") + + scale_color_manual(values = brewer.pal(n = length(unique(imgr_reg$geo[imgr_reg$reg == regions[3]])), "Paired")) + + # Northern Europe + gg4 <- filter(imgr_reg, land %in% c, reg == regions[4]) %>% + ggplot(., aes(x = time, y = values , col = land)) + + geom_line() + + ylab("Immigration count") + + scale_color_manual(values = brewer.pal(n = length(unique(imgr_reg$geo[imgr_reg$reg == regions[4]])), "RdYlGn")) + + print(ggarrange(gg1, gg2, gg3, gg4, + labels = c(regions[1], regions[2], regions[3], regions[4]), + ncol = 2, nrow = 2)) + + } + + + }) + + }) + + + # Interpretation of the plot + + + output$txt1 <- renderText({ + + paste("Plots of immigration in Europe for years 1990-2020") + + }) + + + observe({ + + y <- input$selectyear + s <- input$selectstat + + # creates map + output$map <- renderPlot({ + + # totals + if (s == "Total number") { + + filter(eu, year == y) %>% + tm_shape(.) + + tm_fill("values", + title = "Total immigration", + palette = "PuRd") + + tm_layout("Immigration", + legend.title.size = 1, + legend.text.size = 0.6, + legend.position = c("left","top"), + legend.bg.color = "white", + legend.bg.alpha = 1) + + } + + if (s == "Per capita") { + + # per capita + + filter(eu_n, year == y) %>% + tm_shape(.) + + tm_fill("im_cap", + title = "Immigration per capita (in %)", + palette = cols) + + tm_layout("", + legend.title.size = 1, + legend.text.size = 0.6, + legend.position = c("left","top"), + legend.bg.color = "white", + legend.bg.alpha = 1) + + } + + + }) + + }) + + + # Interpretation of the map + + observe({ + + y <- input$selectyear + + output$txt2 <- renderText({ + + paste0("Map of immigration in Europe for year ", y) + + }) + + }) + + +} + diff --git a/Practicals/Maaike/Shiny app week 6/ui.R b/Practicals/Maaike/Shiny app week 6/ui.R new file mode 100644 index 0000000..324a962 --- /dev/null +++ b/Practicals/Maaike/Shiny app week 6/ui.R @@ -0,0 +1,116 @@ +# +# This is the user-interface definition of a Shiny web application. You can +# run the application by clicking 'Run App' above. +# +# Find out more about building applications with Shiny here: +# +# http://shiny.rstudio.com/ +# + +require(shiny) +require(tidyverse) +require(ggplot2) + + +shinyUI(fluidPage( + + # Application title + br(), + titlePanel(h1("Immigration in Europe", align = "center")), + + #Line break + br(), + + + + br(), + br(), + + # Sidebar with interactive elements + sidebarLayout( + sidebarPanel( + + # Time-series plot choices: + + helpText(strong("Time-series plot parameters"), style = "font-size:16px;"), + + selectizeInput( + inputId = "selectcountry", # selectedstat + label = "Select country:", + choices = c("Austria","Belgium","Bulgaria","Switzerland", "Cyprus","Czechia","Germany","Denmark", + "Estonia","Spain","Finland","France","Great Brittain","Greece","Croatia","Hungary", + "Ireland","Iceland", "Italy","Liechtenstein", "Lithuania","Luxembourg","Latvia","Montenegro", + "North Macedonia", "Malta","Netherlands","Norway","Poland","Portugal","Romania","Sweden", "Slovenia", "Slovakia"), + selected = c("Austria","Belgium","Bulgaria","Switzerland", "Cyprus","Czechia","Germany","Denmark", + "Estonia","Spain","Finland","France","Great Brittain","Greece","Croatia","Hungary", + "Ireland","Iceland", "Italy","Liechtenstein", "Lithuania","Luxembourg","Latvia","Montenegro", + "North Macedonia", "Malta","Netherlands","Norway","Poland","Portugal","Romania","Sweden", "Slovenia", "Slovakia"), + multiple = TRUE, + ), + + + selectizeInput( + inputId = "selectregion", + label = "View by region:", + choices = c("Not by region", "By region"), # + selected = "Not by region", # selected first + multiple = FALSE, # allows no multiple items to be selected + ), + + br(), + br(), + br(), + br(), + br(), + br(), + br(), + br(), + + helpText(strong("Map parameters"), style = "font-size:16px;"), + + selectizeInput( + inputId = "selectyear", + label = "Select year:", + choices = c(2010:2019), # choices are all different periods + selected = "2019", # selected first + multiple = FALSE, # allows no multiple items to be selected + options = list(maxItems = 1) # limits the selection to + ), + + selectizeInput( + inputId = "selectstat", + label = "Select statistic:", + choices = c("Total number", "Per capita"), # choices are all different periods + selected = "Per capita", # selected first + multiple = FALSE, # allows no multiple items to be selected + options = list(maxItems = 1) # limits the selection to + ), + + + ), + + + + mainPanel( + + + h4("Time-series plot"), # header directly above the plot + plotOutput("plot", width = "80%", height = 400), # the output for the plot + + span(textOutput("txt1"), style = "font-style:italic"), + + br(), + br(), + + + h4("Map of Europe"), # header directly above the plot + plotOutput("map", width = "80%", height = 400), # the output for the plot + + span(textOutput("txt2"), style = "font-style:italic"), + + br(), + + ) + +))) + \ No newline at end of file From eacc9b483574e9c8b6135d921c76be188eec4c8b Mon Sep 17 00:00:00 2001 From: maaikewalraad <93885404+maaikewalraad@users.noreply.github.com> Date: Wed, 18 Jan 2023 16:56:15 +0100 Subject: [PATCH 2/5] Link Shiny app --- Practicals/Maaike/Shiny app week 6/Link Shiny app.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Practicals/Maaike/Shiny app week 6/Link Shiny app.txt diff --git a/Practicals/Maaike/Shiny app week 6/Link Shiny app.txt b/Practicals/Maaike/Shiny app week 6/Link Shiny app.txt new file mode 100644 index 0000000..38b072b --- /dev/null +++ b/Practicals/Maaike/Shiny app week 6/Link Shiny app.txt @@ -0,0 +1 @@ +https://mgomcb-maaikewalraad.shinyapps.io/assignment6b/ \ No newline at end of file From 112432ad552d0a8139f58191ec00b179a8b7d833 Mon Sep 17 00:00:00 2001 From: maaikewalraad <93885404+maaikewalraad@users.noreply.github.com> Date: Thu, 19 Jan 2023 16:15:27 +0100 Subject: [PATCH 3/5] Markdown presentation exercise --- .../Exercise week 5/Presentation markup.Rmd | 194 ++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 Practicals/Maaike/Exercise week 5/Presentation markup.Rmd diff --git a/Practicals/Maaike/Exercise week 5/Presentation markup.Rmd b/Practicals/Maaike/Exercise week 5/Presentation markup.Rmd new file mode 100644 index 0000000..da933a5 --- /dev/null +++ b/Practicals/Maaike/Exercise week 5/Presentation markup.Rmd @@ -0,0 +1,194 @@ +--- +title: "Migration in Europe" +subtitle: "A visualization report" +author: "Maaike Walraad" +output: ioslides_presentation +widescreen: true +--- + + + + +```{r, include = F, message = F} +#library(plotly) +require(ggplot2) +require(magrittr) +require(dplyr) +require(DT) +require(plotly) +require(readr) +require(RColorBrewer) +library(eurostat) +library(tmap) +library(rjson) +library(RJSONIO) +library(sf) + + +# Requirements: +# a logo +# a centered still figure +# an interactive table +# a moving figure, interactive figure or movie +# a 2-column slide +# an aligned multi-row equation +# a citation and reference list +# r-code, displayed but not executed +# cached r-code (may overlap with the next requirement) +# r-code, executed, but not displayed (e.g. a figure generation) +``` + +```{r, echo=FALSE} +logo <- "/Users/maaikewalraad/Documents/UU.png" +``` + +## Immigration in Europe | A visualization report + +For this presentation, immigration data from [Eurostat](https://ec.europa.eu/eurostat/data/database) using the `eurostat` package. [Grid data](https://github.com/eurostat/Nuts2json) and [country codes](https://github.com/lukes/ISO-3166-Countries-with-Regional-Codes/blob/master/all/all.csv) are obtained from Github and analyzed with the use of the `sf` and `tmap` packages. + + + +## Estimation + +Population growth formula is given by [1] +$$P = P_{0} x e^{rt}, $$ + +where \ + $P$ = Total population after time "t" \ + $P_{0}$ = Starting population \ + $r$ = \% Rate of growth \ + $T$ = Time in hours or years \ + $e$ = Euhler's number (2.71828...) \ + + +## Europe and immigration [2] + +```{r, message = F, warning = F, echo = F} +europe <- read_sf("https://raw.githubusercontent.com/eurostat/Nuts2json/master/pub/v2/2021/3035/20M/0.json") + +imgr <- get_eurostat("migr_imm8") # obtain data +imgr %<>% filter(age == "TOTAL", sex == "T", agedef == "COMPLET") +imgr_20 <- imgr[imgr$time == "2020-01-01", ] +eu <- merge(imgr_20, europe, by.x = "geo", by.y = "id", all.x = T) +eu <- st_as_sf(eu) +``` + + +```{r, message = F, warning = F, fig.show = "hide"} +tm_shape(eu) + + tm_fill("values", + title = "Total immigration (2020)", + palette = "PuRd") + + tm_layout("Immigration", + legend.title.size = 1, + legend.text.size = 0.6, + legend.position = c("left","top"), + legend.bg.color = "white", + legend.bg.alpha = 1) +``` + +## Europe and immigration + +```{r, message = F, warning = F, echo = F, fig.align = "center"} +tm_shape(eu) + + tm_fill("values", + title = "Total immigration (2020)", + palette = "PuRd") + + tm_layout("Immigration", + legend.title.size = 1, + legend.text.size = 0.6, + legend.position = c("left","top"), + legend.bg.color = "white", + legend.bg.alpha = 1) +``` + + +## Immigration in Western Europe | A brief history + +```{r, message = F, warning = F, echo = F} +cc <- read_csv(url("https://raw.githubusercontent.com/datasets/country-codes/master/data/country-codes.csv")) + +cc %<>% select("ISO3166-1-Alpha-2", "Sub-region Name", "official_name_en") + +# Some data adjustments +cc$official_name_en[cc$`ISO3166-1-Alpha-2` == "GB"] <- "Great Brittain" +cc$official_name_en[cc$`ISO3166-1-Alpha-2` == "MK"] <- "North Macedonia" +imgr$geo[imgr$geo == "UK"] <- "GB" +imgr$geo[imgr$geo == "EL"] <- "GR" +cc$`Sub-region Name`[cc$`ISO3166-1-Alpha-2` == "CY"] <- "Southern Europe" +imgr_reg <- merge(imgr, cc, by.x = "geo", by.y = "ISO3166-1-Alpha-2", all.x = T) +``` + +```{r, message = F, warning = F, echo = F} +regions <- unique(imgr_reg$`Sub-region Name`) + +# Western Europe +gg_west <- ggplot(data = filter(imgr_reg, `Sub-region Name` == regions[1]), aes(x = time, y = values , col = official_name_en)) + + geom_line() + + ylab("Immigration count") + + labs(col = "Country") + + scale_color_manual(values = brewer.pal(n = length(unique(imgr_reg$geo[imgr_reg$`Sub-region Name` == regions[1]])), "Set1")) + +ggplotly(gg_west) +``` + + +## Slide Title {.columns-2 .smaller} + +>- So far we've seen the absolute immigration numbers + +>- It may be more useful to look at the immigration per capita + + + +>- **See the plot on the right for this** + +
+ +```{r, warning = F, message = F, echo = FALSE, out.width = "600"} +pop_tot <- get_eurostat("demo_gind") # obtain population totals +pop_tot %<>% filter(time == "2020-01-01") +n <- pop_tot %>% + group_by("geo") %>% + filter(indic_de == "JAN") # on january 1st 2019 +names(n)[names(n) == "values"] <- "pop_size" # rename 'values' + +eu_n <- merge(eu, n, by = "geo", all.x = T) # Re-use eu data created in 5c +eu_n$im_cap <- eu_n$values / eu_n$pop_size * 100 +eu_n <- st_as_sf(eu_n) + +cols <- brewer.pal(n = 9, name = "GnBu")[c(4,6,8)] + +tm_shape(eu_n) + + tm_fill("im_cap", + title = "Immigration per capita in % (2020)", + palette = cols) + + tm_layout("Immigration", + legend.title.size = 1, + legend.text.size = 0.6, + legend.position = c("left","top"), + legend.bg.color = "white", + legend.bg.alpha = 1) +``` + +## An interactive table + +```{r, message = F, warning = F, echo = F} +imgr_tab <- imgr_reg %>% filter(time == "2020-01-01", `Sub-region Name` == regions[1]) +imgr_tab %<>% select(official_name_en, time, values) +datatable(imgr_tab, options = list(pageLength = 5), caption = "This is the immigration in Western Europe on January 1st 2020.") +``` + +## References + +[1] *Background* - *NUTS* - *Nomenclature of territorial units for statistics* - *Eurostat*. [Online; accessed 19. Jan. 2023]. 2023. url: https://ec.europa. +eu/eurostat/web/nuts/background. \ +[2] *How do you calculate population growth? + Example.* [Online; accessed +19. Jan. 2023]. 2023. url: https://socratic.org/questions/how-do- +you-calculate-population-growth. +4 + + + From cbafebc0596384342ba5b508d350c6ca6d8e6caf Mon Sep 17 00:00:00 2001 From: maaikewalraad <93885404+maaikewalraad@users.noreply.github.com> Date: Thu, 19 Jan 2023 16:27:45 +0100 Subject: [PATCH 4/5] Update presentation markup --- Practicals/Maaike/Exercise week 5/Presentation markup.Rmd | 1 - 1 file changed, 1 deletion(-) diff --git a/Practicals/Maaike/Exercise week 5/Presentation markup.Rmd b/Practicals/Maaike/Exercise week 5/Presentation markup.Rmd index da933a5..89d8a8a 100644 --- a/Practicals/Maaike/Exercise week 5/Presentation markup.Rmd +++ b/Practicals/Maaike/Exercise week 5/Presentation markup.Rmd @@ -12,7 +12,6 @@ widescreen: true ```{r, include = F, message = F} -#library(plotly) require(ggplot2) require(magrittr) require(dplyr) From f04ed2a6f07e38e86f3d2ddbb7bd151138c71195 Mon Sep 17 00:00:00 2001 From: maaikewalraad <93885404+maaikewalraad@users.noreply.github.com> Date: Thu, 19 Jan 2023 16:30:46 +0100 Subject: [PATCH 5/5] Update markdown presentation --- Practicals/Maaike/Exercise week 5/Presentation markup.Rmd | 3 --- 1 file changed, 3 deletions(-) diff --git a/Practicals/Maaike/Exercise week 5/Presentation markup.Rmd b/Practicals/Maaike/Exercise week 5/Presentation markup.Rmd index 89d8a8a..b315975 100644 --- a/Practicals/Maaike/Exercise week 5/Presentation markup.Rmd +++ b/Practicals/Maaike/Exercise week 5/Presentation markup.Rmd @@ -39,9 +39,6 @@ library(sf) # r-code, executed, but not displayed (e.g. a figure generation) ``` -```{r, echo=FALSE} -logo <- "/Users/maaikewalraad/Documents/UU.png" -``` ## Immigration in Europe | A visualization report