Cya
Cya

Reputation: 367

How to edit the position of value labels in plot.likert?

I have a dataframe of scores scores <- data.frame(var1=c(1,3,5,6,1,4,10,2,5,3,7), var2=c(10,9,1,4,3,3,4,7,8,10,10))

which I transform into a factor with three levels as:

library(likert)
library(dplyr)

scores_factor <- scores %>% sapply(., cut, c(0, 6, 8, 10), include.lowest = TRUE, labels = c("Negative", "Okay", "Positive")) %>% data.frame

and then transforming it into a likert item and plotting it using the likert.plot from the "likert" package:

likert_scores <- likert(scores_factor)
p <- plot(likert_scores, 
          low.color="#ED5949", 
          neutral.color="#F3CA71", 
          high.color="#7CB166") +
     labs(title= "Hello world!") +
     theme(plot.title=element_text(size=16, 
           face="bold", color="black"), 
           plot.subtitle=element_text(size=11, 
           face="italic", color="black"), 
           text = element_text(color = "#333333", 
           axis.text.x=element_blank(), 
           legend.position="right") +
     theme_hc()
plot(p)

Now, the problem is that the likert.plot displays the edge value labels not inside the bars. I wish to find a way to print the labels inside the bars without having to resort on building a stacked barplot from scratch with ggplot2? Is this possible? If not what could be an alternative?

Thanks in advance.enter image description here

Upvotes: 2

Views: 1252

Answers (1)

Cya
Cya

Reputation: 367

After a day of trying to solve this myself, found a work-around which I post below in case others want to benefit from it.

For the history, a likert item stores the proportions of the factor's levels in the position:

likert_scores[["results"]][["Negative"]][1] 
likert_scores[["results"]][["Okay"]][1]
likert_scores[["results"]][["Positive"]][1]  

(for column 1 in case you have three-leveled factors like in the current post)

Therefore, we add the following lines of code to print the percentage labels for both columns var1 and var2 through geom_text:

geom_text(label=paste0(c(round(likert_scores[["results"]][["Negative"]][1]), round(likert_scores[["results"]][["Okay"]][1]), 
                         round(likert_scores[["results"]],[["Positive"]][1]),round(likert_scores[["results"]][["Negative"]][2]), round(likert_scores[["results"]][["Okay"]][2]), 
                         round(likert_scores[["results"]][["Positive"]][2])), "%"), position = position_stack(vjust= .5))

but we also need to change the parameters to not allow likert.plot() to print the original labels in order to not overlap with the new labels. Altogether, this is my current solution:

p <- plot(likert_scores, 
          low.color="#ED5949", 
          neutral.color="#F3CA71", 
          high.color="#7CB166", 
          centered= FALSE, 
          plot.percents=FALSE, 
          plot.percent.low=FALSE, 
          plot.percent.high=FALSE, 
          plot.percent.neutral = FALSE) +
     geom_text(position = position_stack(vjust= .5),
               label=paste0(
                            c(round(likert_scores[["results"]][["Negative"]][1]), 
                              round(likert_scores[["results"]][["Okay"]][1]), 
                              round(likert_scores[["results"]][["Positive"]][1]),
                              round(likert_scores[["results"]][["Negative"]][2]), 
                              round(likert_scores[["results"]][["Okay"]][2]),
                              round(likert_scores[["results"]][["Positive"]][2])), 
                            "%"))

And the new labels are in place!

enter image description here

Upvotes: 2

Related Questions