Created
November 30, 2011 03:36
-
-
Save jimlindstrom/1407884 to your computer and use it in GitHub Desktop.
Plotting Likert-scale data with R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inputs | |
feature_names <- c("Feature 5", "Feature 4", "Feature 3", "Feature 2", "Feature 1") | |
num_features <- length(feature_names) | |
y <- array(c(10,4,1,0, 3,4,2,0, 1,2,8,1, 0,0,5,1, 1,2,5,3), dim=c(4,num_features)) | |
# Calculate plot | |
num_neg_ratings <- 0 | |
num_pos_ratings <- 0 | |
for(i in 1:num_features) { | |
num_neg_ratings = max(num_neg_ratings, sum(y[1:2,i]), sum(y[3:4,i])) | |
num_pos_ratings = max(num_pos_ratings, sum(y[1:2,i]), sum(y[1:2,i])) | |
} | |
x <- array(0, dim=c(6, num_features)) | |
for(i in 1:num_features) { | |
x[1, i] <- num_neg_ratings-sum(y[1:2, i]) | |
x[2:5,i] <- y[1:4, i] | |
x[6, i] <- num_pos_ratings-sum(y[3:4, i]) | |
} | |
# do the plot | |
png("/tmp/jbl.png", width=600, height=280) | |
colors <- c("white","#c91629","#ff5c76","#4e9cff","#0557d6","white") | |
par(mar=c(4.1,10.1,4.1,4.1)) | |
barplot(x, main="Feature Valence", axes=FALSE, | |
col=colors, space=1.1, cex.axis=1.0, las=1, border=NA, | |
names.arg=feature_names, cex=1.0, horiz=TRUE) | |
axis( | |
side=1, # X axis | |
at=c(0, num_neg_ratings/2, num_neg_ratings, num_neg_ratings+(num_pos_ratings/2), num_neg_ratings+num_pos_ratings), | |
labels=c("Hate","Dislike",NA,"Like","Love") | |
) | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment