Skip to content

Instantly share code, notes, and snippets.

@jmclawson
Created April 6, 2024 13:48
Show Gist options
  • Save jmclawson/30d058f869da80b56afd66bd1efd57ca to your computer and use it in GitHub Desktop.
Save jmclawson/30d058f869da80b56afd66bd1efd57ca to your computer and use it in GitHub Desktop.
---
title: "Development Indicators by Continent"
author: "Gapminder Analytics Group"
format: dashboard
---
# Charts
```{r}
#| label: setup
#| include: false
library(ggplot2)
library(dplyr)
library(plotly)
library(gapminder)
```
## Row {height=60%}
```{r}
#| title: GDP and Life Expectancy
#| padding: 0
# Chart legends are reordered based on the order of items on the chart. To avoid the confusion of changed color assignments, set a palette by assigning names to hex colors and use the same palette for each chart. The palette here uses five colors from the Dark2 palette in Brewer. The fifth color is skipped because the stacked area chart would otherwise have two greens beside each other.
my_palette <-
RColorBrewer::brewer.pal(6, "Dark2")[c(1:4,6)] |>
setNames(c("Africa", "Americas", "Asia", "Europe", "Oceania"))
{gapminder |>
ggplot(aes(
group = country,
frame = year,
x = gdpPercap,
y = lifeExp,
text = paste0("<b>", country,"</b> (", year,")",
"</br></br></br>",
"GDP: ", scales::label_currency()(gdpPercap), " per cap</br>",
"life expectancy: ", round(lifeExp, 1), " years</br>",
"continent: ", continent, "</br>",
"population: ",
scales::label_comma()(pop))
)) +
geom_point(aes(
size = pop,
color = continent),
alpha = .4) +
facet_grid(cols = vars(continent)) +
labs(x = "GDP per capita",
y = "life expectancy") +
scale_x_log10(
labels = scales::label_currency(
scale_cut = scales::cut_short_scale())
) +
scale_size(range = c(1,20)) +
scale_color_manual(values = my_palette)} |>
ggplotly(tooltip = "text") |>
style(showlegend = FALSE) |>
config(displayModeBar = FALSE)
```
## Row {height=40%}
```{r}
#| title: Population
#| padding: 0
continent_pops <-
gapminder |>
summarize(
continent_pop = sum(pop, na.rm = TRUE),
.by = c(continent, year)) |>
arrange(continent_pop) |>
mutate(
continent = continent |>
forcats::fct_inorder(),
popup = glue::glue("In {year}, {if_else(!continent %in% c('Americas', 'Oceania'), 'the continent of ', if_else(continent == 'Americas', 'the ', ''))}<b>{continent}</b> had a population of {scales::label_comma(accuracy = 0.1, scale_cut = c(' million' = 1000000, ' billion' = 1000000000))(continent_pop)} people.")) |>
ungroup() |>
ggplot(aes(
x = year,
y = continent_pop,
color = continent,
fill = continent,
group = continent,
text = popup |> scales::label_wrap(35)()
)) +
geom_area(stat = "identity") +
labs(
x = NULL,
y = NULL) +
scale_y_continuous(
labels = scales::label_comma(
scale_cut = scales::cut_short_scale()
)) +
theme_minimal() +
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()
) +
scale_color_manual(values = my_palette) +
scale_fill_manual(values = my_palette) +
scale_x_continuous(
expand = expansion(0.01,0),
breaks = c(
min(gapminder$year),
1970, 1990,
max(gapminder$year)))
continent_pops |>
ggplotly(tooltip = "text") |>
config(displayModeBar = FALSE)
```
```{r}
#| title: Life Expectancy
#| padding: 0
{gapminder |>
mutate(
continent_mean = mean(lifeExp,
na.rm = TRUE),
.by = c(continent, year)) |>
mutate(
continent = forcats::fct_reorder2(
continent,
year,
continent_mean)) |>
ggplot(aes(
x = year,
y = lifeExp,
group = country,
color = continent,
text = paste0("<b>", country,"</b> (", year,")",
"</br></br></br>",
"life expectancy: ", round(lifeExp, 1), " years</br>",
"continent: ", continent, "</br>"))) +
geom_line() +
labs(
x = NULL,
y = NULL) +
theme_minimal() +
theme(panel.grid.major.x = element_blank()) +
scale_color_manual(values = my_palette) +
scale_x_continuous(
expand = expansion(0.01,0),
breaks = c(
min(gapminder$year),
1970, 1990,
max(gapminder$year)))} |>
ggplotly(tooltip = c("text")) |>
config(displayModeBar = FALSE)
```
# Data
## Row {height=80%}
```{r}
DT::datatable(gapminder)
```
## Row {height=20%}
```{r}
#| content: valuebox
#| title: "Population"
list(
icon = "people",
color = "primary",
value = gapminder |>
filter(year == max(year)) |>
pull(pop) |>
sum(na.rm = TRUE) |>
scales::label_comma(scale_cut = scales::cut_short_scale(), accuracy = 0.1)()
)
```
```{r}
#| content: valuebox
#| title: "Average life expectency"
list(
icon = "person-raised-hand",
color = "secondary",
value = gapminder |>
filter(year == max(year)) |>
pull(lifeExp) |>
mean(na.rm = TRUE) |>
round(1) |>
paste("years")
)
```
```{r}
#| content: valuebox
#| title: "Global economy"
list(
icon = "currency-exchange",
color = "success",
value = gapminder |>
filter(year == max(year)) |>
group_by(country) |>
summarize(gdp = gdpPercap * pop) |>
summarize(economy = sum(gdp, na.rm = TRUE)) |>
pull(economy) |>
scales::label_currency(
accuracy = 0.1,
scale_cut = scales::cut_short_scale())()
)
```
Summary values of global population, life expectancy, and economy were calculated from the year `{r} max(gapminder$year)`.
Data from the [`gapminder` package](https://jennybc.github.io/gapminder/). Dashboard example adapted for R from the Python [dashboard walkthrough](https://quarto.org/docs/dashboards/#walkthrough) found on the Posit website.
@jmclawson
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment