Skip to content

Instantly share code, notes, and snippets.

@prabhasp
Created April 17, 2014 20:41
Show Gist options
  • Save prabhasp/11010323 to your computer and use it in GitHub Desktop.
Save prabhasp/11010323 to your computer and use it in GitHub Desktop.
Plot the percent of don't know per question in two ossap surveys.
get_dk_reason <- function(df) {
dk <- names(df)[str_detect(names(df), "dont")]
llply(df[dk], function(x) { as.character(na.exclude(x)) })
}
plot_percent_dks <- function(dk_list, N) {
# d will be a list of question name and length
d <- ldply(dk_list, length)
# order the data frame
d <- arrange(d, V1)
# divide by N (which is supposed to be total responses
d$V1 <- d$V1 / N
# chop off _dontknow from end of question
d$.id <- str_replace(d$.id, '_dontknow', '')
# re-order the question list (will order the bar graph)
d$.id <- factor(d$.id, levels=d$.id)
# make plot
ggplot(data=d, aes(x=.id, y=V1)) + geom_bar(stat='identity') + coord_flip() +
scale_y_continuous(labels=percent) + labs(x="Percent of Don't Know response", y="Question")
}
require(formhub); require(ggplot2); require(stringr); require(scales)
edu <- formhubDownload("education_mopup", "ossap", pass=CHANGETHIS)
health <- formhubDownload("health_mopup", "ossap", pass=CHANGETHIS)
edu_dks <- get_dk_reason(edu)
hlt_dks <- get_dk_reason(health)
plot_percent_dks(edu_dks, nrow(as.data.frame(edu)))
plot_percent_dks(hlt_dks, nrow(as.data.frame(health)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment