Skip to content

Instantly share code, notes, and snippets.

@ha0ye
Created June 29, 2019 00:47
Show Gist options
  • Save ha0ye/c51c5aea9b0c8799469d923c94b4479c to your computer and use it in GitHub Desktop.
Save ha0ye/c51c5aea9b0c8799469d923c94b4479c to your computer and use it in GitHub Desktop.
library(likert)
data(pisaitems)
##### Item 24: Reading Attitudes
items24 <- pisaitems[,substr(names(pisaitems), 1,5) == 'ST24Q']
head(items24); ncol(items24)
names(items24) <- c(
ST24Q01="I read only if I have to.",
ST24Q02="Reading is one of my favorite hobbies.",
ST24Q03="I like talking about books with other people.",
ST24Q04="I find it hard to finish books.",
ST24Q05="I feel happy if I receive a book as a present.",
ST24Q06="For me, reading is a waste of time.",
ST24Q07="I enjoy going to a bookstore or a library.",
ST24Q08="I read only to get information that I need.",
ST24Q09="I cannot sit still and read for more than a few minutes.",
ST24Q10="I like to express my opinions about books I have read.",
ST24Q11="I like to exchange books with my friends.")
str(items24)
l24 <- likert(items24)
l24 #print(l24)
summary(l24)
summary(l24, center=1.5)
summary(l24, center=2)
# xtable
xtable(l24)
##### ----
new_fill_scale <- function(l, labels = letters[seq(l$nlevels)])
{
opts <- likert.options()
center <- (l$nlevels - 1)/2 + 1
lowrange <- 1:floor(center - 0.5)
highrange <- ceiling(center + 0.5):l$nlevels
low.color <- "#D8B365"
neutral.color.ramp <- "white"
high.color <- "#5AB4AC"
ramp <- colorRamp(c(low.color, neutral.color.ramp))
ramp <- rgb(ramp(seq(0, 1, length = length(lowrange) +
1)), maxColorValue = 255)
bamp <- colorRamp(c(neutral.color.ramp, high.color))
bamp <- rgb(bamp(seq(0, 1, length = length(highrange) +
1)), maxColorValue = 255)
cols <- NULL
if (center%%1 != 0) {
cols <- c(ramp[1:(length(ramp) - 1)], bamp[2:length(bamp)])
} else {
cols <- c(ramp[1:(length(ramp) - 1)], neutral.color,
bamp[2:length(bamp)])
}
scale_fill_manual(breaks = l$levels,
labels = labels, values = cols,
drop = FALSE)
}
# Plots
plot(l24) + new_fill_scale(l24)
plot(l24) + new_fill_scale(l24, labels = c("lorem", "ipsum", "red", "blue"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment