A short tutorial on multi-panel or faceted plotly graphics in R, without ggplotly(), with data from the Arab Barometer III survey project.
The Arab Barometer Wave III survey includes a number of questions about trust in different government institutions. Respondents were given the following prompt: “I will name a number of institutions, and I would like you to tell me to what extent you trust each of them.” In each of the twelve states included in the survey, the public was significantly more likely to trust “the armed forces (the army)” than to trust “the government (the cabinet).”
In some states, like in Kuwait, for example, the level of trust in both institutions was relatively high. But in many other states, the difference in the level of trust between both institutions was extreme. For example, in Egypt, only 21% stated that they trusted the government to a “medium extent” or “great extent,” but 91% stated that they trusted the army. Likewise, in Lebanon, only 14% stated that they trusted the government, but 81% stated that they trusted the army.
I could think of a few hypotheses that might plausibly explain variation in support for each institution across these countries, but rather than write an article on the topic of Arab confidence in national armed forces, I’d like to use this data to explore some features of the interface between R and plotly – mainly faceting and including chloropleth maps.
Making the Plot
I’m using pre-summarized data, which can be downloaded here, because most real-life sample surveys rely on complex design elements, for example, estimates that need to be weighted, or standard errors that need to account for clustering. Few vizualization options in R – ggplot and plotly included – easily accomodate complex samples, so it is usually necessary to work with pre-summarized data, for example, estimates and standard errors calculated with Thomas Lumley’s excellent survey package.
The basic syntax of a plotly plot is to use the plot_ly() function to make the plot, and then the layout() function to apply additional aesthetics like titles and margins. However, it’s not always clear which options go in which functions, and plotly’s R API documentation can be a little confusing, because so many examples rely on ggplot. Editing specific plot elements can also be difficult, because plotly’s JSON format needs to be worked with in R though a number of nested lists. The plot here has three pannels, and the process of getting to the final plot can be broken down into about five sequential steps:
Defining some global options, like fonts, margins, colors, and map projections.
Plot the first frame: I mapped trust in government first.
Plot the second frame: I plotted points with confidence intervals second.
Plot the third frame: I mapped trust in armies last.
Bring the three plots together, applying some additional aesthetics.
Trust in Government
Points and Error Bars
Trust in Army
Combining the Panels
First we combine the two maps into a single subplot, and then we create a second subplot by combining the points and error bars with the first subplot of maps. Note that when creating subplots, the previous layouts are overwritten. So, we have to apply a new layout to the final plot. I provide a title with a link to the source data, with a basic HTML tag. I suppress the legend, which is arguably a bad idea, but I’m going to rely on the hovertext and the fact that colors codes are consistent across the plots. Because my previous layouts were overwritten, including plot and axis titles, I have to add these again. But, the catch is: a plot can only have one title. So, I have to add subtitles for each of the subplots as plot annotations. Annotations take an x and y value, which are percents away from the anchor points, which in this plot, is the bottom-left corner; it took some trial and error to get these in appropriate positions. Finally, my left margin is greater than my right margin, to keep country names from being clipped. Append %>% plotly_POST("Trust in Government vs. Trust in Army in Twelve Arab States") to post to plot.ly.
RnotR CHLOROPLETH · PLOTLY · R · BAROMETER tutorials