right order in levels (Likert) with negative and positive values, but wrong color assignment

182 views Asked by At

I have a four-point Likert scale that I would like to stack by an ordered color palette that I have made (likert_palette), but there is always one side that is not ordered. Are there anyone with a solution on how to get the darkest red on the utmost left? The categories are of type factor and is ordered accordingly, and two of the categories have negative values and the other two positive values.

likert_palette = c("#F58C7B","#F9B8AD","#A0DCB3","#67C785")    


fivelevels <- c("Substantially less often",
                  "Somewhat less often",
                  "Unchanged",
                  "Somewhat more often",
                  "Substantially more often"
  )

Here in the example I have only one type of activity in the data frame, but there are several more in the original.

df <- cbind("activities"=c("Online lectures"), 
      "x"=c("Substantially less often",
            "Somewhat less often",
            "Somewhat more often",
            "Substantially more often"), 
      "prosent"=c(-0.02,-0.05, 0.32,0.42)) %>% 
  as_tibble() %>% 
  mutate(prosent = as.double(prosent),
         x = factor(x, levels = fivelevels))

Here it becomes tricky whenusing: reverse = T only reverse the color order for the positive values, but not the negative ones. So the green colors are

df %>%  
ggplot(aes(y = reorder(activities, desc(activities)), x = prosent, fill= x)) +
  # geom_col(position = position_stack (reverse = F)) +
  geom_col(position = position_stack (reverse = T)) +  #HERE
  geom_text(aes( x = prosent, label = scales::percent(prosent, accuracy = 1L)), 
            position = position_stack( reverse = T, vjust = 0.5), size = 2) + 
  scale_fill_manual(values = likert_palette) 

Thanks for the help!

1

There are 1 answers

0
Allan Cameron On

Because some of your values are negative, the negative values will stack away from zero, and the positive values also stack away from zero. You need to label your factors appropriately, and to avoid confusion you should use a named vector in scale_fill_manual:

df <- cbind("activities"=c("Online lectures"), 
      "x"=c("Substantially less often",
            "Somewhat less often",
            "Somewhat more often",
            "Substantially more often"), 
      "prosent" = c(-0.02,-0.05, 0.32,0.42)) %>% 
  as_tibble() %>% 
  mutate(prosent = as.double(prosent),
         x = factor(x, levels = fivelevels[c(1:3, 5:4)]))

df %>%  
ggplot(aes(y = activities, x = prosent, fill = x)) +
  geom_col() +
  geom_text(aes(x = prosent, 
                label = scales::percent(prosent, accuracy = 1L)), 
            position = position_stack(vjust = 0.5), size = 2) + 
  scale_fill_manual(limits = fivelevels[c(1:2, 4:5)],
                    values = setNames(likert_palette, fivelevels[-3])) 

enter image description here