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 lengthdose
: 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
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Coursera - Online Courses and Specialization
Data science
- Course: Machine Learning: Master the Fundamentals by Stanford
- Specialization: Data Science by Johns Hopkins University
- Specialization: Python for Everybody by University of Michigan
- Courses: Build Skills for a Top Job in any Industry by Coursera
- Specialization: Master Machine Learning Fundamentals by University of Washington
- Specialization: Statistics with R by Duke University
- Specialization: Software Development in R by Johns Hopkins University
- Specialization: Genomic Data Science by Johns Hopkins University
Popular Courses Launched in 2020
- Google IT Automation with Python by Google
- AI for Medicine by deeplearning.ai
- Epidemiology in Public Health Practice by Johns Hopkins University
- AWS Fundamentals by Amazon Web Services
Trending Courses
- The Science of Well-Being by Yale University
- Google IT Support Professional by Google
- Python for Everybody by University of Michigan
- IBM Data Science Professional Certificate by IBM
- Business Foundations by University of Pennsylvania
- Introduction to Psychology by Yale University
- Excel Skills for Business by Macquarie University
- Psychological First Aid by Johns Hopkins University
- Graphic Design by Cal Arts
Amazon FBA
Amazing Selling Machine
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Version: Français
No Comments