Highcharter - Line Plots

Line plots a very common type of plots specially in finance domain. These plots are useful for viewing the variation in a numerical attribute over a time period.

Author

Affiliation

Umesh Narayanappa

 

Published

Nov. 12, 2020

DOI

What is highcharter?

Highcharter is a R wrapper for Highcharts javascript library and its modules. Highcharts is very flexible and customizable javascript charting library and it has a great and powerful API.

Line plot

Line plots a very common type of plots specially in finance domain. These plots are useful for viewing the variation in a numerical attribute over a time period.

Data View

We are going to use this dataset for plotting different variations of the scatterplots

library(tidyverse)
library(highcharter)
library(DT)

data(economics_long, package = "ggplot2")

economics_long2 <- dplyr::filter(economics_long, variable %in% c("pop", "uempmed", "unemploy"))

DT::datatable(economics_long2)

Default Line Plot

hchart(
  dplyr::filter(economics_long, variable %in% c("unemploy")),
  "line",
  hcaes(x=date,
        y=value01)
)
Created with Highcharts 8.1.2datevalue01197019751980198519901995200020052010201500.20.40.60.811.2

Changing the color of the line

hchart(
  dplyr::filter(economics_long, variable %in% c("unemploy")),
  "line",
  hcaes(x=date,
        y=value01),
  color = "#fca311"
)
Created with Highcharts 8.1.2datevalue01197019751980198519901995200020052010201500.20.40.60.811.2

Plotting two numerical variables over time

hchart(
  dplyr::filter(economics_long, variable %in% c("pop","unemploy")),
  "line",
  hcaes(x=date,
        y=value01,
        group = variable),
  color = c("#00296b","#fca311")
)
Created with Highcharts 8.1.2datevalue01popunemploy197019751980198519901995200020052010201500.20.40.60.811.2

Plotting three numerical variables over time

hchart(
  dplyr::filter(economics_long, variable %in% c("pop","unemploy", "uempmed")),
  "line",
  hcaes(x=date,
        y=value01,
        group = variable),
  color = c("#00296b","#fca311","#177e89")
)
Created with Highcharts 8.1.2datevalue01popuempmedunemploy197019751980198519901995200020052010201500.20.40.60.811.2

Legend - Horizontal Bottom Right

hchart(
  dplyr::filter(economics_long, variable %in% c("pop","unemploy", "uempmed")),
  "line",
  hcaes(x=date,
        y=value01,
        group = variable),
  color = c("#00296b","#fca311","#177e89")
) %>%
  hc_legend(
    layout = "horizontal",
    align = "right",
    verticalAlign = "bottom"
    
  )
Created with Highcharts 8.1.2datevalue01popuempmedunemploy197019751980198519901995200020052010201500.20.40.60.811.2

Legend - Horizontal Bottom Right

hchart(
  dplyr::filter(economics_long, variable %in% c("pop","unemploy", "uempmed")),
  "line",
  hcaes(x=date,
        y=value01,
        group = variable),
  color = c("#00296b","#fca311","#177e89")
) %>%
  hc_legend(
    layout = "horizontal",
    align = "left",
    verticalAlign = "bottom"
    
  )
Created with Highcharts 8.1.2datevalue01popuempmedunemploy197019751980198519901995200020052010201500.20.40.60.811.2

Legend - Horizontal Top Right

hchart(
  dplyr::filter(economics_long, variable %in% c("pop","unemploy", "uempmed")),
  "line",
  hcaes(x=date,
        y=value01,
        group = variable),
  color = c("#00296b","#00509d","#fdc500")
) %>%
  hc_legend(
    layout = "horizontal",
    align = "right",
    verticalAlign = "top"
    
  )
Created with Highcharts 8.1.2datevalue01popuempmedunemploy197019751980198519901995200020052010201500.20.40.60.811.2

Legend - Horizontal Top Left

hchart(
  dplyr::filter(economics_long, variable %in% c("pop","unemploy", "uempmed")),
  "line",
  hcaes(x=date,
        y=value01,
        group = variable),
  color = c("#00296b","#00509d","#fdc500")
) %>%
  hc_legend(
    layout = "horizontal",
    align = "left",
    verticalAlign = "top"
    
  )
Created with Highcharts 8.1.2datevalue01popuempmedunemploy197019751980198519901995200020052010201500.20.40.60.811.2

Legend - Vertical Top Right

hchart(
  dplyr::filter(economics_long, variable %in% c("pop","unemploy", "uempmed")),
  "line",
  hcaes(x=date,
        y=value01,
        group = variable),
  color = c("#00296b","#00509d","#fdc500")
) %>%
  hc_legend(
    layout = "vertical",
    align = "right",
    verticalAlign = "top"
    
  )
Created with Highcharts 8.1.2datevalue01popuempmedunemploy197019751980198519901995200020052010201500.20.40.60.811.2

Plotting more numerical variables over time

hchart(
  economics_long,
  "line",
  hcaes(x=date,
        y=value01,
        group = variable),
  color = c("#00296b","#fca311","#177e89", "#8f2d56", "#6d597a")
)
Created with Highcharts 8.1.2datevalue01pcepoppsavertuempmedunemploy197019751980198519901995200020052010201500.20.40.60.811.2

Credits