Split Bars

Visualising Likert scales (survey results), using Split Bars.

R code

# Libraries -------------------------------------------------------------

library(tidyverse)
library(stringr)
library(RColorBrewer)
# Data -------------------------------------------------------------

dataSource <- "data/trust_in_media_2015.csv"
data <- read_csv(dataSource)
data <- gather(data, colnames(data)[2:6], key = "Index", value = "Value") # gather data in long format
data$Index <- factor(data$Index, levels = c("Very low trust", "Low trust", "No answer", "High trust", "Very high trust")) # set factors
data$Topic <- str_wrap(data$Topic, width = 42) # wrap text into two lines
# Theme -------------------------------------------------------------

plotTitle <- "Trust in media reporting"
plotSubTitle <- "regarding widely reported topics in 2015"
plotCaption <- "inspired by: https://www.datawrapper.de/_/9rodN"

familyPrimary <- "IBM Plex Sans Condensed"
familySecondary <- "IBM Plex Sans Condensed"

colorTextPrimary <- "#2a2a2a"
colorTextSecondary <- "#7a7a7a"

update_geom_defaults("text", list(family = familySecondary, size = 3, face = "bold", color = colorTextSecondary )) # update default geom text

theme_plot <- function(){
theme(

aspect.ratio = 1,
text = element_text(family = familyPrimary, size = 12),

strip.background = element_blank(),
strip.text = element_text(family = familyPrimary, hjust = -0.06, size = 10, color = colorTextPrimary),

plot.title = element_text(size = 13, margin = unit(c(0, 0, 6, 0), "pt"), color = colorTextPrimary ),
plot.subtitle = element_text(size = 13, margin = unit(c(0, 0, 32, 0), "pt"), color = colorTextSecondary ),
plot.caption = element_text(size = 9, margin = unit(c(32, 0, 0, 0), "pt"), color = colorTextSecondary, face = "italic", hjust = 0),
plot.margin = unit( c(32, 48, 32, 48), "pt"),

panel.grid = element_blank(),
panel.border = element_blank(),
panel.background = element_rect(fill = NA, colour = NA), 
panel.spacing = unit(0, "lines"),

axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(size = 10, family = familySecondary, hjust = 1, color = colorTextPrimary, lineheight = 0.9, margin = unit(c(0, 12, 0, 0), "pt"))

) 
}
# Plot -------------------------------------------------------------

plot <- ggplot(data) +
geom_bar(aes(x = Topic, y = Value, fill = Index), width = 0.5, stat = "identity") +
geom_text(aes(x = Topic, y = Value, label = paste(Value, "%", sep = "") ), hjust = -0.3) +

scale_y_continuous(limits = c(0, 100), expand = c(0, 0)) +
scale_fill_brewer(palette = "BrBG") +
guides(fill = FALSE) +

labs(title = plotTitle, subtitle = plotSubTitle, caption = plotCaption ) +

coord_flip() +
theme_plot() +

facet_wrap(~Index, nrow = 1, strip.position = "bottom")

plot
# Export --------------------------------------------------------------------

#ggsave(plot, height = 12, width = 12, device = cairo_pdf, filename = paste("export/", plotTitle, ".pdf", sep = "") )