Highcharter R Package Essentials for Easy Interactive Graphs

Highchart Interactive Pie Chart and Alternatives in R

This article describes how to create an interactive pie chart in R using the highcharter R package.



Contents:

Loading required R package

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

Data preparation

df <- data.frame(
        x = c(0, 1, 2, 3, 4),
        y = c(10, 19.4, 21.1, 14.4, 6.4),
        name = as.factor(c("grape", "olive", "guava", "nut", "pear"))
)
df
##   x    y  name
## 1 0 10.0 grape
## 2 1 19.4 olive
## 3 2 21.1 guava
## 4 3 14.4   nut
## 5 4  6.4  pear

Pie chart

hc <- df %>%
  hchart(
    "pie", hcaes(x = name, y = y),
    name = "Fruit consumption"
    )
hc

Polar line chart

hc <- highchart() %>%
  hc_chart(type = "line", polar = TRUE) %>% 
  hc_xAxis(categories = df$name) %>% 
  hc_series( list(
    name = "Fruit Consumption",
    data = df$y,
    pointPlacement = "on",
    type = "line",
    color = "steelblue",
    showInLegend = FALSE
    ))
hc

Polar column

hc <- highchart() %>%
  hc_chart(type = "column", polar = TRUE) %>% 
  hc_xAxis(categories = df$name) %>% 
  hc_series(list(
     name = "Fruit Consumption",
     data = df$y,
     colorByPoint = TRUE,
     type = "column",
     colors = c("#d35400", "#2980b9", "#2ecc71", "#f1c40f", "#2c3e50"),
     showInLegend = FALSE
   )
   )
hc

Combine polar line and polar column charts

hc <- highchart() %>%
  hc_chart(polar = TRUE) %>% 
  hc_xAxis(categories = df$name) %>% 
  hc_series(
   list(
     name = "Fruit Consumption",
     data = df$y,
     colorByPoint = TRUE,
     type = "column",
     colors = c("#d35400", "#2980b9", "#2ecc71", "#f1c40f", "#2c3e50"),
     showInLegend = FALSE
   ),
   list(
    name = "Fruit Consumption",
    data = df$y,
    pointPlacement = "on",
    type = "line",
    color = "steelblue",
    showInLegend = FALSE
    )
   ) 
hc

Polar Column range

Add y value ranges into the data:

variation  <- c(7.6, 4.3, 17.6, 2.7, 3.3)
df <- df %>% 
  mutate(
    low = y - variation,
    high = y + variation,
    color = c("#d35400", "#2980b9", "#2ecc71", "#f1c40f", "#2c3e50")
  )
df
##   x    y  name  low high   color
## 1 0 10.0 grape  2.4 17.6 #d35400
## 2 1 19.4 olive 15.1 23.7 #2980b9
## 3 2 21.1 guava  3.5 38.7 #2ecc71
## 4 3 14.4   nut 11.7 17.1 #f1c40f
## 5 4  6.4  pear  3.1  9.7 #2c3e50

Create the plot:

hc <- highchart() %>%
  hc_chart(type = "columnrange", polar = TRUE) %>% 
  hc_xAxis(categories = df$name) %>% 
  hc_add_series(df, name = "Fruit Consumption", showInLegend = FALSE)
hc



Version: Français

Highchart Interactive Highstock Plot in R (Prev Lesson)
(Next Lesson) Highchart Interactive Time Series Data Visualization in R
Back to Highcharter R Package Essentials for Easy Interactive Graphs

Comment ( 1 )

  • Munish kohli

    Nice article. Anyway you can help in providing code or hints in how to plot semi circle Pie chart in Shiny/R

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