## Load libraries
library(dplyr)
library(tidyr)
library(ggplot2)
## Simulate raw data
set.seed(123)
plot_data <- data.frame(response = rnorm(10, 100, 30),
system = as.factor(1:10),
group = sample(size = 10,
x = c("G1", "G2", "G3"),
replace = TRUE),
A = round(runif(10, 3, 9), 2),
B = round(runif(10, 1, 5), 2),
C = round(runif(10, 3, 7), 2),
D = round(runif(10, 1, 9), 2))
head(plot_data)
## Basic plot
ggplot(data = plot_data, aes(x = system, y = response))+
geom_pie_glyph(slices = c("A", "B", "C", "D"),
data = plot_data)+
theme_classic()
## Change pie radius using `radius` and border colour using `colour`
ggplot(data = plot_data, aes(x = system, y = response))+
# Can also specify slices as column indices
geom_pie_glyph(slices = 4:7, data = plot_data,
colour = "black", radius = 0.5)+
theme_classic()
## Map radius to a variable
p <- ggplot(data = plot_data, aes(x = system, y = response))+
geom_pie_glyph(aes(radius = group),
slices = c("A", "B", "C", "D"),
data = plot_data, colour = "black")+
theme_classic()
p
## Add custom labels
p <- p + labs(x = "System", y = "Response",
fill = "Attributes", radius = "Group")
p
## Change slice colours
p + scale_fill_manual(values = c("#56B4E9", "#CC79A7",
"#F0E442", "#D55E00"))
##### Stack the attributes in one column
# The attributes can also be stacked into one column to generate
# the plot. This variant of the function is useful for situations
# when the data is in tidy format. See vignette("tidy-data") and
# vignette("pivot") for more information.
plot_data_stacked <- plot_data %>%
pivot_longer(cols = c("A", "B", "C", "D"),
names_to = "Attributes",
values_to = "values")
head(plot_data_stacked, 8)
ggplot(data = plot_data_stacked, aes(x = system, y = response))+
# Along with slices column, values column is also needed now
geom_pie_glyph(slices = "Attributes", values = "values")+
theme_classic()
Run the code above in your browser using DataLab