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

时间:2018-05-30 12:53:36

标签: r highcharts bar-chart r-highcharter

R代码:

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)

输出:

enter image description here

我尝试使用较小的数据集,并在每次组中只有一个值时实现。标签被截断。例如:开发人员&gt;赋权

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

就像Kamil Kulig提到的那样,您可以尝试将 类别 做成 array 而不是 vector 为了我。使用您提供的示例代码,它将是:

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