Site icon R-bloggers

Creating a simple Morris bar chart with rCharts and Survey packages

[This article was first published on dataism » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Creating interactive graphs and charts is a great way to show a lot of information in a small space without cluttering up the drawing canvas. This post describes the use of Ramnath Vaidyanathan’s rCharts package with Thomas Lumley’s survey package to create a simple bar chart with design-corrected standard errors that appear on mouseover. The data used in this example come from the Pew Research Center’s (PRC) Internet & American Life Project, December 2010 survey, which measured technology’s role in group formation and community engagement (a personal interest). The data can be downloaded, free of charge, from the PRC website here. A description of the survey methods can be found here.

Data preparation

This first bit of code creates a data frame called “tempslst” from a column-subsetted version of the downloaded .csv file, “December_2010_Social_Side.csv”. A call to colClasses specifies the variable types: columns 1-8 are assigned to characters, columns 9-58 to factors, and columns 67 and 68 (weight variables) to numeric.

tempslst <- read.csv(file="December_2010_Social_Side_Short.csv", head=TRUE,sep=",", colClasses=c(rep('character',8), rep('factor',57), 'numeric','numeric'))

After loading the survey package, a survey design object called "sdta", created through assignment of the results of survey's svydesign function, will include both the survey responses and the survey sample design information needed for analysis. Functions svytotal and svymean will create two vectors, with weighted totals and means and their corresponding standard errors, respectively. For the chart, I'm more interested in showing the population proportions than the population totals, but will keep the totals as an optional addition to the hoverCallback return string later. The two vectors will then be combined column-wise. Only the 'total' column from the totals data frame (dt[1]) will be appended.

require(survey)

sdta< -svydesign(id=~1, weights=~weight, data=tempslst)

t < -svytotal(~sex, sdta)

m < -svymean(~sex, sdta)

dt < - as.data.frame(t)

dm < - as.data.frame(m)

d < - as.data.frame(cbind(dt[1],dm))

To make a prettier chart, these next lines of code create formatted variables for the hoverCallback return string.

d$ttl < - ceiling(dt$total)

d$pct < - 100*round(dm$mean, digits = 3)

d$se < - 100*round(dm$SE,digits=3)

d$sex < -c("Male","Female")

Graphing

After loading rCharts, creating the bar chart is simple. Wrapping the hoverCallback string in between #! and !# is a little hackery credited entirely to Dr. Vaidyanathan.

require(rCharts)

mp <- mPlot(ttl~sex, data = d, type = "Bar", names.arg=c("Male","Female") )

mp$set(hoverCallback = "#! function(index, options, content){ var row = options.data[index] return '' + row.sex + '' + ' ' + 'Percent: ' + row.pct + '' + 'SE: ' + row.se } !#")

mp

To see a copy of the interactive graph, click on this link: Simple Morris Chart

For a more detailed example of chart created with rCharts, click on this link: Horizontal Multibar

To leave a comment for the author, please follow the link and comment on their blog: dataism » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.