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
Recommended for you
This section contains best data science and self-development resources to help you on your path.
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
Nice article. Anyway you can help in providing code or hints in how to plot semi circle Pie chart in Shiny/R