Skip to content

Instantly share code, notes, and snippets.

@dantonnoriega
Last active December 15, 2016 18:55
Show Gist options
  • Save dantonnoriega/61af737fcadb962f687b7b453aa77aca to your computer and use it in GitHub Desktop.
Save dantonnoriega/61af737fcadb962f687b7b453aa77aca to your computer and use it in GitHub Desktop.
highcharter version of the example found at http://www.highcharts.com/demo/spline-irregular-time
library(highcharter)
library(tidyverse)
# set up the series data (so many list of lists...)
## dates are one of the toughest things to plot
## the key to understanding this is in the function `highcharter::hc_add_series_time_values`
## in it, you will find how highcharter parses dates, using `highcharter::datetime_to_timestamp`
## which accepts as.Date values, converts to as.POSIXct then to numeric * 1000
series = list(
list(
name = 'Winter 2012-2013',
data = list(
list(x = 1000*as.numeric(ISOdate(1970, 10, 21)), y = 0),
list(x = 1000*as.numeric(ISOdate(1970, 11, 4)), y = 0.28),
list(x = 1000*as.numeric(ISOdate(1970, 11, 9)), y = 0.25),
list(x = 1000*as.numeric(ISOdate(1970, 11, 27)), y = 0.2),
list(x = 1000*as.numeric(ISOdate(1970, 12, 2)), y = 0.28),
list(x = 1000*as.numeric(ISOdate(1970, 12, 26)), y = 0.28),
list(x = 1000*as.numeric(ISOdate(1970, 12, 29)), y = 0.47),
list(x = 1000*as.numeric(ISOdate(1971, 1, 11)), y = 0.79),
list(x = 1000*as.numeric(ISOdate(1971, 1, 26)), y = 0.72),
list(x = 1000*as.numeric(ISOdate(1971, 2, 3)), y = 1.02),
list(x = 1000*as.numeric(ISOdate(1971, 2, 11)), y = 1.12),
list(x = 1000*as.numeric(ISOdate(1971, 2, 25)), y = 1.2),
list(x = 1000*as.numeric(ISOdate(1971, 3, 11)), y = 1.18),
list(x = 1000*as.numeric(ISOdate(1971, 4, 11)), y = 1.19),
list(x = 1000*as.numeric(ISOdate(1971, 5, 1)), y = 1.85),
list(x = 1000*as.numeric(ISOdate(1971, 5, 5)), y = 2.22),
list(x = 1000*as.numeric(ISOdate(1971, 5, 19)), y = 1.15),
list(x = 1000*as.numeric(ISOdate(1971, 6, 3)), y = 0)
)
),
list(
name = 'Winter 2013-2014',
data = list(
list(x = 1000*as.numeric(ISOdate(1970, 10, 29)), y = 0),
list(x = 1000*as.numeric(ISOdate(1970, 11, 9)), y = 0.4),
list(x = 1000*as.numeric(ISOdate(1970, 12, 1)), y = 0.25),
list(x = 1000*as.numeric(ISOdate(1971, 1, 1)), y = 1.66),
list(x = 1000*as.numeric(ISOdate(1971, 1, 10)), y = 1.8),
list(x = 1000*as.numeric(ISOdate(1971, 2, 19)), y = 1.76),
list(x = 1000*as.numeric(ISOdate(1971, 3, 25)), y = 2.62),
list(x = 1000*as.numeric(ISOdate(1971, 4, 19)), y = 2.41),
list(x = 1000*as.numeric(ISOdate(1971, 4, 30)), y = 2.05),
list(x = 1000*as.numeric(ISOdate(1971, 5, 14)), y = 1.7),
list(x = 1000*as.numeric(ISOdate(1971, 5, 24)), y = 1.1),
list(x = 1000*as.numeric(ISOdate(1971, 6, 10)), y = 0)
)
),
list(
name = 'Winter 2014-2015',
data = list(
list(x = 1000*as.numeric(ISOdate(1970, 11, 25)), y = 0),
list(x = 1000*as.numeric(ISOdate(1970, 12, 6)), y = 0.25),
list(x = 1000*as.numeric(ISOdate(1970, 12, 20)), y = 1.41),
list(x = 1000*as.numeric(ISOdate(1970, 12, 25)), y = 1.64),
list(x = 1000*as.numeric(ISOdate(1971, 1, 4)), y = 1.6),
list(x = 1000*as.numeric(ISOdate(1971, 1, 17)), y = 2.55),
list(x = 1000*as.numeric(ISOdate(1971, 1, 24)), y = 2.62),
list(x = 1000*as.numeric(ISOdate(1971, 2, 4)), y = 2.5),
list(x = 1000*as.numeric(ISOdate(1971, 2, 14)), y = 2.42),
list(x = 1000*as.numeric(ISOdate(1971, 3, 6)), y = 2.74),
list(x = 1000*as.numeric(ISOdate(1971, 3, 14)), y = 2.62),
list(x = 1000*as.numeric(ISOdate(1971, 3, 24)), y = 2.6),
list(x = 1000*as.numeric(ISOdate(1971, 4, 2)), y = 2.81),
list(x = 1000*as.numeric(ISOdate(1971, 4, 12)), y = 2.63),
list(x = 1000*as.numeric(ISOdate(1971, 4, 28)), y = 2.77),
list(x = 1000*as.numeric(ISOdate(1971, 5, 5)), y = 2.68),
list(x = 1000*as.numeric(ISOdate(1971, 5, 10)), y = 2.56),
list(x = 1000*as.numeric(ISOdate(1971, 5, 15)), y = 2.39),
list(x = 1000*as.numeric(ISOdate(1971, 5, 20)), y = 2.3),
list(x = 1000*as.numeric(ISOdate(1971, 6, 5)), y = 2),
list(x = 1000*as.numeric(ISOdate(1971, 6, 10)), y = 1.85),
list(x = 1000*as.numeric(ISOdate(1971, 6, 15)), y = 1.49),
list(x = 1000*as.numeric(ISOdate(1971, 6, 23)), y = 1.08)
)
)
)
hc <- highchart() %>%
hc_add_series_list(series)
hc %>%
hc_chart(
type = 'spline'
) %>%
hc_title(
text = 'Snow depth at Vikjafjellet, Norway'
) %>%
hc_subtitle(
text = 'Irregular time data in Highcharts JS'
) %>%
hc_xAxis(
type = 'datetime',
dateTimeLabelFormats = list(
month = '%e. %b',
year = '%b'
),
title = list(
text = 'Date'
)
) %>%
hc_yAxis(
title = list(
text = 'Snow depth (m)'
),
min = 0
) %>%
hc_tooltip(
headerFormat = '<b>{series.name}</b><br>',
pointFormat = '{point.x:%e. %b}: {point.y:.2f} m'
) %>%
hc_plotOptions(
spline = list(
marker = list(
enabled = TRUE
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment