Search code examples
rplotsjplotlikert

Custom color palette for plot_likert function from sjPlot


I can plot with following data using plot_likert function from sjPlot R package.

library(tidyverse)
df1 <-
  data.frame(
  matrix(
    data = sample(x = c("Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree"), size = 500, replace = TRUE),
    ncol = 5
    )
  ) %>% 
  mutate_all(., ~ ordered(., levels = c("Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree")))

df1
library(sjPlot)
plot_likert(
   items       = df1
  , cat.neutral = 3
  )

enter image description here

I wonder how to get the following color scheme:

Strongly Disagree = Dark Red
Disagree          = Light Red
Neutral           = Gray
Agree             = Light Green
Strongly Agree    = Dark Green

Solution

  • You could set your desired colors via the arguments geom.colors and cat.neutral.color:

    Note: As R has no color named lightred I switched to firebrick1.

    library(tidyverse)
    library(sjPlot)
    
    df1 <- data.frame(
        matrix(
          data = sample(x = c("Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree"), size = 500, replace = TRUE),
          ncol = 5
        )
      ) %>% 
      mutate_all(., ~ ordered(., levels = c("Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree")))
    
    plot_likert(items = df1, cat.neutral = 3, 
                geom.colors = c("darkred", "firebrick1", "lightgreen", "darkgreen"),
                cat.neutral.color = "gray")