Chapter 6 Single Country Trend Graphs
This chapter will cover how to create trend graphs for one country. Prior to his, we have only used a single data set, survey1
. We will now also use the data sets survey2
and survey3
. Refer to the assumed header code to see where survey1
, survey2
, and survey3
came from.
6.1 Create Single Trend Graph
At the end, your code will look like the following:
df_list <- list(
survey1,
survey2,
survey3
)
survey_dates <- c("Survey 1 Year",
"Survey 2 Year",
"Survey 3 Year")
plot_trend_individual("Q1COVID19",
df_list,
survey_dates,
"Algeria",
.caption = "Arab Barometer Wave VI, Algeria")
The code produces this graph:
Let’s go!
6.1.1 Prep Work
Unlike other graphs in other chapters, the user does not create a summary for a trend graph. There is only one function to use: plot_trend_individual()
.
You can create the plot all in one step with the function, but it is prudent to do a little prep work before hand. This makes your work more clear and your life easier down the line.
Create a Data Frame List
The first step is to create a list of data frames.
There should be one data frame for each period you wish to graph. That is, each data frame should be from a different survey you want to include in your graph. In the example for the chapter, we’re using the three surveys from Wave VI, which we call survey1
, survey2
, and survey3
. Each survey is its own data frame.
The list of data frames should be in the order you want them to appear (ideally, chronologically). The data on the graph will show up in the order of the list. So if you create a list in the order list(survey2, survey1, survey3)
, the data from survey2
will show up before the data from survey1
.
Please note: This is ambivalent to language! The plot_trend_individual()
function will assume the order of the list is the correct order, and treat it accordingly. When you enter a list as list(survey1, survey2, surveye3)
, the English graph will show the data right to left (survey 1 -> survey 2 -> survey 3), while the Arabic graph will show the data left to right (survey 3 <- survey 2 <- survey 1). You do not need to alter your input.
Create a Date Vector
The next step is to create a vector of the dates you want to show on the x-axis of your graph. Branding guidelines call for the years in which the survey took place. For simplicity in the example, let’s just say "Survey X Year"
.
You need a date for each data frame in your data frame list. Otherwise, the function won’t know how to label the x-axis.
The dates should also be in the order you want them to appear. The dates should line up with the data frames. In the chapter example, the order of the data frames is survey 1, survey 2, survey 3. Therefore, the survey dates need to have the order survey 1 year, survey 2 year, survey 3 year.
Please note: This is ambivalent to language! Just as with the data frame list, the plot_trend_individual()
function will put the data in the correct order according to the language of the graph.
Now that we have defined our data frame list and survey dates, we can create our trend plot.
6.1.2 Plot a Trend Graph
To create a trend graph plot for an individual country, use the plot_trend_individual()
function.
This function takes a few more parameters, and in a different order, than the functions we have worked with so far. In total, there are four necessary parameters: .var
, data_frames
, svry_dates
, and select_country
. To see a complete list of parameters, including optional ones, use ?plot_trend_individual()
in your R
console.
The parameter .var
is the variable you want to plot. It must have the same name in every data frame in the data frame list. If the variable you want to plot is named "Q101"
in one sure, but "Q102"
in another, the function will not include "Q102"
in the plot. Computers can do a lot but as of yet they cannot think critically, so the onus is on you.
The parameter data_frames
is a list of data frames. This is what we created here.
The parameter svry_dates
is a character vector of dates that will show up on the x-axis of the graph. This is what we created here.
The parameter select_country
is the name of the country you want to create the graph for.
Now let’s fill it in.
plot_trend_individual(.var = "Q1COVID19", # Variable to graph
data_frames = df_list, # List of data frames
svry_dates = survey_dates, # Vector of survey dates
select_country = "Algeria") # Country to graph
Nearly there! What still needs to be changed?
The caption, of course! Just as in all the other plot_
functions in the ArabBarometR
package, you can change the caption using the .caption
parameter.
plot_trend_individual(.var = "Q1COVID19",
data_frames = df_list,
svry_dates = survey_dates,
select_country = "Algeria",
.caption = "Arab Barometer Wave VI, Algeria")
Putting all the steps together, we have the code and graph we originally started with.
6.2 Create Many Graphs
A nice payoff of creating the data frame list and the date vector is that these objects (df_list
and survey_dates
) can be re-used any time you want to create a graph over the same time period.
For example, say you wanted to also create a graph tracking Algerian’s views of the economy and religiosity. The only parameter in the plot_trend_individual()
function that needs updating is .var
.
df_list <- list(
survey1,
survey2,
survey3
)
survey_dates <- c("Survey 1 Year",
"Survey 2 Year",
"Survey 3 Year")
plot_trend_individual("Q1COVID19",
df_list,
survey_dates,
"Algeria",
.caption = "Arab Barometer Wave VI, Algeria")
plot_trend_individual("Q101",
df_list,
survey_dates,
"Algeria",
.caption = "Arab Barometer Wave VI, Algeria")
plot_trend_individual("Q609",
df_list,
survey_dates,
"Algeria",
.caption = "Arab Barometer Wave VI, Algeria")
Notice that the code produced two graphs, even though df_list
and survey_dates
were only defined one time. Once you have created the df_list
and survey_dates
objects, you can re-use them for any graph that is covering the same time period.
You can also you the map()
function from the purrr
package. This method is similar to what was done for creating many single country graphs, many single country demographic graphs, many comparative graphs, and many comparative demographic graphs.
As a quick review, in earlier chapters the first step was identify the variables to plot. We did that by creating a named list of of the variables we wanted to plot.
The second step was creating a list of summaries by give the summarize function the named variable list.
The third and final step was mapping that list of summaries to the related plot function. In the mapping function, we would hold the caption constant since we wanted the same caption to appear on every graph.
The plot_trend_individual()
function creates summaries internally, so we can “skip” the second step. Once we identify our variables, we go right to the mapping step. We map a named list of variables to the plot_trend_individual()
function. We want to create a graph for the same country over the same time period, just using different variable, so we hold the data frame list, survey dates, country, and caption constant.
6.2.1 Identify the Variables
We are creating trend graphs for the variables Q1COVID19
, Q101
, and Q609
.
variables_2_plot <- list("Q1COVID19",
"Q101",
"Q609")
names(variables_2_plot) <- c("Q1COVID19",
"Q101",
"Q609")
Now you have a names list of variables. Let’s plot them.
6.2.2 Plot the Variables
Recall we want to hold constant the data_frames
, svry_dates
, select_country
and .caption
parameters because those will be the same for every graph. The only parameter we want to vary is .var
.
plots <- map(
variables_2_plot, # Vector of variables to plot
plot_trend_individual, # Map to function `plot_trend_individual()`
data_frames = df_list, # Hold constant the `data_frames` parameter
svry_dates = survey_dates, # Hold constant the `svry_dates` parameter
select_country = "Algeria", # Hold constant the `select_country` parameter
.caption = "Arab Barometer Wave VI, Algeria"
)
Excellent!
The plots have been saved to a named list called plots
. To access the plots for each variable, you can use the $
and variable name.
You’ve done it! You have created many trend plots at once!
6.3 Extras
6.3.1 Font Size
The font size default brand size is used for the graph text. You can learn how to change that in the chapter on changing the font size.
6.3.2 Colors
You can learn how to change the colors for the single country trend graph in the chapter on changing graph colors.