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

穿精又带淫゛_ 提交于 2020-04-18 07:13:18

问题


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.


回答1:


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!



来源:https://stackoverflow.com/questions/58487796/how-to-edit-the-position-of-value-labels-in-plot-likert

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!