0

R Code:

setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) 
options(stringsAsFactors = FALSE)
rm(list = ls()) 
if (!require("pacman")) install.packages("pacman")
pacman::p_load("dplyr","tidyr","highcharter")

raw_data <- read.csv("results.csv")
DT <- data.table(raw_data)

cols <- c('Person','ABC_Capability','ABC_Sub.capability','Leadership.Facet','Facet.Score')
DT <- DT[, cols, with = FALSE]
names(DT) <- c('Person','Capability','Sub_Capability','SVL','Facet_Score')

DT <- dcast(DT, Capability + Sub_Capability + SVL ~ Person, 
            value.var = c('Facet_Score'))

DT1 <- DT %>% 
  group_by(name = Sub_Capability) %>% 
  do(categories = .$SVL) %>% 
  list_parse()


highchart() %>% 
  hc_chart(type = "bar") %>% 
  hc_title(text = "Some Title") %>%
  hc_add_series(name="A", data = DT$Joan) %>% 
  hc_add_series(name="B", data = DT$Shane) %>%
  hc_add_series(name="C", data = DT$Simon) %>%
  hc_xAxis(categories = DT1)

Output:

enter image description here

I tried using a smaller dataset and realized every time there is a single value in a group. The label gets truncated. For example: Develops people > Empowering

Any help would be appreciated.

Urvah Shabbir
  • 945
  • 2
  • 15
  • 46
  • I don't know much about R, but it seems that it's the same issue as presented in this demo: https://jsfiddle.net/BlackLabel/eLf0rn2q/ If you have only one category on the second level you should remember about representing it as array with only one element. If you pass a String, JS will interpret it as an array of characters. – Kamil Kulig Jun 01 '18 at 10:42

1 Answers1

0

Like Kamil Kulig mentioned, you can try making the categories an array instead of vector and it worked for me. Using the sample code you provided, it would be:

DT1 <- DT %>% 
  group_by(name = Sub_Capability) %>% 
  # store SVL as array
  do(categories = array(.$SVL)) %>% 
  list_parse()

New Update:

The vector needs to be converted to an array using the array() function even if you are not using grouped categories but simply wanted to rename the tick labels i.e.

highcharter::hchart(mtcars[1, ],
                    "column", name = "MPG",
                    highcharter::hcaes(x = 0, y = mpg),
                    showInLegend = F) %>%
    # x axis format
    highcharter::hc_xAxis(title = list(text ="Car Name"),
                          # relabel x axis tick to the name of the cars
                          categories = array(rownames(mtcars)[1]))

then the axis tick label will display the name of the car.

When car name vector is converted to an array

If you use simply categories = rownames(mtcars)[1]) instead of converting it into an array the x axis label won't display properly:

When car name vector just entered as is

Here the tick label is just M instead of Mazada RX4.

Aaron Situ
  • 11
  • 2