Highcharter R Package Essentials for Easy Interactive Graphs

Highchart Interactive Line Plot in R

You will learn how to create an interactive line plot in R using the highchart R package.



Contents:

Loading required R packages

# Load required R packages
library(tidyverse)
library(highcharter) 
# Set highcharter options
options(highcharter.theme = hc_theme_smpl(tooltip = list(valueDecimals = 2)))

Data preparation

We’ll create two data frames derived from the ToothGrowth datasets.

df <- data.frame(dose=c("D0.5", "D1", "D2"),
                len=c(4.2, 10, 29.5))

head(df, 4)
##   dose  len
## 1 D0.5  4.2
## 2   D1 10.0
## 3   D2 29.5
df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
                dose=rep(c("D0.5", "D1", "D2"),2),
                len=c(6.8, 15, 33, 4.2, 10, 29.5))

head(df2, 4)
##   supp dose  len
## 1   VC D0.5  6.8
## 2   VC   D1 15.0
## 3   VC   D2 33.0
## 4   OJ D0.5  4.2
  • len: Tooth length
  • dose: Dose in milligrams (0.5, 1, 2)
  • supp: Supplement type (VC or OJ)

Basic line plots

hc <- df %>% hchart(
  'line', hcaes(x = dose, y = len),
  color = "steelblue"
  ) 
hc

Line plot with multiple groups

hc <- df2 %>% 
  hchart(
  'line', hcaes(x = dose, y = len, group = supp)
  )  %>%
  hc_colors(c("#999999", "#E69F00"))
hc

Line plot with a numeric x-axis

If the variable on x-axis is numeric, it can be useful to treat it as a continuous or a factor variable depending on what you want to do:

# Create some data
df3 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
                dose=rep(c("0.5", "1", "2"),2),
                len=c(6.8, 15, 33, 4.2, 10, 29.5))
head(df3)
##   supp dose  len
## 1   VC  0.5  6.8
## 2   VC    1 15.0
## 3   VC    2 33.0
## 4   OJ  0.5  4.2
## 5   OJ    1 10.0
## 6   OJ    2 29.5

X axis treated as continuous variable:

df3$dose <- as.numeric(as.vector(df3$dose))
hc <- df3 %>%
  hchart('line', hcaes(x = dose, y = len, group = supp)) 
hc

X axis treated as discrete variable:

df3$dose<-as.factor(df3$dose)
hc <- df3 %>% 
  hchart('line', hcaes(x = dose, y = len, group = supp)) 
hc

Line plot with dates on x-axis: Time series

# Economic time series data
# Check automatically if the x column is date class
data(economics_long, package = "ggplot2")
economics_long2 <- economics_long %>%
  dplyr::filter(variable %in% c("pop", "uempmed", "unemploy"))
economics_long2 
## # A tibble: 1,722 x 4
##   date       variable  value value01
##   <date>     <chr>     <dbl>   <dbl>
## 1 1967-07-01 pop      198712 0      
## 2 1967-08-01 pop      198911 0.00164
## 3 1967-09-01 pop      199113 0.00330
## 4 1967-10-01 pop      199311 0.00492
## 5 1967-11-01 pop      199498 0.00646
## 6 1967-12-01 pop      199657 0.00777
## # … with 1,716 more rows
hc <- hchart(
  economics_long2, "line", 
  hcaes(x = date, y = value01, group = variable)
  )
hc

Spline: Line with polynomial interpolation

# Data preparation 
data("diamonds", package = "ggplot2")
df4 <- diamonds %>%
  group_by(cut, color)%>%
  count()

# Spline Plot
hc <- df4 %>% 
  hchart('spline', hcaes(x = 'cut', y = 'n', group = "color"))
hc



Version: Français

Highchart Interactive Boxplot in R (Prev Lesson)
(Next Lesson) Highchart Interactive Bar Plot in R
Back to Highcharter R Package Essentials for Easy Interactive Graphs

No Comments

Give a comment

Want to post an issue with R? If yes, please make sure you have read this: How to Include Reproducible R Script Examples in Datanovia Comments