jbkunst / highcharter

R wrapper for highcharts
http://jkunst.com/highcharter/
Other
719 stars 148 forks source link

Would like a way to group/aggregate like ggplot's stat_summary allows.... #281

Closed d8aninja closed 7 years ago

d8aninja commented 7 years ago

Trying to better understand last answer here (https://github.com/jbkunst/highcharter/issues/156)...

I have hc_xAxis set to an hourly category, and hc_yAxis set to a count of flows (which I set with data in hc_add_series). Problem is there are multiple obs per (direction) category for each hour, so as it is right now I have an hc_xAxis that has "1 1 1 1 1 2 2 2 2 3 3 3 3 3 ...." for the time across X axis. Futhermore, the hover tip is showing its "X" as its ID, not it's "hour" value....Very confused.

d8aninja commented 7 years ago

Code is as follows:

output$flows_plot <- renderHighchart({
    hc <- highchart() %>%
      hc_add_series(data = partitionFiltered()$n_flows, 
                    type = input$plot_type,
                    name = "NetFlows, by Hour",
                    showInLegend = TRUE) %>%
      hc_yAxis(title = list(text = "NetFlows"), 
               allowDecimals = FALSE) %>%
      hc_xAxis(categories = sort(partitionFiltered()$updatedTimeHour),
               tickmarkPlacement = "on",
               opposite = FALSE) %>%
      hc_title(text = "NetFlows, by Hour",
               style = list(fontWeight = "bold")) %>% 
      hc_subtitle(text = 'This has taken me all day.') %>%
      # hc_tooltip(valueDecimals = 4,
                 # pointFormat = "Day: {point.x} <br> Diff: {point.y}") %>%
      hc_credits(enabled = TRUE,
                 text = "Sources: DS Enforcement Team / PWE Security.",
                 style = list(fontSize = "10px"))
    if (input$theme != "No theme") {
      theme <- switch(input$theme,
                      db = hc_theme_db(),
                      darkunica = hc_theme_darkunica(),
                      fivethirtyeight = hc_theme_538(),
                      gridlight = hc_theme_gridlight(),
                      flat = hc_theme_flat(),
                      economist = hc_theme_economist(),
                      sandsignika = hc_theme_sandsignika()
      )
      hc <- hc %>%
        hc_add_theme(theme)
    }
    hc
  })
d8aninja commented 7 years ago

What I'm looking for, given by ggplot:

output$flows_plot <- renderPlot({
    ggplot(data = partitionFiltered()) +
      facet_grid(factor(isEncrypted) ~ factor(direction), scales = 'free') +
      ggthemes::theme_fivethirtyeight(base_size = 14) +
      stat_summary(aes(x = updatedTimeHour, y = n_flows, color = factor(direction)), 
                   # color = 'black', 
                   alpha = 0.6,
##################################
#  imho this is the key request: #
##################################
                   fun.y = function(x) sum(x), 
                   geom = "line") +
      scale_y_continuous(labels=function(x) x/1000000) +
      labs(
        x = 'Hour of Day', 
        y = 'Flows (Millions)',
        title = paste(sprintf('Number of Flows, by Hour [Encrypted X Direction]'))
      )
  })
jbkunst commented 7 years ago

Hi, can you provide a reproducible example please? Do you can plot what you want using some data example in R? Have you see https://cran.r-project.org/web/packages/highcharter/vignettes/charting-data-frames.html or http://jkunst.com/highcharter/hchart.html?

One approach to make a stat_summary layer would be precalculate the summary (a data frame result) and add it using hc_add_series