Laurence_jj
Laurence_jj

Reputation: 726

How to create a highcharts dot plot graph? - R Highcharter

How can I create a highcharts graph like this using the R highcharter package?

It is a simple count of sectors (instances) above or bellow 0 and coloured to reflect the value.

This may sometime be termed a dot plot (https://ggplot2.tidyverse.org/reference/geom_dotplot.html)?

image from (https://graphics.wsj.com/job-market-tracker/)

enter image description here

Some sample data:

data = data.table(
  CJ(date = seq(as.IDate("2019-01-01"), as.IDate("2019-01-10"), by = "day"),
     group = seq(1,20))
)

data[, value := runif(n=200, -5,5)]

Upvotes: 2

Views: 1361

Answers (1)

Laurence_jj
Laurence_jj

Reputation: 726

This is as far as I got:

library(highcharter)
library(data.table)

data = data.table(
  CJ(date = seq(as.Date("2019-01-01"), as.Date("2019-02-10"), by = "day"),
     group = seq(1,20))
)

# generate random value
data[, value := round(runif(n=dim(data)[1], -5,5),4)]

# categorize it from 1 to 10
data[, cat:=cut(value, breaks=quantile(data[value!=0]$value, seq(0,1,0.1)), labels=seq(1,10))]

# assign colour based on value
colf = colorRampPalette(colors = c("red","yellow", "green"))
cols = colf(10)
data[, color := as.factor(cols[cat])]

# generate x and y
data[, x := datetime_to_timestamp(date)]
data[, y := order(order(value))-sum(value<0), date]

data[, name := group]

highchart() %>%
  hc_chart(type = "scatter") %>%
  hc_xAxis(type = "datetime", dateTimeLabelFormats = list(day = '%d of %b')) %>%
  hc_tooltip(pointFormat = "Performance = <b>{point.value}</b> <br> Group = <b>{point.name}</b>") %>%
  hc_add_theme(hc_theme_flat(chart = list(backgroundColor = "#FFF"))) %>%
  hc_add_series(data, groupPadding=0)

enter image description here

It also works with more points: enter image description here

Upvotes: 2

Related Questions