Chapter 3 Single Country Demographic Plots
This chapter will cover how to make a frequency graph by a demographic for a single country.
This code assumes you have already added the demographic variables to your code. See the section Header Code in the introduction.
3.1 Create a Single Graph
This section will go over how to make a one-off frequency graph by a demographic for a single country.
At the end, your code will look like the following:
survey1 %>%
calculate_demographic_smry_individual("Q1COVID19",
"gender",
"Algeria") %>%
plot_demographic_smry_individual(.caption = "Arab Barometer Wave VI, Survey I, Algeria")
That code will produce the following graph:
## Joining with `by = join_by(Q1COVID19)`
## Joining with `by = join_by(Q1COVID19)`
## Warning: `position_dodge()` requires non-overlapping x intervals.
Notice these messages and warnings? That’s ok! You can ignore them. In the rest of this chapter, the messages and warning will be silenced. They are shown here to make sure the reader knows nothing is wrong if you see them.
3.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_individual() function.
Just like calculate_smry_individual(), the four main parameters you need to provide to this function are (1) the data you are using, (2) the variable you want to plot, (3) the demographic you want to divide the variable by, and (4) the country you want to plot it for. To see all the input parameters for the function, type the code ?calculate_demographic_smry_individual
in your R console.
In this example, the variable we want to plot is Q1COVID19, the demographic we want to compare is gender, and the country we want to plot it for is Algeria1.
survey1 %>% # Data
calculate_demographic_smry_individual("Q1COVID19", # Variable to summarize
.dem = "gender", # Demographic
.country = "Algeria") # Country
The above code will produce the following output:
## $gender
## gender Q1COVID19 Percent
## 1 Female 1 33
## 2 Female 2 44
## 3 Female 3 8
## 4 Female 4 15
## 5 Female 666 0
## 6 Male 1 22
## 7 Male 2 44
## 8 Male 3 9
## 9 Male 4 25
## 10 Male 666 0
The function calcuate_demographic_smry() creates a named list. In this list, each item is a summary broken down by a demographic and the name of each item is that demographic.
Let’s save our gender demographic summary and move on to plotting it.
3.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_individual().
There is only one necessary parameter to use plot_smry_individual(): the summary data frame. For a complete list of acceptable parameters and documentation, you can run ?plot_demographic_smry_individual
in your R console.
Now, we can plug our summary into the plot function:
The above code is the same as:
Both stylings of code produce the following graph:
Yet again, we see the caption needs to be changed. We can change it the same way we did in Chapter 1.
Q1COVID19_gender %>%
plot_demographic_smry_individual(
.caption = "Arab Barometer Wave VI, Survey I, Algeria"
)
Now we have the graph:
Since Q1COVID19_gender
is the same as survey1 %>% calculate_demographic_smry_individual("Q1COVID19","gender","Algeria")
, 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.
3.2 Create Many Graphs
We will follow the same steps from Chapter one to create many graphs at once.
- First, identify the variables to plot.
- Second, create summaries of those variables.
- 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",
"Q609")
names(variables_2_plot) <- c("Q1COVID19",
"Q2061A",
"Q609")
#......................Create the summaries......................
gender_summaries <- survey1 %>%
calculate_demographic_smry_individual(variables_2_plot,
"Algeria",
.dem = "gender")
#.......................Plot the summaries.......................
gender_plots <- map(gender_summaries,
plot_demographic_smry_individual,
.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:
To see the plot for variable Q2061A
, run the following code:
Finally, to see the plot for variable Q609
, run the following code:
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 1: Q1COVID19
, Q2061A
, and Q609
.
Let’s begin.
3.2.1 Create Summaries
The same function used to create a single summary is used to create many summaries: calculate_demographic_smry_individual(). Recall it takes three parameters: (1) the data you are using, (2) the variable(s) you want to plot, and (3) the country you are creating the plot for. Now, instead of one variable, you supply the list of variables.
survey1 %>% # Data
calculate_demographic_smry_individual(variables_2_plot, # Variables to summarize
"Algeria") # Country
The above code produces a nested named list. Each element in the list is another named list. Each element in the outer list is named for a variable you want to create summaries for. The inner list of each element is a summary with the data grouped by age, gender, income, and education. You can think of it like the following bullet point list:
calculate_demographic_smry_individual
outputQ1COVID19
age
Q1COVID19 summary grouped by agegender
Q1COVID19 summary grouped by genderincome
Q1COVID19 summary grouped by incomeeducation
Q1COVID19 summary grouped by education
Q2061A
age
Q2061A summary grouped by agegender
Q2061A summary grouped by genderincome
Q2061A summary grouped by incomeeducation
Q2061A summary grouped by education
Q609
age
Q609 summary grouped by agegender
Q609 summary grouped by genderincome
Q609 summary grouped by incomeeducation
Q609 summary grouped by education
For this example, we will again only be focusing on gender plots. To get only gender summaries, just specify the .dem parameter.
survey1 %>% # Data
calculate_demographic_smry_individual(variables_2_plot, # Variables to summarize
"Algeria", # Country
.dem = "gender") # Demographic
Now, the output of this function looks like this bullet point list:
calculate_demographic_smry_individual(.dem="gender")
outputQ1COVID19
gender
Q1COVID19 summary grouped by gender
Q2061A
gender
Q2061A summary grouped by gender
Q609
gender
Q609 summary grouped by gender
Much more manageable .
Let’s save this outcome as an object and move on to plotting.
gender_summaries <- survey1 %>%
calculate_demographic_smry_individual(variables_2_plot,
"Algeria",
.dem = "gender")
## Joining with `by = join_by(Q1COVID19)`
## Joining with `by = join_by(Q1COVID19)`
## Joining with `by = join_by(Q2061A)`
## Joining with `by = join_by(Q2061A)`
## Joining with `by = join_by(Q609)`
## Joining with `by = join_by(Q609)`
3.2.2 Plot the Summaries
Again, the same function to create one plot is used to create many plots: plot_demographic_smry_individual(). Just like in Chapter 1, we need to use the map function.
In this case, you supply the list of summaries you just created, and the plot_demographic_sumry_individual() function. The code follows:
The code produces the following output:
## $Q1COVID19
##
## $Q2061A
##
## $Q609
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(
gender_summaries, # List of summaries
plot_demographic_smry_individual, # Plotting function
.caption = "Arab Barometer Wave VI, Survey I" # Caption
)
## $Q1COVID19
##
## $Q2061A
##
## $Q609
Congratulations! You have created three plots at once. You can store them in as a single list and call them one at a time.
gender_plots <- map(
gender_summaries, # List of summaries
plot_demographic_smry_individual, # Plotting function
.caption = "Arab Barometer Wave VI, Survey I" # Caption
)
Now, all three plots have been stored in a named list named gender_plots
. To look at the first plot:
To see the plot for variable Q2061A
, run the following code:
Finally, to see the plot for variable Q609
, run the following code:
You have now completed all steps in the example code. Congrats!
If the style of this code is confusing, see Create a Summary in Chapter 1↩︎