Skip to content

Instantly share code, notes, and snippets.

@estherjk
Last active November 6, 2015 23:33
Show Gist options
  • Save estherjk/06e7ba1d7446ec8de4ed to your computer and use it in GitHub Desktop.
Save estherjk/06e7ba1d7446ec8de4ed to your computer and use it in GitHub Desktop.
Visualizing usability data with a heat map in R

This example shows how to create a heat map in R to visualize task success data.

Task A Task B Task C Task D Task E
0 0 1 1 0
0 1 1 1 0
1 1 1 1 1
0 0 1 1 0
1 1 1 1 0
0 1 0 1 0
0 0 1 1 0
0 0 0 0 0
library(ggplot2)
library(reshape)
# Change this to point to your directory
setwd('~/Desktop/uxr-heatmap')
# Read in data
data.df <- read.csv('data.csv')
num.users <- dim(data.df)[1]
num.tasks <- dim(data.df)[2]
# When R imports the data, spaces in the header row are replaced with "."... let's revert that
task.names <- gsub(".", " ", names(data.df), fixed=TRUE)
# Create a data frame with the total number of successes for each task
total <- data.frame(
task=task.names,
value=colSums(data.df)
)
# Reshape the data... ggplot2 uses long format, which has a column for variable types and a column for those variables' values
total.m <- melt(total)
#### Steps to create heat map ####
# 1. Create heat map
dark.orange <- '#d66a00'
light.gray <- '#f8f8f8'
p <- ggplot(total.m, aes(x=task, y=variable, label=value)) +
geom_tile(aes(fill=value), colour='white') +
scale_fill_gradient(low=dark.orange, high=light.gray, limits=c(0, num.users))
# 2. Add text
p <- p + geom_text()
# 3. Re-size the plot
p <- p + coord_fixed(ratio=2/num.tasks)
# 4. Give finishing touches
p <- p + labs(title='Number of users that successfully completed each task') +
theme(
panel.background=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank()
)
# 5. Save the plot
ggsave(filename='heatmap.png', width=6, height=3)
Task A Task B Task C Task D Task E
2 4 6 7 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment