简体   繁体   English

R highcharter - 分组类别 - 仅具有一个值的组缺少标签

[英]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.就像 Kamil Kulig 提到的那样,您可以尝试将类别设为数组而不是向量,它对我有用。 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即使您不使用分组类别而只是想重命名刻度标签,也需要使用array()函数将向量转换为数组,即

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:如果您仅使用categories = rownames(mtcars)[1])而不是将其转换为数组,则 x 轴标签将无法正确显示:

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

Here the tick label is just M instead of Mazada RX4 .这里的刻度标签只是M而不是Mazada RX4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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