3

I've looked as this question which has shown me how to add the percent labels to the plot ( likert plot showing percentage values).

However, I can't seem to get it working with a multipanel plot.

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

This adds the percentage labels I want to add to my graph enter image description here

However, I'm working on a plot with multiple panels see here.

I'm pretty sure I need to edit the panel.likert bit somehow.

#Example data

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)
Cole
  • 11,130
  • 1
  • 9
  • 24
JCran
  • 375
  • 2
  • 14
  • I got the following error when I tried your "my_panel_func(...)" : "Error in my_panel_func(...) <- { : '...' used in an incorrect context" – Abdur Rohman Aug 11 '21 at 16:34
  • Have you considered using the *package* `likert`? (`likert::likert()`) That method adds the percentages to the plot automatically. They have demos built into the package, so you could at least look and see if it works. Considering why you're asking, grouping is an argument in that function. – Kat Aug 11 '21 at 20:47
  • @Kat, yes and in retrospect would probably have gone with that, but would mean redoing a lot of other graphs to match style. – JCran Aug 12 '21 at 07:39

1 Answers1

0

Not to handy with the chart formatting but try using the formattable package Sorry I cant reproduce the answer your function is not running but I would try 2 approaches:

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




d3hero23
  • 380
  • 1
  • 12