Home > Mobile >  How do I fix likert() clipping of data labels
How do I fix likert() clipping of data labels

Time:03-16

I'm working with likert() to generate plots of survey data. The plots are clipping display of percentage levels, as below:

image of plot

This is more / less severe for other similar plots in the same data set (so it's just percentage symbols here, but clips into digits in other instances) - and I'm wondering if anyone has a good idea as to how to tweak this bit of likert display so that labels are within the plot grid.

# data for reproducible example:

q25_data <- structure(list(Q25_self_and_family = c(4, 2, 3, 5, 3, 3, 4, 2, 
4, 2, 4, 4, 3, 3, 2, 5, 3, 4, 1, 3, 3, 2, 4, 2, 2, 2, 4, 3, 3, 
3, 2, 5, 5, 4, 2, 2, 2, 3, 1, 3, 2, 1, 2, 4, 2), Q25_local_area = c(3, 
3, 3, 5, 3, 2, 4, 2, 4, 2, 4, 3, 2, 3, 2, 5, 4, 5, 1, 4, 3, 3, 
4, 2, 3, 2, 3, 3, 2, 3, 2, 5, 5, 2, 2, 2, 2, 3, 1, 1, 2, 1, 2, 
4, 3), Q25_uk = c(4, 3, 3, 5, 2, 3, 5, 2, 4, 2, 4, 3, 3, 3, 3, 
5, 4, 5, 2, 3, 3, 2, 4, 2, 4, 3, 4, 3, 2, 4, 4, 5, 5, 4, 3, 3, 
2, 4, 2, 5, 2, 2, 2, 3, 3), Q25_outside_uk = c(4, 4, 3, 5, 4, 
4, 5, 2, 4, 3, 3, 3, 3, 4, 3, 5, 4, 5, 4, 3, 3, 2, 4, 2, 5, 3, 
3, 2, 2, 3, 4, 4, 5, 4, 4, 3, 2, 4, 4, 5, 2, 3, 2, 2, 2)), row.names = c(NA, 
-45L), class = c("tbl_df", "tbl", "data.frame"))

# libraries:
library(tidyverse)
library(likert)

# here goes!
title <- "How serious a threat do you think \nclimate change poses to the following?"
names(q25_data) <- c("You and your family in the UK", "People in your local area or city", "The UK as a whole", "Your family and/or friends living outside the UK")
# Set up levels text for question responses
q25_levels <- paste(c("not at all", "somewhat", "moderately", "very", "extremely"),  
                    "serious")
q25_likert_table <- q25_data %>% 
  mutate(across(everything(), 
                factor, ordered = TRUE, levels = 1:5, labels=q25_levels)) %>% 
  as.data.frame %>% 
  likert

plot(q25_likert_table, wrap=20, text.size=3, ordered=TRUE, low.color='#B18839', high.color='#590048')   
  ggtitle(title)  
  labs(title = title, y="")   
  guides(fill = guide_legend(title = NULL))   
  theme_ipsum_rc()  
  theme(axis.text.y = element_text(size = 9))

CodePudding user response:

That's always the trouble when using out-of-the-box options. (:

Two quick options as with each ggplot are to expand the limits or to increase the expansion of the scale (via e.g. expand = c(.1, 0)) to make more room for the labels.

Note that likert uses coord_flip under the hood so the relevant scale is scale_y_continuous. By default the limits are set to -105 to 105 which I increase to -110 to 110. Also, for the labels the absolute values are used.

p <- plot(q25_likert_table, wrap = 20, text.size = 3, ordered = TRUE, low.color = "#B18839", high.color = "#590048")  
  ggtitle(title)  
  labs(title = title, y = "")  
  guides(fill = guide_legend(title = NULL))  
  theme_ipsum_rc()  
  theme(axis.text.y = element_text(size = 9)) 

p  
  scale_y_continuous(labels = abs, limits = c(-110, 110))
#> Scale for 'y' is already present. Adding another scale for 'y', which will
#> replace the existing scale.

  • Related