Cet article décrit comment créer un graphique en camembert interactif dans R en utilisant le package R highcharter.
Sommaire:
Chargement des packages R requis
# Charger les packages R requis
library(dplyr)
library(highcharter)
# Définir les options de highcharter
options(highcharter.theme = hc_theme_smpl(tooltip = list(valueDecimals = 2)))
Préparation des données
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
Graphique linéaire polaire
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
Bar plot polaire
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
Combiner les lignes et les bars en coordonnées polaires
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
Bar plot de plage de données en coordonnées polaires
Ajouter les plages de valeurs de y dans les données:
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
Créer le graphique:
hc <- highchart() %>%
hc_chart(type = "columnrange", polar = TRUE) %>%
hc_xAxis(categories = df$name) %>%
hc_add_series(df, name = "Fruit Consumption", showInLegend = FALSE)
hc
Version: English
No Comments