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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With