Creating a simple Morris bar chart with rCharts and Survey packages

December 16, 2013

(This article was first published on dataism » R, and kindly contributed to R-bloggers)

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.


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

t <-svytotal(~sex, sdta)

m <-svymean(~sex, sdta)

dt <-

dm <-

d <-[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")


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.


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

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


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. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)