0

Is there a way to associate dashStyle by group in the highcharter package. I would like the line with 'forecast' as type to be dashed.

library(highcharter)

# Create sample data frame
sample_df <- data_frame(sku = c(rep("12",40)),
                        type = c(rep("actuals",20), rep("forecast",20)),
                        calendar_week = rep(seq(as.Date("2017-09-08"), as.Date("2018-01-23"), by=7),2),
                        units = round(c(rnorm(11, mean=50, sd=10), rep(0, 9), c(rnorm(20, mean=100, sd=10))),0))

# Create colours vector
custom_colours <- c("#4286f4", "#d66048")

# Chart
hchart(sample_df, "line",  hcaes(calendar_week, units, group = type))  %>%
  hc_yAxis(title = list(text = "Units")) %>%  
  hc_xAxis(title = list(text = "Week"), type = "datetime", tickInterval = 7* 24 * 3600 * 1000) %>%
  hc_colors(custom_colours)

Also, is it possible to associate colour with a group, for instance using a mutate with ifelse to create a column with the colours in the dataframe?

tonyk
  • 348
  • 5
  • 22

1 Answers1

2

You can do:

custom_colours <- c("#4286f4", "#d66048")
custom_dashes <- c("Solid", "ShortDashDotDot")

hchart(sample_df, "line",  hcaes(calendar_week, units, group = type),
       color = custom_colours, dashStyle = custom_dashes)  %>%
  hc_yAxis(title = list(text = "Units")) %>%  
  hc_xAxis(title = list(text = "Week"), type = "datetime", tickInterval = 7* 24 * 3600 * 1000)

enter image description here

jbkunst
  • 3,009
  • 1
  • 22
  • 28
  • Thanks! And correct me if I am wrong but it looks like the mapping order is alphabetical by the group? – tonyk Feb 22 '18 at 15:18
  • 1
    Yes if the grouped variable is character, in the case of a factor in the order of the levels (try with `sample_df$type <- factor(sample_df$type, levels = c("forecast", "actuals"))`) – jbkunst Feb 22 '18 at 16:32