Chapter 5 Comparative Demographic Graphs

This chapter will cover how to make a comparative frequency graph by demographic for multiple countries.

5.1 Create a Single Graph

This section will go over how to make a one-off comparative frequency graph by demographic for a multiple countries.

At the end, your code will look like the following:

survey1 %>%
  calculate_demographic_smry_comp("Q1COVID19",
                                  .dem = "gender") %>%
  plot_demographic_smry_comp(.caption = "Arab Barometer Wave VI, Survey I")

That code will produce the following graph:

## Warning: `position_dodge()` requires non-overlapping x intervals.

Let’s make it.

5.1.1 Creating a Summary

The first step in creating a plot is to gather the data you want to display and organize it. You do this with the calculate_demographic_smry_comp() function.

Just like calculate_smry_comp(), the three main parameters you need to provide to this function are (1) the data you are using, (2) the variable you want to plot, and (3) the demographic you want to organize the variable by. To see all the input parameters for the function, type the code ?calculate_demographic_smry_comp in your R console.

In this example, the variable we want to plot is Q1COVID19 and the demographic is "gender"4.

survey1 %>%                                       # Data
  calculate_demographic_smry_comp("Q1COVID19",    # Variable to summarize  
                                  .dem = "gender" # Demographic
                                  )  

The above code will produce the following output:

## # A tibble: 6 × 3
## # Groups:   Country [3]
##   Country gender Q1COVID19
##   <chr>   <fct>      <dbl>
## 1 Algeria Female        77
## 2 Algeria Male          66
## 3 Lebanon Female        87
## 4 Lebanon Male          77
## 5 Morocco Female        75
## 6 Morocco Male          56

Let’s save our gender demographic summary and move on to plotting it.

comparative_Q1COVID19_gender <- survey1 %>%
  calculate_demographic_smry_comp("Q1COVID19",
                                  .dem = "gender")

5.1.2 Plotting the Summary

The next step is plot the summary we just created. To do this, we use the function plot_demographic_smry_comp().

There is only one necessary parameter to use plot_smry_comp(): the summary data frame. For a complete list of acceptable parameters and documentation, you can run ?plot_demographic_smry_comp in your R console.

Now, we can plug our summary into the plot function:

plot_demographic_smry_comp(comparative_Q1COVID19_gender)

The above code is the same as:

comparative_Q1COVID19_gender %>%
  plot_demographic_smry_comp()

Both stylings of code produce the following graph:

## Warning: `position_dodge()` requires non-overlapping x intervals.

Yet again, we see the caption needs to be changed. We can change it the same way we did in Chapter 1.

comparative_Q1COVID19_gender %>%
  plot_demographic_smry_comp(
    .caption = "Arab Barometer Wave VI, Survey I"
  )

Now we have the graph:

## Warning: `position_dodge()` requires non-overlapping x intervals.

Since comparative_Q1COVID19_gender is the same as survey1 %>% calculate_demographic_smry_comp("Q1COVID19", .dem = "gender"), we can substitute the former with the latter to get the original code in our example. This will give us the same plot that we just created.

survey1 %>%
  calculate_demographic_smry_comp("Q1COVID19",
                                  .dem = "gender",) %>%
  plot_demographic_smry_comp(.caption = "Arab Barometer Wave VI, Survey I")

Tada! You have created the example graph.

5.2 Create Many Graphs

We will follow the same steps from Chapter Three (and Two, and One…) to create many graphs at once.

  1. First, identify the variables to plot.
  2. Second, create summaries of those variables.
  3. Third, plot those summaries.

At the end of this section, your code will look like the following:

     #.....................Identify the variables.....................
variables_2_plot <- list("Q1COVID19",
                         "Q2061A_1",
                         "Q609")

names(variables_2_plot) <- c("Q1COVID19",
                             "Q2061A_1",
                             "Q609")

     #......................Create the summaries......................
comparative_gender_summaries <- survey1 %>%
  calculate_demographic_smry_comp(variables_2_plot,
                                  .dem = "gender") 

     #.......................Plot the summaries.......................
comparative_gender_plots <- map(comparative_gender_summaries,
                                plot_demographic_smry_comp,
                                .caption = "Arab Barometer Wave VI, Survey I")

The result is a named list of plots. Each element in the list is a plot. The element is named for the variable it is a plot of.

For example, to see the plot for variable Q1COVID19, run the following code:

comparative_gender_plots$Q1COVID19
## Warning: `position_dodge()` requires non-overlapping x intervals.

To see the plot for variable Q2061A_1, run the following code:

comparative_gender_plots$Q2061A_1
## Warning: `position_dodge()` requires non-overlapping x intervals.

Finally, to see the plot for variable Q609, run the following code:

comparative_gender_plots$Q609
## Warning: `position_dodge()` requires non-overlapping x intervals.

That’s it! The only limit on the number of graphs you can create at once is the time it will take R to make them. The more graphs you try to create at once, the longer it will take.

The steps to creating a named variable list are exactly the same as the ones we reviewed in Chapter 1; therefore, we will begin by creating summaries. For a refresher on creating named lists, click here. We will use the same variables as in Chapter 3: Q1COVID19, Q2061A_1, and Q609.

Let’s begin.

5.2.1 Create Summaries

The same function used to create a single summary is used to create many summaries: calculate_demographic_smry_comp(). Recall it takes three parameters: (1) the data you are using, (2) the variable(s) you want to plot, and (3) the demographic you are creating the plot for. Now, instead of one variable, you supply the list of variables.

survey1 %>%                                         # Data
  calculate_demographic_smry_comp(variables_2_plot, # Variables to summarize
                                  .dem = "gender"   # Demographic
                                  )  

The above code produces a named list. Each element in the list is a data frame. You can think of it like the following bullet point list:

  • calculate_demographic_smry_comp output
    • Q1COVID19
      • Q1COVID19 summary grouped by gender
    • Q2061A_1
      • Q2061A_1 summary grouped by gender
    • Q609
      • Q609 summary grouped by gender

Let’s save this outcome as an object and move on to plotting.

comparative_gender_summaries <- survey1 %>%                                               
  calculate_demographic_smry_comp(variables_2_plot,
                                  .dem = "gender")  

5.2.2 Plot the Summaries

Again, the same function to create one plot is used to create many plots: plot_demographic_smry_comp(). Just like in Chapter 3, we need to use the map() function from the purrr package.

In this case, you supply the list of summaries you just created, and the plot_demographic_sumry_comp() function. The code follows:

map(
  comparative_gender_summaries,   # List of summaries
  plot_demographic_smry_comp      # Plotting function
)

The code produces the following output:

map(
  comparative_gender_summaries,     # List of summaries
  plot_demographic_smry_comp        # Plotting function
)
## $Q1COVID19
## Warning: `position_dodge()` requires non-overlapping x intervals.
## 
## $Q2061A_1
## Warning: `position_dodge()` requires non-overlapping x intervals.
## 
## $Q609
## Warning: `position_dodge()` requires non-overlapping x intervals.

Notice, yet again, the caption needs to be changed. To change the caption for all the graphs, just add one line to the map function.

map(
  comparative_gender_summaries,                  # List of summaries
  plot_demographic_smry_comp,                    # Plotting function
  .caption = "Arab Barometer Wave VI, Survey I"  # Caption
)
## $Q1COVID19
## Warning: `position_dodge()` requires non-overlapping x intervals.
## 
## $Q2061A_1
## Warning: `position_dodge()` requires non-overlapping x intervals.
## 
## $Q609
## Warning: `position_dodge()` requires non-overlapping x intervals.

Congratulations! You have created three plots at once. You can store them in as a single list and call them one at a time.

comparative_gender_plots <- map(
  comparative_gender_summaries,                  # List of summaries
  plot_demographic_smry_comp,                    # Plotting function
  .caption = "Arab Barometer Wave VI, Survey I"  # Caption
)

Now, all three plots have been stored in a named list named comparative_gender_plots. To look at the first plot:

comparative_gender_plots$Q1COVID19
## Warning: `position_dodge()` requires non-overlapping x intervals.

To see the plot for variable Q2061A_1, run the following code:

comparative_gender_plots$Q2061A_1
## Warning: `position_dodge()` requires non-overlapping x intervals.

Finally, to see the plot for variable Q609, run the following code:

comparative_gender_plots$Q609
## Warning: `position_dodge()` requires non-overlapping x intervals.

You have now completed all steps in the example code. Congrats!


  1. If the style of this code is confusing, see Create a Summary in Chapter 1↩︎