v_pie | R Documentation |
Create a Pie Chart
v_pie(
vc,
mapping = NULL,
data = NULL,
name = NULL,
label = list(visible = TRUE),
...,
serie_id = NULL,
data_id = NULL
)
vc |
A chart initialized with |
mapping |
Default list of aesthetic mappings to use for chart. |
data |
Default dataset to use for chart. If not already
a |
name |
Name for the serie, only used for single serie (no |
label |
Options for displaying labels on the pie chart. |
... |
Additional parameters for the serie. |
data_id , serie_id |
ID for the data/serie, can be used to further customize the chart with |
A vchart()
htmlwidget
object.
library(vchartr)
# Basic Pie Chart
subset(world_electricity, year == 2023 & type == "total") %>%
vchart() %>%
v_pie(aes(x = source, y = generation))
# Use custom colors
subset(world_electricity, year == 2023 & type == "total") %>%
vchart() %>%
v_pie(aes(x = source, y = generation)) %>%
v_scale_color_manual(c(
"Low carbon" = "#a3be8c",
"Fossil fuels" = "#4C566A"
))
# Customize tooltip
subset(world_electricity, year == 2023 & type == "total") %>%
vchart() %>%
v_pie(aes(x = source, y = generation)) %>%
v_specs_tooltip(
mark = list(
content = list(
list(
key = JS("datum => datum['x']"),
value = JS("datum => Math.round(datum['y']) + ' TWh'")
),
list(
hasShape = FALSE,
key = "Proportion",
value = JS("datum => datum._percent_ + '%'")
)
)
)
)
# Nested Pie Chart
vchart() %>%
v_pie(
data = subset(world_electricity, year == 2023 & type == "total"),
mapping = aes(x = source, y = generation),
outerRadius = 0.65,
innerRadius = 0,
label = list(
visible = TRUE,
position = "inside",
rotate = FALSE,
style = list(fill = "white")
),
pie = list(
style = list(
stroke = "#FFFFFF",
lineWidth = 2
)
)
) %>%
v_pie(
data = subset(world_electricity, year == 2023 & type == "detail"),
mapping = aes(x = source, y = generation),
outerRadius = 0.8,
innerRadius = 0.67,
pie = list(
style = list(
stroke = "#FFFFFF",
lineWidth = 2
)
)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.