简体   繁体   中英

R highcharter - grouped categories - missing label for group with one value only

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:

在此处输入图片说明

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.

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 ie

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.

当车名向量转换为数组时

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

当汽车名称矢量刚按原样输入时

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM