Skip to content

Commit

Permalink
cia factbook
Browse files Browse the repository at this point in the history
  • Loading branch information
hardin47 committed Oct 23, 2024
1 parent 13c5bd3 commit c812dc2
Show file tree
Hide file tree
Showing 19 changed files with 4,064 additions and 4 deletions.
6 changes: 3 additions & 3 deletions 2024-10-08/species.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<meta name="author" content="Jo Hardin">
<meta name="dcterms.date" content="2024-10-08">

<title>National Park Speciees</title>
<title>National Park Species</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
Expand Down Expand Up @@ -81,7 +81,7 @@

<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">National Park Speciees</h1>
<h1 class="title">National Park Species</h1>
</div>


Expand Down Expand Up @@ -253,7 +253,7 @@ <h2 class="anchored" data-anchor-id="the-data">The Data</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">praise</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "You are magnificent!"</code></pre>
<pre><code>[1] "You are superior!"</code></pre>
</div>
</div>
</section>
Expand Down
2 changes: 1 addition & 1 deletion 2024-10-08/species.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "National Park Speciees"
title: "National Park Species"
author: "Jo Hardin"
date: "10/08/2024"
format: html
Expand Down
68 changes: 68 additions & 0 deletions 2024-10-22/CIAFactbook/app.R
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)
592 changes: 592 additions & 0 deletions 2024-10-22/ciafact.html

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions 2024-10-22/ciafact.qmd
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.
Loading

0 comments on commit c812dc2

Please sign in to comment.