R HighCharter-无数据可显示

时间:2018-12-04 04:59:34

标签: r r-highcharter

全部,尝试结合使用highchart()函数和add_series-list来构建堆积的柱形图。使用:Highcharter stacked column groupings not using hchart()

大多数情况下,但是当我运行highcharter代码时,最终得到的图是: 正确主题,正确标题和订购类型似乎已被考虑。但是,我最后没有要显示的数据。试图简化并删除第二个列表。这是我所拥有的:

orderTypeBar <- monthSummary %>%
  group_by(OrderType) %>%
  do(monthSummary = list_parse2(.[, c('monthGroup', 'Total')])) %>%
  rename(name = OrderType) %>%
  mutate(OrderType = 'column') %>%
  list_parse()

highchart() %>%
  hc_add_theme(hc_theme_ffx()) %>%
  hc_title(text = "Revenue By Order Type") %>%
  hc_add_series_list(orderTypeBar) %>%
  hc_xAxis(categories = monthSummary$monthGroup) %>%
  hc_plotOptions(series=list(stacking='normal'))

摘要表是使用以下dplyr转换构建的。

monthSummary <- data %>%
  group_by(monthGroup, OrderType) %>%
  summarise(CustomerNumber = n()
            , SalesFulfilled = sum(Fulfilled)
            , SalesFreight = sum(Freight)
            , SalesTax = sum(Tax)
            , ServiceLabor = sum(LaborAmount)
            , ServiceMaterials = sum(MaterialCost)
            , Total = sum(Total)) %>%
  ungroup()

绘制结果: Plot - Empty Data

用于生成数据子集的代码:

test <- tibble::tribble(
    ~monthGroup,     ~OrderType, ~TransActionCount, ~SalesFulfilled, ~SalesFreight, ~SalesTax, ~ServiceLabor, ~ServiceMaterials,   ~Total,
    "2017-01",       "Credit",                4L,            -189,             0,      -3.6,             0,                 0,   -192.6,
    "2017-01",    "Equipment",                9L,           12286,             0,    250.66,             0,                 0, 12536.66,
    "2017-01",   "Networking",                2L,             9.9,             0,         0,             0,                 0,      9.9,
    "2017-01",   "Part Order",                2L,             658,             0,     39.48,             0,                 0,   697.48,
    "2017-01", "Service Call",              190L,               0,             0,         0,       9523.62,            2287.9, 12269.38,
    "2017-01",       "Supply",               76L,        26682.18,             5,   1274.05,             0,                 0, 24639.73
)

1 个答案:

答案 0 :(得分:2)

您需要type = 'column'hc_xAxis(categories = test$monthGroup)

library(tidyverse)
library(highcharter)

test <- tibble::tribble(
  ~monthGroup,     ~OrderType, ~TransActionCount, ~SalesFulfilled, ~SalesFreight, ~SalesTax, ~ServiceLabor, ~ServiceMaterials,   ~Total,
  "2017-01",       "Credit",                4L,            -189,             0,      -3.6,             0,                 0,   -192.6,
  "2017-01",    "Equipment",                9L,           12286,             0,    250.66,             0,                 0, 12536.66,
  "2017-01",   "Networking",                2L,             9.9,             0,         0,             0,                 0,      9.9,
  "2017-01",   "Part Order",                2L,             658,             0,     39.48,             0,                 0,   697.48,
  "2017-01", "Service Call",              190L,               0,             0,         0,       9523.62,            2287.9, 12269.38,
  "2017-01",       "Supply",               76L,        26682.18,             5,   1274.05,             0,                 0, 24639.73
)

orderTypeBar <- test %>%
  group_by(OrderType) %>%
  do(data = list_parse2(.[, c('monthGroup', 'Total')])) %>%
  rename(name = OrderType) %>% 
  mutate(type = 'column') %>% 
  list_parse()

highchart() %>%
  hc_xAxis(categories = test$monthGroup) %>%
  hc_add_series_list(orderTypeBar) %>% 
  hc_add_theme(hc_theme_ffx()) %>%
  hc_title(text = "Revenue By Order Type") %>%
  hc_plotOptions(column = list(
    dataLabels = list(enabled = TRUE),
    stacking = "normal",
    enableMouseTracking = TRUE))