This post shows how to use the likert R package. It allows to build 0-centered stacked barplot to study likert type items.
# Libraries
library(dplyr)
library(expss) # for apply_labels
# Create data
sample_prob <- function(n){
x <- sample(1:100,n)
return(x/sum(x))
}
set.seed(1806)
n <- 11
dataset <- data.frame(
question1 = factor(sample(0:(n-1), size=500, replace=T, prob = sample_prob(n))),
question2 = factor(sample(0:(n-1), size=500, replace=T, prob = sample_prob(n))),
question3 = factor(sample(0:(n-1), size=500, replace=T, prob = sample_prob(n))),
group = factor(sample(c("group1", "group2"), size=500, replace=T))) %>%
apply_labels(
question1 = "Voici un très long ou très long énoncé pour la question 1",
question2 = "Voici un très long ou très long énoncé pour la question 2",
question3 = "Voici un très long ou très long énoncé pour la question 3")
# Libraries
library(likert)
library(scales)
# Create likert object
p_group <- likert(dataset[, -which(names(dataset)=="group")], grouping = dataset$group)
# Build plot
plot(p_group, group.order = c("group1", "group2"), text.size = 3) + # text.size : size of percentage
ggtitle("Percentage of response to questions") +
egg::theme_article() +
theme(
# group labels
strip.background = element_blank(),
strip.text.x = element_text(size = 9, margin = margin(0,0,0.12,0, "cm")), # margin(top, right, bottom, left, "cm")
# axes
axis.title.x = element_text(size = 8), # x axis title
axis.text.x = element_text(size = 9), # x axis labels
axis.title.y = element_text(size = 8), # y axis title
axis.text.y = element_text(size = 11), # y axis labels
# legend
# legend.title = element_text(size = 10),
legend.title = element_blank(),
legend.text = element_text(size = 7.5),
legend.position = "bottom"
) +
guides(fill=guide_legend(ncol=11))
If you want to add labels:
# Libraries
library(likert)
library(scales)
# Function to get labels et set the width
get_label_df <- function(dataset, wrap=NULL){
vect_label <- NULL
for(i in names(dataset)){
label <- attr(dataset[, i], "label")
if(is.null(label)) label <- i
vect_label <- c(vect_label, label)
}
if(!is.null(wrap)){
vect_label <- vect_label %>% str_wrap(20)
}
names(vect_label) <- names(dataset)
return(vect_label)
}
# Change names of the dataset with labels
dataset_gp <- dataset
names(dataset_gp) <- get_label_df(dataset_gp)
# Create likert object
p_group <- likert(dataset_gp[, -which(names(dataset_gp)=="group")], grouping = dataset_gp$group)
# Build plot
plot(p_group, group.order = c("group1", "group2"), text.size = 3) + # text.size : size of percentage
ggtitle("Percentage of response to questions") +
egg::theme_article() +
theme(
# group labels
strip.background = element_blank(),
strip.text.x = element_text(size = 9, margin = margin(0,0,0.12,0, "cm")), # margin(top, right, bottom, left, "cm")
# axes
axis.title.x = element_text(size = 8), # x axis title
axis.text.x = element_text(size = 9), # x axis labels
axis.title.y = element_text(size = 8), # y axis title
axis.text.y = element_text(size = 11), # y axis labels
# legend
# legend.title = element_text(size = 10),
legend.title = element_blank(),
legend.text = element_text(size = 7.5),
legend.position = "bottom"
) +
guides(fill=guide_legend(ncol=11))
This document is a work of the statistics team in the Biostatistics and Medical Information Department at Saint-Louis Hospital in Paris (SBIM).
Developed and updated by Noémie Bigot and Anouk Walter-Petrich
noemie.bigot@aphp.fr; anouk.walter-petrich@u-paris.fr
Based on The R Graph Gallery by Yan Holtz.