2

This is my dataframe

library(likert)

# Create dataframe
group <-    c("A","B","C","C","B","A","B","C","A","B","A","C","B","B","C","A","B","B","A","C","B","B","A","A")
question1<- c(1,2,3,2,2,3,1,3,1,2,3,1,3,2,3,1,2,3,3,1,2,3,2,1)
question2<- c(1,2,3,2,1,1,1,3,3,2,1,3,2,2,3,1,2,3,1,2,3,1,2,1)
question3<- c(1,1,1,2,2,2,3,3,3,2,2,3,1,3,2,2,3,2,3,1,2,3,1,2)
question4<- c(3,3,3,2,1,2,3,3,2,1,2,2,3,1,3,2,2,1,2,3,3,2,1,2)
my_df <-data.frame(group,question1,question2,question3,question4)

# Add label 
my_df[, 2:5] <- lapply(my_df[, 2:5], factor,
                                      levels=c(1, 2, 3), 
                                      labels = c("Competent", "Average", "Imcompetent")) 
my_df <- as.data.frame(my_df)

# Draw graph using likert()
graph <- plot(likert(my_df[, 2:5], grouping=my_df[, 1]), wrap = 60, text.size=4, col=c("#C70039", "#E0DCD8", "#DAF7A6")) +
  guides(fill=guide_legend("")) +# plot graph
  theme(legend.text =element_text(color="black",size=12), axis.text=element_text(color="black",size=12), strip.text=element_text(size=15))
graph

And the result looks like this enter image description here

I want to reorder the groups within each question block in ascending/descending order basing on the share of competent answer. For example, groups in "question1" block will be ordered as A, C, B; and "question3" block will be C, A, B.

Thank you in advance!

I have listed what I tried above

  • I believe `likert` uses `ggplot2`, which in many ways is a good thing. Without looking at the source underneath, I'll infer that this is a `facet_*` plot, and therefore the `aes`thetics are predefined. This means that if the groups in the first facet (question 1) are ordered `C B A`, then _all_ facets (all questions) will share the same order. I think `likert` cannot control this, and I don't know if it's even possible in ggplot2 native. – r2evans Aug 18 '23 at 03:46
  • My first thought would be to break it into one question per plot, and use `patchwork` (including its ability to unify legends into one) to combine the plots. This way, you can control the order of groups for each question. – r2evans Aug 18 '23 at 03:46
  • Shouldn't the order of question2 be `A B C`? I don't see why "C" would be first since it's _empty_. – r2evans Aug 18 '23 at 04:02
  • Hi @r2evans, I have modified the question, what I was referring to was "question3", not "question2". Thank you for your suggestion – James_greyyy Aug 18 '23 at 07:17

1 Answers1

1

I don't think ggplot2 is going to support this natively, so we can generate each question individually and combine them with the patchwork package.

We'll remove the x-axis name/labels/ticks for all but the bottom question, and we will combine the legends into one at the bottom.

In order to control the order of the groups on the y-axis, as is always the case when a ggplot2 question asks "how to control the order": factors (and their levels).

plots <- lapply(2:5, function(i) {
  lik <- likert(my_df[, i, drop=FALSE], grouping=my_df[, 1])
  lik$results$Group <-
    factor(lik$results$Group, levels = lik$results$Group[ order(lik$results$Competent) ])
  p <- plot(lik, wrap=60, text.size=4, col=c("#C70039", "#E0DCD8", "#DAF7A6")) +
    guides(fill = guide_legend("")) +# plot graph
    theme(
      legend.text = element_text(color="black", size=12),
      axis.text = element_text(color="black", size=12),
      strip.text = element_text(size=15))
  if (i < 5) {
    p <- p +
      theme(axis.title.x=element_blank(),
            axis.text.x=element_blank(),
            axis.ticks.x=element_blank())
  }
  p
})

library(patchwork)
Reduce(`/`, plots) + plot_layout(guides = "collect") &
  theme(legend.position = "bottom")

ggplot combined single-facets with y-axis groups individually ordered

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Thank you @r2evans, your code produced the exact output I need. Also, how can I interpret the "lik$results$Group" in the code? – James_greyyy Aug 18 '23 at 07:14
  • Look at `str(lik)` to see the structure of the object returned by the `likert()` function, by I'm just tweaking one component of it – r2evans Aug 18 '23 at 11:00