by Peggy Fan
Ph.D. Candidate at Stanford's Graduate School of Education
Part of my dissertation at Stanford Graduate School of Education, International Comparative Education program, is looking at the World Values Survey (WVS), a cross-national social survey that started in 1981. Since then there has been 6 waves, and the surveys include questions that capture the demographic, behaviors, personal beliefs, and attitudes of the respondents in a variety of contexts. I am interested in looking civic participation, which is often measured by the extent to which a person belongs to an organization outside of family and work (and religion).
The goal is to create a tool that facilitates preliminary data analyses on this large dataset. The shiny app turns out to be a great tool for data visualization and exploration.
Data manipulation
There are 85 countries from the first five waves in this dataset with about 255,000 observations. My outcome variable is based on a battery of questions from the WVS that ask if the respondent is a member of any of the following types of association: sports, arts, labor, politics, environmental, charity, women’s rights, human rights, or other. A respondent gets a “1” if he and she answers “yes” to any of the associational membership.
For the purpose of this app, I extract variables that are relevant, such as regions, country IDs, gender, educational attainment, and membership from the larger data set. Because the lowest unit of analysis is “country”, I calculate the country and regional averages of membership by topics of gender and educational attainment.
Reactive input
The reactive input function in shiny adjusts the data displayed based on the criteria selected. This allows many ways to dissect data. I utilize it to present data at the world, region, and country levels as well as by gender and education.
I create three tabs by topics. Users can choose “the world” or a region of interest or on the left, and using the reactive
with selectInput
, I create an object that makes the main panel display the corresponding data. I also use observe
to add another reactive element, which displays the list of country after the region is selected.
For gender and educational attainment tabs, renderTable
is perfect for displaying the information because it allows users to sort the data based on variables of interest. It also has other options, such as including a search box in the table, but I do not want extraneous features to clutter the table and a simple version is adequate in conveying the information.
server.R selectedData1 <- reactive({ if (input$region == "the world") { highested_table[,-1] } else { region = input$region highested_table[(highested_table$region == region), -1] } }) output$mytable1 = renderDataTable({ selectedData1() }, options = list(lengthMenu = c(5,10), pageLength = 5, searching = FALSE) ) observe({ region = input$region updateSelectInput(session, "country", choices = levels(as.factor(as.character(wvs_c$country[wvs_c$region==region]))), selected = levels(as.factor(as.character(wvs_c$country[wvs_c$region==region])))[1] ) }) ui.R sidebarPanel( selectInput("region", "Select a region:", list("All World"= "the world", "North America & Western Europe"="region1", "Central Europe"="region2", "Asia"="region3", "Latina America & Caribbean"="region4", "Sub-Saharan Africa"="region5", "Middle East & Northern Africa"="region6", "Oceania"="region7"), selected= "the World" ) mainPanel( tabPanel('Gender', dataTableOutput('mytable'), selectInput('country', 'Select a Country:', names(wvs_c$country), selected=names(wvs_c$country)[1]), plotOutput("myplot") ),
Visualization
The maps provide a holistic view of world and regional comparisons. I choose the rworldmap
package because it uses ISO3 as country identifier and I also use ISO3 in my own data, which makes merging of country level data and spatial polygons quite easy. Moreover, its default is a choropleth map by country, with which I only have to adjust the palette for styling.
#server.R library(rworldmap) wvs_c <- read.csv("/Users/peggyfan/Downloads/R_data/Developing_data_products/wvs_c") wvs_c <- wvs_c[, -1] colourPalette1 <-c("#F5A9A9", "#F6D8CE", "#F8ECE0", "#EFFBFB", "#E0F2F7", "#CEE3F6", "#A9BCF5") world <- joinCountryData2Map(wvs_c ,joinCode = "ISO3" ,nameJoinColumn = "iso3" ,mapResolution="li")
When “the world” is chosen, the map tab shows the mapping of the entire data set. The gender and educational attainment tabs show the regional breakdown of those two topics using ggplot
.
Below the table, I embed another panel so users can choose a specific country (listed in alphabetical order) to view its gender and educational attainment breakdown in charts created also with ggplot
.
For those who are interested in the tabular data displayed on the “gender” and “education attainment” tabs, they can download the data from the website for their own research purposes.
App address: https://peggyfan.shinyapps.io/shinyapps/
Hey Peggy,
wow, thanks a lot for your post! I really like the idea looking at survey data with shiny to make complex data more accessible. I've worked on a related thing recently (though not yet shiny) which might interest you:
In a global smartphone study, Dalia Research asked 51 questions to young mobile users in 64 countries including Colombia, Iran and Ukraine.
This is impressive - you have opinions from 32 000 people collected within 4 days from all over the world. About their religion, what they think about the Unites States, the global influence of the EU and where your country should invest more.
In my blog post workspace13.wordpress.com I show how to merge the open data set with World Bank indicators and create a world map using R.
If you come up with a cool visualization, you can win the chance to ask your own 5 question in the next global survey round www.facebook.com/DaliaResearch
Posted by: Tim | November 06, 2014 at 05:04