3

我已经看过这个问题了,它向我展示了如何将百分比标签添加到图中( 显示百分比值的李克特图)。

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

library(HH)

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

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

DF <- data.frame(correctX = rep(c(-50, 0, 75), each = 5),
             abs = rep(c(-50, 0, 75), each = 5),
             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)
}

这会添加我想要添加到图表中的百分比标签在此处输入图像描述

但是,我正在处理一个包含多个面板的图,请参见此处

我很确定我需要以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", "A", "S", "S"), attribute = c("", "B", 
"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)
4

1 回答 1

0

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

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




于 2021-08-11T20:39:25.637 回答