<p><a href="https://twitter.com/drsimonj">@drsimonj</a> here to introduce <a href="https://github.com/drsimonj/ourworldindata"><code>ourworldindata</code></a>: a new data package for R.</p>
<p>The <code>ourworldindata</code> package contains data frames that are generated by combining datasets from <a href="https://ourworldindata.org/">OurWorldInData.org</a>: “an online publication that shows how living conditions around the world are changing”. The data frames in this package have undergone tidying so that they are suited to quick analysis in R. The purpose of this package is to serve as a central R resource for these datasets so that they might be used for the likes of practice or exploratory data analysis in a replicable manner.</p>
<h2><a href="https://drsimonj.svbtle.com/#thanks-to-the-ourworldindata-team_2"> </a>Thanks to the OurWorldInData team</h2>
<p>Before discussing the package, I’d like to express my thanks to <a href="http://www.maxroser.com/about/">Max Roser</a> and the rest of the <a href="https://ourworldindata.org/about/">OurWorldInData team</a>, who collate the data sets that form the foundation of this package. If you appreciate their work and make use of this package, please consider <a href="https://ourworldindata.org/support/">supporting OurWorldInData</a>. Personal thanks to <a href="http://www.maxroser.com/about/">Max Roser</a> for also supporting the development of this package. Max tells me that OurWorldInData is planning to develop an API to allow access to their database, so keep an eye out for this! Still, I suspect that this package will come in handy for R users.</p>
<h2><a href="https://drsimonj.svbtle.com/#getting-started_2"> </a>Getting Started</h2>
<p><code>ourworldindata</code> is currently available as a development version, which can be installed from GitHub as follows:</p>
<pre><code>install.packages(&quot;devtools&quot;)  # Only needed if you don't have devtools installed
devtools::install_github(&quot;drimonj/ourworldindata&quot;)
</code></pre>
<p>Once installed, you can load the package and all available data frames with:</p>
<pre><code>library(ourworldindata)
</code></pre>
<p>A general help page is available by running the following command:</p>
<pre><code>?ourworldindata
</code></pre>
<p>Running this code will show you a list of the available data frames and links to their help pages.</p>
<h2><a href="https://drsimonj.svbtle.com/#what-data-frames-are-available_2"> </a>What data frames are available?</h2>
<p>The goal of the package is to start by providing a single and tidy data frame for each section of <a href="https://ourworldindata.org/">OurWorldInData.org</a>. As a starting point, two data frames are available in this package:</p>
<ul><li> <code>child_mortality</code> corresponding to the data available on the <a href="https://ourworldindata.org/child-mortality/">Child Mortality page</a>.</li>
<li> <code>financing_healthcare</code> corresponding to the data available on the <a href="https://ourworldindata.org/financing-healthcare/">Financing Healthcare page</a>.</li>
</ul><p>As the package develops, the intention is to generate a single data frame for each section of the website. Completing this will be a rather large task, so please feel free to get in touch if there’s a particular part you’d like added to the list.</p>
<h2><a href="https://drsimonj.svbtle.com/#how-to-use-a-data-frame_2"> </a>How to use a data frame</h2>
<p>After loading the package (via <code>library(ourworldindata)</code>), you can access any data frame by calling its name. For example:</p>
<pre><code>child_mortality
#> # A tibble: 44,926 × 10
#>     year     country continent population child_mort survival_per_woman
#>    &lt;int>       &lt;chr>     &lt;chr>      &lt;int>      &lt;dbl>              &lt;dbl>
#> 1   1957 Afghanistan      Asia    9147286     378.25           4.752952
#> 2   1958 Afghanistan      Asia    9314915     372.30           4.801279
#> 3   1959 Afghanistan      Asia    9489453     366.50           4.847305
#> 4   1960 Afghanistan      Asia    9671046     360.95           4.891030
#> 5   1961 Afghanistan      Asia    9859928     355.20           4.936288
#> 6   1962 Afghanistan      Asia   10056480     349.60           4.981547
#> 7   1963 Afghanistan      Asia   10261254     344.45           5.024505
#> 8   1964 Afghanistan      Asia   10474903     339.20           5.065161
#> 9   1965 Afghanistan      Asia   10697983     333.80           5.108119
#> 10  1966 Afghanistan      Asia   10927724     328.65           5.149542
#> # ... with 44,916 more rows, and 4 more variables: deaths_per_woman &lt;dbl>,
#> #   poverty &lt;dbl>, education &lt;dbl>, health_exp &lt;dbl>
</code></pre>
<p>We can see that <code>child_mortality</code> is a tibble (just like a data frame) with 44926 rows and 10 variables.</p>
<p>A data frame’s help page will describe the data and its variables. Running <code>?child_mortality</code> displays the help for this package that describes it as “Country-level changes in child mortality rates and related variables over time…”, followed by the variable list. For example, the variable <code>child_mort</code> is “Child Mortality (0-5 year-olds dying per 1,000 born)…”.</p>
<p>For best practice, it’s best to assign the data frame to another object before working with it. For example, if you want to add a variable such as child mortality as a proportion of the population size, it’s best to do it as follows:</p>
<pre><code>d &lt;- child_mortality
d$child_mort_prop &lt;- d$child_mort / d$population
</code></pre>
<p>To demonstrate the use of this data set, here we will create a similar graphic to the <a href="https://ourworldindata.org/child-mortality/#child-mortality-globally-post-1950">Child mortality by world region Figure</a> with some help from the dplyr and ggplot2 packages:</p>
<pre><code>library(dplyr)
library(ggplot2)

d &lt;- child_mortality
d %>% 
  filter(year >= 1960 &amp; !is.na(continent)) %>%
  group_by(continent, year) %>% 
  summarise(child_mort = mean(child_mort, na.rm = TRUE)) %>% 
  ggplot(aes(x = year, y = child_mort, color = continent)) +
    geom_line(size = 2) +
    theme_bw() +
    labs(
      title = &quot;Child mortality by world region&quot;,
      x = &quot;&quot;,
      y = &quot;Children dying before the age of 5 years (per 1,000 births)&quot;
    )
</code></pre>
<p><a href="https://svbtleusercontent.com/16ckkd15fwa2bw.png"></a><br>
Here’s an example using the <code>fanancial_healthcare</code> data that replicates the 2013 frame from the <a href="https://ourworldindata.org/financing-healthcare/#recent-trends-in-domestic-healthcare-expenditure-around-the-world">Healthcare expenditure as a percentage of GDP Figure</a>:</p>
<pre><code>d &lt;- financing_healthcare %>% 
  filter(year == 2013) %>% 
  select(country, health_exp_total, gdp) %>% 
  na.omit() %>% 
  mutate(exp_gdp = 100 * health_exp_total / gdp) %>% 
  rename(region = country) %>% 
  mutate(region = ifelse(region == &quot;United States&quot;, &quot;USA&quot;, region))

world_map &lt;-  map_data(map = &quot;world&quot;) %>% left_join(d)

ggplot(world_map) +
  geom_map(map = world_map, aes(map_id = region, x = long, y = lat, fill = exp_gdp)) +
  scale_fill_gradient(name = &quot;Healthcare expenditure\nas % of GDP&quot;,
                      low = &quot;palegreen1&quot;, high = &quot;navyblue&quot;, na.value = &quot;gray95&quot;) +
  coord_equal() +
  ggtitle(&quot;Total Healthcare Expenditure as Share (%) of National GDP by Country, 2013&quot;) +
  theme_void()
</code></pre>
<p><a href="https://svbtleusercontent.com/gd74kllhf1n3qq.png"></a></p>
<h2><a href="https://drsimonj.svbtle.com/#how-is-the-data-prepared_2"> </a>How is the data prepared?</h2>
<p>If you looked for these plots on <a href="https://ourworldindata.org/">OurWorldInData.org</a>, you would notice that there isn’t a perfect match between the graphs. This mismatch occurs for a couple of reasons. For example, on the global map, the country names are not correctly matched between <code>fanancial_healthcare</code> and <code>world_map</code> which contains the map coordinates. To demonstrate, I had to change occasions of “United States” to “USA”. However, this isn’t a major problem if we invest some time. What is important is that the data can be slightly different between this <code>ourworldindata</code> package and any single data source on <a href="https://ourworldindata.org/">OurWorldInData.org</a>, and this is a product of the process by which the data frame is prepared.</p>
<p>To create a particular data frame for this package (e.g., <code>financing_healthcare</code>), every data set available on the relevant web page is extracted and merged, and the variables are given tidy names. In some instances, the same variable (such as child mortality rate) is present in multiple data sets but with different values due to them being derived from different sources. In such instances, the variable that appears in the final data frame is computed as the mean of all available estimates. This is the main source of differences between a single data set from the website and the complete data frame in the package, and an important thing to be aware of if you’re using this package for reasons other than practice.</p>
<p>A final point on data preparation: If you want to know exactly how a data frame was prepared, the R script used to create it is available in the <a href="https://github.com/drsimonj/ourworldindata/tree/master/R/data_prep">data_prep folder on GitHub</a>. If you’d prefer to stick to values from a single source, for now, the best thing to do is download the specific data set from <a href="https://ourworldindata.org/">OurWorldInData.org</a> and merge it to the package data frame in R.</p>
<h2><a href="https://drsimonj.svbtle.com/#plans-for-the-future-and-support_2"> </a>Plans for the future and support</h2>
<p>Whenever I can, I’d like to continue creating tidy data frames from <a href="https://ourworldindata.org/">OurWorldInData.org</a> and adding them to this package for public use. I might also consider adding some functions if I think that there would be a common usage for them.</p>
<p>If you try this package, please don’t hesitate to get in touch with me with any sort of feedback. Similarly, if you’d like to contribute directly and be a part of developing the <code>ourworldindata</code> package, please check out the <a href="https://github.com/drsimonj/ourworldindata/">GitHub project</a> or get in touch with me directly.</p>
<h2><a href="https://drsimonj.svbtle.com/#sign-off_2"> </a>Sign off</h2>
<p>Thanks for reading and I hope this was useful for you.</p>
<p>For updates of recent blog posts, follow <a href="https://twitter.com/drsimonj">@drsimonj</a> on Twitter, or email me at <a href="mailto:drsimonjackson@gmail.com">drsimonjackson@gmail.com</a> to get in touch.</p>
<p>If you’d like the code that produced this blog, check out the <a href="https://github.com/drsimonj/blogR">blogR GitHub repository</a>.</p>