-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
4,064 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# | ||
# This is a Shiny web application. You can run the application by clicking | ||
# the 'Run App' button above. | ||
# | ||
# Find out more about building applications with Shiny here: | ||
# | ||
# https://shiny.posit.co/ | ||
# | ||
|
||
library(shiny) | ||
library(tidyverse) | ||
library(maps) | ||
|
||
cia <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-10-22/cia_factbook.csv') |> | ||
mutate(iso_a3 = countrycode::countrycode( | ||
country, | ||
origin = "country.name", | ||
destination = "iso3c" | ||
)) |> | ||
drop_na(iso_a3) | ||
|
||
vars <- setdiff(names(cia), c("country", "iso_a3")) | ||
|
||
world_data <- map_data("world") |> # ggplot2 | ||
mutate(iso_a3 = countrycode::countrycode( | ||
region, | ||
origin = "country.name", | ||
destination = "iso3c" | ||
)) |> | ||
drop_na(iso_a3) | ||
|
||
data4map <- world_data |> | ||
left_join(cia, by = "iso_a3") | ||
|
||
# Define UI for application that draws a histogram | ||
ui <- fluidPage( | ||
|
||
pageWithSidebar( | ||
headerPanel('CIA Factbook map'), | ||
sidebarPanel( | ||
selectInput('xcol', 'color variable', vars), | ||
HTML('<p>The data come from <a href = "https://www.cia.gov/the-world-factbook/" target = "_blank"> CIA World Factbook </a> in 2014</p>'), | ||
HTML('<p>and were collated at <a href = "https://github.com/rfordatascience/tidytuesday/tree/master/data/2024/2024-10-22" target = "_blank">TidyTuesday 10/21/2024</a>.</p>'), | ||
HTML("<p>The <em>World Factbook</em> provides basic intelligence on the history, people, government, economy, energy, geography, environment, communications, transportation, military, terrorism, and transnational issues for 265 world entities.</p>") | ||
), | ||
|
||
# Show a plot of the generated distribution | ||
mainPanel( | ||
plotOutput("mapPlot") | ||
) | ||
) | ||
) | ||
|
||
# Define server logic required to draw a histogram | ||
server <- function(input, output) { | ||
|
||
output$mapPlot <- renderPlot({ | ||
|
||
data4map |> | ||
ggplot(aes(x = long, y = lat, group = group)) + | ||
geom_polygon(aes_string(fill = input$xcol)) + | ||
theme_void() + | ||
scale_fill_gradient(trans = "log") | ||
}) | ||
} | ||
|
||
# Run the application | ||
shinyApp(ui = ui, server = server) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
--- | ||
title: "CIA Factbook" | ||
author: "Jo Hardin" | ||
date: "10/22/2024" | ||
format: html | ||
execute: | ||
warning: false | ||
message: false | ||
--- | ||
|
||
```{r} | ||
library(tidyverse) # ggplot, lubridate, dplyr, stringr, readr... | ||
library(maps) | ||
library(praise) | ||
``` | ||
|
||
|
||
## The Data | ||
|
||
This week we're exploring the [CIA World Factbook](https://www.cia.gov/the-world-factbook/)! | ||
The dataset this week comes from the [CIA Factbook, Country Comparisons, 2014](https://www.cia.gov/the-world-factbook/references/guide-to-country-comparisons), | ||
via the [{openintro}](https://openintrostat.github.io/openintro/) R package, | ||
via the [{usdatasets}](https://cran.r-project.org/package=usdatasets) R package, | ||
via [this post on LinkedIn](https://www.linkedin.com/posts/andrescaceresrossi_rstats-rstudio-opensource-activity-7249513444830318592-r395). | ||
|
||
> The *World Factbook* provides basic intelligence on the history, people, government, | ||
> economy, energy, geography, environment, communications, transportation, military, | ||
> terrorism, and transnational issues for 265 world entities. | ||
```{r} | ||
cia <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-10-22/cia_factbook.csv') |> | ||
mutate(iso_a3 = countrycode::countrycode( | ||
country, | ||
origin = "country.name", | ||
destination = "iso3c" | ||
)) |> | ||
drop_na(iso_a3) | ||
``` | ||
|
||
|
||
## World Map | ||
|
||
```{r} | ||
world_data <- map_data("world") |> # ggplot2 | ||
mutate(iso_a3 = countrycode::countrycode( | ||
region, | ||
origin = "country.name", | ||
destination = "iso3c" | ||
)) |> | ||
drop_na(iso_a3) | ||
data4map <- world_data |> | ||
left_join(cia, by = "iso_a3") | ||
``` | ||
|
||
|
||
```{r} | ||
#| fig-cap: world map colored by the death rate (number of deaths per 1,000 people) in 2014. | ||
#| fig-alt: world map where each country is colored by the number of deaths per 1000 people in 2014. | ||
data4map |> | ||
ggplot(aes(x = long, y = lat, group = group)) + | ||
geom_polygon(aes(fill = death_rate), color = "black", size = 0.15) + | ||
theme_void() + | ||
scale_fill_gradient(trans = "log") | ||
#scale_fill_gradient(low = "blue", high = "purple") | ||
``` | ||
|
||
## Shiny App | ||
|
||
I also created a Shiny App which allows the user to change the variable that colors each country. However, I don't have an obvious place to host the Shiny App. The source code for the Shiny App is in `app.R` which is located at https://github.com/hardin47/TidyTuesday/tree/gh-pages/2024-10-22/CIAFactbook . | ||
|
||
|
||
|
||
```{r} | ||
praise() | ||
``` | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.