apexcharter/inst/examples/timeseries.R

92 lines
2.3 KiB
R
Raw Normal View History

2018-09-03 23:52:53 +02:00
# ------------------------------------------------------------------------
#
# Title : Timeseries with apexcharter
# By : Victor
# Date : 2018-09-03
#
# ------------------------------------------------------------------------
# Packages ----------------------------------------------------------------
library(apexcharter)
library(ggplot2) # data
# Data --------------------------------------------------------------------
data("economics", package = "ggplot2")
# One serie (class Date) --------------------------------------------------
apexcharter() %>%
ax_chart(type = "area", zoom = list(enabled = TRUE)) %>%
ax_plotOptions(line = list(curve = "smooth")) %>%
ax_dataLabels(enabled = FALSE) %>%
ax_series(list(
name = "personal savings rate",
data = parse_df(economics[, c("date", "psavert")])
)) %>%
ax_markers(size = 0, style = "full") %>%
ax_title(text = "US economic time series", align = "left") %>%
ax_subtitle(text = "Data from ggplot2") %>%
ax_fill(gradient = list(
enabled = TRUE,
shadeIntensity = 1,
inverseColors = FALSE,
opacityFrom = 0,
opacityTo = 1,
stops = c(0, 2000)
)) %>%
ax_yaxis(
min = 0, max = 20,
tickAmount = 4,
labels = list(
formatter = htmlwidgets::JS("function(val) {return val.toFixed(0);}")
),
title = "Personal savings rate"
) %>%
ax_xaxis(type = "datetime", labels = list(format = "d MMM yy"))
# Two series (Date) -------------------------------------------------------
apexcharter() %>%
ax_chart(type = "line", zoom = list(enabled = TRUE)) %>%
ax_plotOptions(line = list(curve = "smooth")) %>%
ax_dataLabels(enabled = FALSE) %>%
ax_series(
list(
name = "personal savings rate",
data = parse_df(economics[, c("date", "psavert")])
),
list(
name = "median duration of unemployment, in weeks,",
data = parse_df(economics[, c("date", "uempmed")])
)
) %>%
ax_markers(size = 0, style = "full") %>%
ax_title(text = "US economic time series", align = "left") %>%
ax_subtitle(text = "Data from ggplot2") %>%
ax_yaxis(
min = 0, max = 30,
tickAmount = 4,
labels = list(
formatter = htmlwidgets::JS("function(val) {return val.toFixed(0);}")
),
title = "Personal savings rate"
) %>%
ax_xaxis(type = "datetime", labels = list(format = "d MMM yy"))