Lecture 18
Cornell University
INFO 3312/5312 - Spring 2024
March 28, 2024
Single plots with Plotly
Dashboards with Quarto
Interactive web applications with Shiny
ggplotly()
in plotly converts ggplot2 plots to plotly plots# get World Bank indicators
indicators <- c(
population = "SP.POP.TOTL",
prop_women_parl = "SG.GEN.PARL.ZS",
gdp_per_cap = "NY.GDP.PCAP.KD"
)
wdi_parl_raw <- WDI(
country = "all", indicators, extra = TRUE,
start = 2022, end = 2022
)
# keep actual "economies"
wdi_clean <- wdi_parl_raw |>
filter(region != "Aggregates")
glimpse(wdi_clean)
Rows: 215
Columns: 15
$ country <chr> "Afghanistan", "Albania", "Algeria…
$ iso2c <chr> "AF", "AL", "DZ", "AS", "AD", "AO"…
$ iso3c <chr> "AFG", "ALB", "DZA", "ASM", "AND",…
$ year <int> 2022, 2022, 2022, 2022, 2022, 2022…
$ status <chr> "", "", "", "", "", "", "", "", ""…
$ lastupdated <chr> "2024-02-21", "2024-02-21", "2024-…
$ population <dbl> 41128771, 2777689, 44903225, 44273…
$ prop_women_parl <dbl> NA, 35.714286, 8.108108, NA, 46.42…
$ gdp_per_cap <dbl> NA, 5155.291, 3999.758, 14969.060,…
$ region <chr> "South Asia", "Europe & Central As…
$ capital <chr> "Kabul", "Tirane", "Algiers", "Pag…
$ longitude <chr> "69.1761", "19.8172", "3.05097", "…
$ latitude <chr> "34.5228", "41.3317", "36.7397", "…
$ income <chr> "Low income", "Upper middle income…
$ lending <chr> "IDA", "IBRD", "IBRD", "Not classi…
wdi_2022 <- wdi_clean |>
filter(year == 2022) |>
drop_na(prop_women_parl) |>
# Scale this down from 0-100 to 0-1 so that scales::label_percent() can format
# it as an actual percent
mutate(prop_women_parl = prop_women_parl / 100)
static_plot <- ggplot(
data = wdi_2022,
mapping = aes(y = fct_rev(region), x = prop_women_parl, color = region)
) +
geom_quasirandom() +
scale_x_continuous(labels = label_percent()) +
scale_color_discrete_qualitative(guide = "none") +
labs(x = "% women in parliament", y = NULL, caption = "Source: The World Bank") +
theme_bw(base_size = 14)
static_plot
theme(legend.position = "none")
)static_plot_tooltip <- ggplot(
data = wdi_2022,
mapping = aes(y = fct_rev(region), x = prop_women_parl, color = region)
) +
geom_quasirandom(
mapping = aes(text = country)
) +
scale_x_continuous(labels = label_percent()) +
scale_color_discrete_qualitative() +
labs(x = "% women in parliament", y = NULL, caption = "Source: The World Bank") +
theme_bw(base_size = 14) +
theme(legend.position = "none")
Creating custom tooltips with the format
Generate a new column using mutate()
with the required character string
str_glue()
to combine character strings with data values<br>
is HTML code for a line breaklabel_percent()
function to format numbers as percents country prop_women_parl fancy_label
1 Albania 0.35714286 Albania<br>35.7% women in parliament
2 Algeria 0.08108108 Algeria<br>8.1% women in parliament
3 Andorra 0.46428571 Andorra<br>46.4% women in parliament
4 Angola 0.33636364 Angola<br>33.6% women in parliament
5 Antigua and Barbuda 0.11111111 Antigua and Barbuda<br>11.1% women in parliament
6 Argentina 0.44747082 Argentina<br>44.7% women in parliament
static_plot_tooltip_fancy <- ggplot(
data = wdi_2022,
mapping = aes(y = fct_rev(region), x = prop_women_parl, color = region)
) +
geom_quasirandom(
mapping = aes(text = fancy_label)
) +
scale_x_continuous(labels = label_percent()) +
scale_color_discrete_qualitative() +
labs(x = "% women in parliament", y = NULL, caption = "Source: The World Bank") +
theme_bw(base_size = 14) +
theme(legend.position = "none")
ggplotly(static_plot_tooltip_fancy,
tooltip = "text"
)
ae-15
ae-15
(repo name will be suffixed with your GitHub name).ggplotly()
to convert ggplot2 plots to an interactive form