Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize width of bar plot in likert plot

Tags:

I'm using the likert package by jbryer and want to visualise the data with stacked bar plots. The size/width of these bar plots depends on how many bars are in the graph, i.e. with only one bar the bar is pretty wide, while they get thinner the more bars are plotted.

I'd like to costumly set the size/width of the bar, so that they stay the same, no matter how many bars are plotted in the graph, i.e. that the bar size is the same for the plots of l29_5 and l29_2.

Likert bar plot with two bars

Likert bar plot with five bars

library(ggplot)
library(likert)    
data(pisaitems)

items29_5 <- pisaitems[,substr(names(pisaitems), 1,5) == 'ST25Q']
colnames(items29_5) <- c("Magazines", "Comic books", "Fiction", 
                    "Non-fiction books", "Newspapers")

items29_2 <-  items29_5 %>% 
  select("Magazines", "Comic books")


l29_5 <- likert(items29_5)
l29_2 <- likert(items29_2)

plot(l29_5)
plot(l29_2)
like image 318
Torakoro Avatar asked Feb 14 '20 18:02

Torakoro


1 Answers

The function likert.bar.plot(..) doesn't have any parameters for the width of the bins. However, it returns an object of class ggplot, so that it can be processed with standard ggplot2 instructions.
The final plot consists of several ggplot layers. The problem here is not to add a new instruction or layer to the plot, but rather to update parameters for one or more inner layers. Namely, we need to set the width for all the layers containing geom_bar(). For this, we find the relevant layers at first:

p <- likert.bar.plot(l29_2)
p$layers

From the output, it can be seen that geom_bar is included in layers 2 and 3. Then we can modify them by adding the width parameter:

p$layers[[2]]$geom_params$width = 0.3
p$layers[[3]]$geom_params$width = 0.3
p

likert plot with predefied bin widths

like image 76
DzimitryM Avatar answered Sep 30 '22 18:09

DzimitryM