使用多个面板时如何向 HH::likert() 添加百分比值?

问题描述

我看过这个问题,它向我展示了如何将百分比标签添加到图中( likert plot showing percentage values).

但是,我似乎无法让它与多面板图一起工作。

#my hacky approach to add some custom labels
my_panel_func(...) <- {

#panel.likert(...) #If commented out returns no bars,if uncommented only returns two bars rather than 5

DF <- data.frame(correctX = rep(c(-50,75),each = 5),abs = rep(c(-50,perc = c(c(4.1,2.9,5,3.2,5.9),c(35.6,15.6,47.2,28.2,47.9),c(60.3,81.5,47.9,68.7,46.2)),y =  rep(5:1,times = 3))
  panel.text(x = DF$correctX,y = DF$y,label = paste0(DF$perc,'%'),cex=0.7)
}

这会添加我想添加到图表中的百分比标签

enter image description here

但是,我正在处理具有多个面板 see here 的情节。

我很确定我需要以某种方式编辑 panel.likert 位。

#示例数据

example_data <- data.frame(c(51232212,31321,3124,3132,3212),c(93123,3123,1232316,1239,1230),c(3723132,12314577,2231320,26232131,113211
),c(43235,211236,23119,321365,72130),c(13255,63120,9513,111,4413),c(3029,101320,212309,161230,113249),c(271322,2163,209132,1200,173122),type = c("All/nSurvey Responses","A","S","S"),attribute = c("","B","C","C"))

HH::likert(
    attribute ~ . | type,as.percent = TRUE,ReferenceZero = 4,between = list(y = 0),data = example_data,layout = c(1,3),h.resizePanels = c(1,2,2),panel = my_panel_func)

解决方法

不方便使用图表格式,而是尝试使用 formattable 包 抱歉,我无法重现您的函数未运行的答案,但我会尝试两种方法:

###approach #1###
###changing the format in the data frame
library(formattable) 

DF$perc <-percent(DF$perc,digits = 1)

####not sure if it changes the number or interprets the number you may have to add in * 100 ###

###method 2 use the same function as above but nest it into the chart function###

attribute ~ percent(perc,1) .