如何在plot.likert中编辑值标签的位置? [英] How to edit the position of value labels in plot.likert?

查看:152
本文介绍了如何在plot.likert中编辑值标签的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分数的数据框 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))

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

,然后将其转换为Likert项,并使用"likert"包中的likert.plot对其进行绘制:

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)

现在,问题在于likert.plot显示边缘值标签不在条形图内.我希望找到一种方法来打印条形内的标签,而不必借助ggplot2从头开始构建堆叠的条形图吗?这可能吗?如果不是,那可以替代吗?

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?

预先感谢.

推荐答案

在尝试自己解决这一问题后,找到了一种解决方法,我在下面发布了此方法,以防其他人希望从中受益.

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]  

(在第1列的情况下,如果您有三级因素,例如当前职位)

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

因此,我们添加以下代码行以通过geom_text为var1和var2列打印百分比标签:

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))

,但是我们还需要更改参数以不允许likert.plot()打印原始标签,以免与新标签重叠.总共,这是我当前的解决方案:

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])), 
                            "%"))

新标签就位!

这篇关于如何在plot.likert中编辑值标签的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆