Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve gallery #9

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 50 additions & 7 deletions r/samples/dhs_examples_1/rdhs_simple_example.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,17 @@ <h2 class="anchored" data-anchor-id="setup">Setup</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">"rdhs"</span>, <span class="at">repos=</span><span class="st">"https://cloud.r-project.org"</span>)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">"dplyr"</span>, <span class="at">repos=</span><span class="st">"https://cloud.r-project.org"</span>)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">"ggplot2"</span>, <span class="at">repos=</span><span class="st">"https://cloud.r-project.org"</span>)</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">"haven"</span>, <span class="at">repos=</span><span class="st">"https://cloud.r-project.org"</span>)</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(rdhs)</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(haven)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">"tidyr"</span>, <span class="at">repos=</span><span class="st">"https://cloud.r-project.org"</span>)</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">"ggplot2"</span>, <span class="at">repos=</span><span class="st">"https://cloud.r-project.org"</span>)</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">"haven"</span>, <span class="at">repos=</span><span class="st">"https://cloud.r-project.org"</span>)</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">"stringr"</span>, <span class="at">repos=</span><span class="st">"https://cloud.r-project.org"</span>)</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(rdhs)</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(haven)</span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyr)</span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(stringr)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="search-for-relevant-indicators" class="level2">
Expand Down Expand Up @@ -246,6 +250,45 @@ <h2 class="anchored" data-anchor-id="work-with-micro-level-dataset">Work with Mi
</div>
</div>
</section>
<section id="combine-all-data-from-wide-columns" class="level2">
<h2 class="anchored" data-anchor-id="combine-all-data-from-wide-columns">Combine all data from wide columns</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="co"># get the recode mapping</span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>code_labels <span class="ot">&lt;-</span> <span class="fu">attributes</span>(ir<span class="sc">$</span>b6_01)<span class="sc">$</span>labels</span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a>code_labels <span class="ot">&lt;-</span> <span class="fu">setNames</span>(<span class="fu">names</span>(code_labels), code_labels)</span>
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a><span class="co"># recode the original data for all the b6_n columns</span></span>
<span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a>df <span class="ot">&lt;-</span> ir <span class="sc">%&gt;%</span></span>
<span id="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="fu">across</span>(<span class="fu">matches</span>(<span class="st">"^b6_</span><span class="sc">\\</span><span class="st">d+$"</span>),</span>
<span id="cb18-8"><a href="#cb18-8" aria-hidden="true" tabindex="-1"></a> <span class="sc">~</span> <span class="fu">factor</span>(., <span class="at">levels =</span> <span class="fu">names</span>(code_labels), <span class="at">labels =</span> code_labels)))</span>
<span id="cb18-9"><a href="#cb18-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-10"><a href="#cb18-10" aria-hidden="true" tabindex="-1"></a><span class="co"># make a long table from the wide data and summarize the counts of total labels</span></span>
<span id="cb18-11"><a href="#cb18-11" aria-hidden="true" tabindex="-1"></a>df_long <span class="ot">&lt;-</span> df <span class="sc">%&gt;%</span></span>
<span id="cb18-12"><a href="#cb18-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="at">cols =</span> <span class="fu">matches</span>(<span class="st">"^b6_</span><span class="sc">\\</span><span class="st">d+$"</span>), <span class="at">names_to =</span> <span class="st">"child_death"</span>, <span class="at">values_to =</span> <span class="st">"label"</span>)</span>
<span id="cb18-13"><a href="#cb18-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-14"><a href="#cb18-14" aria-hidden="true" tabindex="-1"></a><span class="co"># calculate the summary of child death</span></span>
<span id="cb18-15"><a href="#cb18-15" aria-hidden="true" tabindex="-1"></a>df_summary <span class="ot">&lt;-</span> df_long <span class="sc">%&gt;%</span></span>
<span id="cb18-16"><a href="#cb18-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(child_death, label) <span class="sc">%&gt;%</span> </span>
<span id="cb18-17"><a href="#cb18-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(<span class="sc">!</span><span class="fu">is.na</span>(label))</span>
<span id="cb18-18"><a href="#cb18-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-19"><a href="#cb18-19" aria-hidden="true" tabindex="-1"></a><span class="co"># create bar plot for child death</span></span>
<span id="cb18-20"><a href="#cb18-20" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(df_summary, <span class="fu">aes</span>(<span class="at">x =</span> label, <span class="at">y =</span> n, <span class="at">fill =</span> child_death)) <span class="sc">+</span></span>
<span id="cb18-21"><a href="#cb18-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">stat =</span> <span class="st">"identity"</span>) <span class="sc">+</span></span>
<span id="cb18-22"><a href="#cb18-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_discrete</span>(</span>
<span id="cb18-23"><a href="#cb18-23" aria-hidden="true" tabindex="-1"></a> <span class="at">name =</span> <span class="st">"Child Death"</span>,</span>
<span id="cb18-24"><a href="#cb18-24" aria-hidden="true" tabindex="-1"></a> <span class="at">labels =</span> <span class="cf">function</span>(x) <span class="fu">paste0</span>(<span class="fu">str_remove</span>(x, <span class="st">"b6_"</span>), <span class="st">"th child"</span>) </span>
<span id="cb18-25"><a href="#cb18-25" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb18-26"><a href="#cb18-26" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Label"</span>, <span class="at">y =</span> <span class="st">"Count"</span>) <span class="sc">+</span></span>
<span id="cb18-27"><a href="#cb18-27" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_minimal</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="rdhs_simple_example_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>

</main>
<!-- /main column -->
Expand Down
37 changes: 37 additions & 0 deletions r/samples/dhs_examples_1/rdhs_simple_example.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ date: "2024-11-14"
```{r results='hide', warning=FALSE, message=FALSE}
install.packages("rdhs", repos="https://cloud.r-project.org")
install.packages("dplyr", repos="https://cloud.r-project.org")
install.packages("tidyr", repos="https://cloud.r-project.org")
install.packages("ggplot2", repos="https://cloud.r-project.org")
install.packages("haven", repos="https://cloud.r-project.org")
install.packages("stringr", repos="https://cloud.r-project.org")

library(rdhs)
library(dplyr)
library(ggplot2)
library(haven)
library(tidyr)
library(stringr)
```

## Search for relevant indicators
Expand Down Expand Up @@ -96,3 +100,36 @@ datasets$path <- unlist(get_datasets(datasets$FileName))
summarise(count = n())

```

## Combine all data from wide columns
``` {r}
# get the recode mapping
code_labels <- attributes(ir$b6_01)$labels
code_labels <- setNames(names(code_labels), code_labels)

# recode the original data for all the b6_n columns
df <- ir %>%
mutate(across(matches("^b6_\\d+$"),
~ factor(., levels = names(code_labels), labels = code_labels)))

# make a long table from the wide data and summarize the counts of total labels
df_long <- df %>%
pivot_longer(cols = matches("^b6_\\d+$"), names_to = "child_death", values_to = "label")

# calculate the summary of child death
df_summary <- df_long %>%
count(child_death, label) %>%
filter(!is.na(label))

# create bar plot for child death
ggplot(df_summary, aes(x = label, y = n, fill = child_death)) +
geom_bar(stat = "identity") +
scale_fill_discrete(
name = "Child Death",
labels = function(x) paste0(str_remove(x, "b6_"), "th child")
) +
labs(x = "Label", y = "Count") +
theme_minimal()

```

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading