knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE) knitr::opts_chunk$set(fig.width = 9, fig.height = 5)
This vignette demonstrates how to use the lapop package to reproduce figures style from the 2023 Pulse of Democracy report.
Attention: we are only using a subset from the data used in the actual report.
library(lapop)
The lapop package brings two datasets already with design effects (weighted).
# Single-country Single-year AmericasBarometer 2023 Brazil data(bra23) # AmericasBarometer 2006-2023 Brazil Merge data(cm23) # AmericasBarometer 2023 Year Merge data(ym23)
The AmericasBarometer use weight1500 for comparative datasets by time or country, wt weights are only for country specific datasets (single-country single-year). In the AmericasBarometer the primary sampling units (ids = upm) and stratification variable (strata = strata) to account for the survey’s complex design. Finally, PSUs are nested within strata, properly reflecting the survey’s hierarchical sampling structure. The lpr_data() function apply survey design effects.
# Single-Country Single-Year bra23w <- lpr_data(bra23, wt = TRUE) print(bra23w) # Single-Country Multi-Year cm23w <- lpr_data(cm23) print(cm23w) # Multi-Country Single-Year ym23w <- lpr_data(ym23) print(ym23w)
When you load lapop with library(lapop), the package now auto-registers the bundled LAPOP fonts and enables the standard plotting setup. You can still call lapop::lapop_fonts() manually if you want to re-run font registration in the current session.
lapop::lapop_fonts() # Re-run font registration explicitly if needed
fig1.1_data <- lpr_ts(cm23w, outcome = "ing4", rec = c(5, 7), use_wave = TRUE) lapop_ts(fig1.1_data, ymin = 50, ymax = 80, main_title = "Support for democracy decline a decade ago and remains comparatively low in Brazil", subtitle = "% who support democracy")
fig1.2_data <- lpr_cc(ym23w, outcome = "ing4", rec = c(5, 7)) lapop_cc(fig1.2_data, main_title = "In many countries, only about one in two adults support democracy", subtitle = "% who support democracy")
fig2.1_data <- lpr_mline(cm23w, outcome = c("b13", "b21", "b31"), rec = c(5, 7), rec2 = c(5, 7), rec3 = c(5, 7)) # Changing legend for readibility fig2.1_data$varlabel <- ifelse(fig2.1_data$varlabel == "b13", "National Legislature", ifelse(fig2.1_data$varlabel=="b21", "Political Parties", "Supreme Court")) lapop_mline(fig2.1_data, main_title = "Trust in executives has declined to a level similar to other \npolitical institutions", subtitle = "% who trust...")
fig2.3_data <- lpr_ccm(ym23w, outcome_vars = c("b12", "b18"), rec1 = c(5, 7), rec2 = c(5, 7)) # Changing legend for readibility fig2.3_data$var <- ifelse(fig2.3_data$var == "b12", "Armed Forces", "National Police") lapop_ccm(fig2.3_data, main_title = "The public typically reports more trust in the armed forces than the police,\nbut levels vary", subtitle = "% who trust...")
library(dplyr) ym23w$variables <- ym23w$variables %>% mutate( edrer = as.numeric(edre), gender = as.numeric(q1tc_r) ) %>% mutate(edrer = case_when( edrer <= 2 ~ "None/Primary", edrer %in% c(3, 4) ~ "Secondary", edrer > 4 ~ "Superior", TRUE ~ NA_character_ ), edrer = factor(edrer, levels = c("None/Primary", "Secondary", "Superior")), gender = ifelse(q1tc_r == 3, NA, q1tc_r)) %>% mutate(gender = case_when( gender == 1 ~ "Men", gender == 2 ~ "Women", TRUE ~ NA_character_ )) # Changing legend for readibility attributes(ym23w$variables$gender)$label <- "Gender" attributes(ym23w$variables$edrer)$label <- "Education" attributes(ym23w$variables$wealth)$label <- "Wealth" fig_mover <- lpr_mover(ym23w, outcome = "pn4", grouping_vars = c("gender", "edrer", "wealth"), rec = c(1, 2)) lapop_mover(fig_mover, main_title = "Satisfaction with democracy is significantly lower among women,\nthose with higher educational attainment, and the middle class", subtitle = "% who are satisfied with democracy")
spot32_data <- lpr_hist(ym23w, outcome = "vb21n") # Translating to English spot32_data$cat <- c("Vote", "Run for\noffice", "Protest", "Participate\nin local orgs.", "Other", "Change is\nimpossible") lapop_hist(spot32_data, main_title = "On average in the LAC region, one in three say voting is the best way \nto influence change", subtitle = "How do you believe you can best influence change?")
fig3.5_data <- lpr_dumb(ym23w, outcome = "q14f", rec = c(1, 1), over = c(2018, 2023), xvar = "pais", ttest = TRUE) # Select Countries fig3.5_data <- fig3.5_data[c(1:3,5:6), ] lapop_dumb(fig3.5_data, main_title = "Among those with emigration intentions, the percentage who say they are \nvery likely to emigrate increased in Nicaragua and Guatemala", subtitle = "% who say it is very likely they will emigrate", source = "LAPOP Lab, AmericasBarometer 2018/19 and 2023")
attributes(ym23w$variables$q14f)$label <- "Migration intentions" fig3.8_data <- lpr_stack(ym23w, outcome = "q14f", xvar = "pais", order = "hi-lo") lapop_stack(fig3.8_data, xvar = "xvar_label", source = "LAPOP Lab, AmericasBarometer 2023", main_title = "Nicaragua has the highest percentage of individuals with migration intentions,\nwhile Haiti has the lowest")
According to the LAPOP Editorial Guidelines, when variables are measured on a scale, reversing them can enhance clarity and coherence in data representation. For instance, if a survey question aims to measure positive sentiment but higher numeric values indicate negative responses, reversing the variable aligns the coding with the expected direction of responses. This makes it easier for readers to understand the findings.
# For data.frames cm23$ing4r <- lpr_resc(cm23$ing4, reverse = TRUE, map=TRUE) # For LPR_DATA() objects cm23w$variables$ing4r <- lpr_resc(cm23w$variables$ing4, reverse = TRUE, map=TRUE)
This vignette is intended as a template and starting point for your own reports and exploratory visualizations using LAPOP data.
By adapting these examples, you can produce consistent, high-quality charts that follow the LAPOP Lab’s editorial guidelines and communicate survey results effectively.
For further guidance:
?lpr_cc, ?lapop_cc).If you encounter any issues or have suggestions for improvement, please let us know via our GitHub issues page.
Thank you for using the LAPOP R package to help make high-quality, reproducible social science research easier and more accessible!
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.