public
Created

survey visualization

  • Download Gist
vis of survey.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#r code to read survey responses
#from a google spreadsheet published to web as .csv
 
#please see http://lamages.blogspot.com/2012/01/feedback-from-vignette-survey.html
#and the github https://gist.github.com/1579665
#for another method using google fusion tables
 
#require(vcd) #use vcd since I have never used it before
#ended up not using since I could not control size and location
#of the assoc plot
 
url <- "https://docs.google.com/spreadsheet/pub?
 
hl=en_US&hl=en_US&key=0AieeEIaS0AOsdGpvbTltd0pLZGh1cjRjZnlaV3pVY1E&single=true&gid=0&output=csv
 
"
responses <- read.csv (url, stringsAsFactors=FALSE)
 
#thanks to lamages for this date conversion (better than my old)
#mine different since in US with month/day/year and hour:minute:second
responses$Timestamp <- as.POSIXct(responses$Timestamp,
format="%m/%d/%Y %H:%M:%S")
 
#add some easier column names to the responses
colnames(responses) <- c("date","frequency","rating","comments","objective")
 
 
#code to do jpeg
#jpeg(filename="survey results.jpeg",quality=100,units="in",height=8,width=6,res=96)
 
 
#sets up the layout for plotting
#I still have a lot to learn here but maybe
#one more example might help someone out
#matrix will be
# [,1] [,2]
#[1,] 1 2
#[2,] 3 3
#[3,] 4 4
#so graph 1 and 2 will be in the first row
#3 will fill the entire second row and
#4 will fill the entire third row
layout(matrix(c(1,3,4,2,3,4),3,2),widths=c(8,2),heights=c(1,1,2))
 
#get some colors to use in the charts
colpal <- colorRampPalette(c("steelblue4","grey"),space="rgb")
 
#borrowed this also from lamages
#table summarizes
day <- as.data.frame(table(cut(responses$date,"day")),stringsAsFactors=FALSE)
#name the columns
colnames(day) <- c("day","freq")
par(mar=c(2,4,4,4))
plot(day$freq~as.Date(day$day),type="o",lwd=3,bty="n",xlab=NA,ylab="# of responses")
title(main="Analysis of Timely Portfolio Survey Results",
adj=0,cex.main=2,outer=TRUE,line=-1.5)
title(main="Responses by day",adj=0,line=0.5)
 
#gets same as above in matrix form for easier use of barplot
#could just do as.matrix(day)
day.matrix <- as.matrix(table(cut(responses$date,"day")))
par(mar=c(2,0,4,4))
barplot(day.matrix,ylim=c(0,sum(day.matrix)+5),col=colpal(length(day$day)))
 
par(mar=c(4,4,4,4))
#I assume there is an easier way to do this with tickmarks in the middle
#for now I will hack around it
hist(responses$rating,breaks=1:5,main=NA,col=colpal(5),axes=FALSE,xlab=NA)
axis(side=1,at=c(0:5)+0.5,labels=c(1:6),pos=0)
axis(side=2)
title(main="Histogram of rating",adj=0,line=1)
 
#sort by frequency of visit; wish I would have made this numeric
#something like how often do you visit per month - 30,15,10,5,1
#but I'll just deal with this manually
#convert to a numeric value
#unique(responses$frequency) to see the responses for frequency of visit
responses[which(responses$frequency=="Daily"),2] = 30
responses[which(responses$frequency=="Several times each week"),2] = 15
responses[which(responses$frequency=="Once a week"),2] = 4
responses[which(responses$frequency=="Once a month"),2] = 1
responses[which(responses$frequency=="Less than Once a month"),2] = 0
responses$frequency = as.numeric(responses$frequency)
 
par(mar=c(5,4,2,4))
assocplot(with(responses,table(rating,frequency)),
col=c("steelblue4","indianred4"),xlab="rating",ylab="visit frequency")
title(main="Association plot of rating and visit frequency",adj=0)
 
#dev.off()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.