如何编辑 plot.likert 中值标签的位置?

How to edit the position of value labels in 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))

我将其转换为具有三个级别的因子:

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.plot 绘制它:

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 从头开始​​构建堆叠条形图?这可能吗?如果不能,还有什么替代方案?

提前致谢。

经过一天自己尝试解决此问题后,我在下面找到了一个解决方法 post 以防其他人想从中受益。

对于历史,李克特项存储位置中因子水平的比例:

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

(对于第 1 列,如果你有像当前 post 那样的三级因素)

因此,我们添加以下代码行来打印列 var1 和 var2 到 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() 打印原始标签,以免与新标签重叠。总之,这是我目前的解决方案:

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

新标签已经就位!