Highcharter R Package Essentials for Easy Interactive Graphs

Highchart Interactive Density and Histogram Plots in R

You will learn how to create interactive density distribution and histogram plots using the highcharter R package.



Contents:

Loading required R packages

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

Data preparation

Create some data (wdata) containing the weights by sex (M for male; F for female):

set.seed(1234)
wdata = data.frame(
        sex = factor(rep(c("F", "M"), each=200)),
        weight = c(rnorm(200, 55), rnorm(200, 58))
        )

head(wdata, 4)
##   sex weight
## 1   F   53.8
## 2   F   55.3
## 3   F   56.1
## 4   F   52.7

Density plots

Basic density plots

hc <- hchart(
  density(wdata$weight), 
  type = "area", name = "Weight"
  )
hc

Grouped density plots

f <- wdata %>% filter(sex == "F")
m <- wdata %>% filter(sex == "M")
hc <- hchart(
  density(m$weight), type = "area", 
  color = "steelblue", name = "Male"
  ) %>%
  hc_add_series(
    density(f$weight), type = "area",
    color = "#B71C1C", 
    name = "Female"
    )
hc

Histograms

hc <- hchart(
  wdata$weight, 
  color = "#B71C1C", name = "Weight"
  )
hc



Version: Français

Highchart Interactive Bar Plot in R (Prev Lesson)
(Next Lesson) Highchart Interactive Area 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