简体   繁体   中英

HighcharteR: why does hc_series() not show my categorical data?

I use a lot of HighcharteR but somehow got myself in a pickle. Consider the following example:

library(highcharter)

list1 <- list(
          name = "series 1",
          data = list_parse(
                   data.frame("x" = c(1,2,3),
                              "y" = c(4,5,6),
                              "z" = c(1.5,1.2,0.5)))

)

highchart() %>%
  hc_chart(type = "bubble") %>%
  hc_series(list1)

When I run this, all is well -- yet when I run the same graph but using categorical data for both x and y , nothing is displayed:

list2 <- list(
          name = "series 1",
          data = list_parse(
                   data.frame("x" = c("Q1","Q2","Q3"),
                              "y" = c("factor1","factor2","factor3"),
                              "z" = c(1.5,1.2,0.5)))
)

highchart() %>%
  hc_chart(type = "bubble") %>%
  hc_xAxis(type = "category") %>%
  hc_yAxis(type = "category") %>%
  hc_series(list2)

I tried explicitly setting type = "category" for both axes (though I think that one looks for a name value in each series), as well as explicitly setting categories using categories = c("Q1","Q2","Q3") , but neither change anything...

It's not giving me any error message so I'm not sure what's happening here... apologies if the solution is an obvious one (I, for one, don't see it after trying for an hour.). Thanks!

One way to approach this is to keep x and y numeric and then add categories. Categories will be assigned starting from 0, so 0,1,2 in your case, but you can work around that by adding an empty category at 0 and setting the min to 1 and max to 3

list2 <- list(
  name = "series 1",
  data = list_parse(
    data.frame("x" = c(1, 2, 3),
               "y" = c(1, 2, 3),
               "z" = c(1.5,1.2,0.5)))
)

highchart() %>%
  hc_chart(type = "bubble") %>%
  hc_series(list2) %>% 
  hc_xAxis(categories = c("", "Q1","Q2","Q3"),
           min = 1, max = 3) %>% 
  hc_yAxis(categories = c("", "factor1", "factor2", "factor3"),
           min = 1, max = 3)

在此处输入图像描述

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