Having trouble mapping highcharter aesthetics to r

2019-09-06 12:38发布

问题:

I have a large shiny app that allows users to filter through an API and spark table aggregate (dumped to an .Rdata) simultaneously using the same set of initially selectized parameters. Fitting all this into a reproducible example is going to be tough, but, this is the function that is grouping and summming my metric of interest (try to resist asking me to paste in partitionFiltered()):

df <- reactive({partitionFiltered() %>%
      dplyr::group_by(updatedTimeHour, direction) %>%
      dplyr::mutate(count_dir = sum(n_flows)) %>%
      dplyr::ungroup() %>%
      dplyr::select(updatedTimeHour, direction, count_dir) %>%
      dplyr::arrange(updatedTimeHour) %>% 
      unique()})

(Eventually, updatedTimeHour and direction will be replaced by input$periodicity and input$dimension, respectively, but that is beyond the scope of this question.)

The df() object looks like:

updatedTimeHour direction   count_dir
6               1           525071.00
6               2           3491.00
6               0           498816.00
6               3           5374.00
7               2           2432.00
7               0           303818.00
7               1           340768.00
7               3           4852.00
8               1           1969048.00

My highcharter calls do not seem to be grouping and coloring the aesthetics as I would expect:

        hc <- highchart() %>% 
              hc_add_series(data = df()$count_dir, 
              type = input$plot_type,
              name = factor(df()$direction)
              showInLegend = TRUE,
              # ??group = df()$direction,
              # ??color = df()$direction,
              # ??x = df()$updatedTimeHour, y = df()$count_dir, color = df()$direction,
              # ??hcaes(x = df()$updatedTimeHour, y = df()$count_dir, color = df()$direction)
              ) %>%
              hc_xAxis(type = 'datetime',
                       # ??group = factor(df()$direction),
                       categories = df()$updatedTimeHour,
                       tickmarkPlacement = "on",
                       opposite = FALSE) %>%
              hc_title(text = "NetFlows, by Hour",
                       style = list(fontWeight = "bold")) %>% 
              hc_exporting(enabled = TRUE, filename = "threat_extract")

As you can probably tell, I'm very confused about where/how to map the x-grouping udpatedTimeHour, or color the different direction levels appropriately and make sure their group ends up mapped correctly to the labels in the legend and hover.

I have also attempted to map these aesthetics using the hcaes() call I see included as an argument to hc_add_series() in some of the documentation, but I get errors thrown saying that that argument is not (any longer?) named in that hc_ function...

Any help is appreciated, and a related question is here.

回答1:

You are trying to add as one series multiple objects that's the reason why is not working. Just changing a little bit your code and using the "magic" function hchart it should work:

df = data_frame(updatedTimeHour = c(6,6,6,6,7,7,7,7,8), direction = c(1,2,0,3,2,0,1,3,1), count_dir = rnorm(9))
type = "line"
hchart(df, type, hcaes(x = updatedTimeHour, y = count_dir, group = as.factor(direction))) %>% 
   hc_title(text = "NetFlows, by Hour",
       style = list(fontWeight = "bold")) %>% 
   hc_exporting(enabled = TRUE, filename = "threat_extract")