Modérateur : Groupe des modérateurs
Code : Tout sélectionner
library(tidyverse)
library(cowplot)
mdata <- tibble(
categorie = LETTERS[1:3],
echantillon = c(0.33, 0.47, 0.20),
population = c(0.58, 0.38, 0.04)
) %>% mutate(
cumech = cumsum(lag(echantillon, default = 0)),
cumpop = cumsum(lag(population, default = 0)),
categorie = forcats::fct_rev(factor(categorie))
)
inner_pie <- ggplot(data = mdata) +
geom_col(aes(x = 1, y = echantillon, fill = categorie), colour = "black") +
geom_text(aes(x = 1, y = cumech + echantillon/2, label = categorie), size = 12, hjust = 0.5, vjust = 0.5) +
coord_polar(theta = "y", ) +
scale_fill_viridis_d(begin = 0.2) +
theme_void() +
theme(legend.position = "none")
outer_pie <- ggplot(data = mdata) +
geom_rect(aes(
xmin = 1,
xmax = 1 + population,
ymin = cumech,
ymax = echantillon + cumech,
fill = categorie
), colour = "black") +
xlim(c(0, 2)) +
coord_polar(theta = "y") +
scale_fill_viridis_d(alpha = 0.3) +
theme_void() +
theme(legend.position = "none")
ggdraw() +
draw_plot(outer_pie) +
draw_plot(inner_pie, x = 0.25, y = 0.25, width = 0.5, height = 0.5)
Code : Tout sélectionner
library(tidyverse)
mdata <- tibble(
class = rep(LETTERS[1:3], 2),
y = c(0.58, 0.38, 0.04, 0.33, 0.47, 0.20),
pop = rep(c("Population", "Sample"), each = 3)
) %>%
group_by(pop) %>%
mutate(
cum = cumsum(lag(y, default = 0))
) %>%
ungroup() %>%
mutate(label = ifelse(pop=="Population", class, NA))
Code : Tout sélectionner
ggplot(data = mdata) +
geom_bar(
mapping = aes(x = class, y = y, fill = class, alpha = pop),
stat = "identity",
width = 1,
position = "stack",
colour = "white"
) +
geom_text(
mapping = aes(x = class, y = y + cum/2, label = label, colour = class),
size = 12,
hjust = 0.5,
vjust = 0.5,
show.legend = FALSE
) +
scale_alpha_manual(name = NULL, values = c(0.5, 1)) +
scale_fill_viridis_d(begin = 0.2, end = 0.8) +
scale_colour_viridis_d(begin = 0.2, end = 0.8) +
coord_polar() +
theme_void() +
guides(fill = "none")
Code : Tout sélectionner
mdata[["xmin"]] <- rep(filter(mdata, pop=="Sample")[["cum"]], length(unique(mdata[["pop"]])))
mdata[["xmax"]] <- rep(filter(mdata, pop=="Sample")[["y"]], length(unique(mdata[["pop"]]))) + mdata[["xmin"]]
mdata[["ymin"]] <- c(filter(mdata, pop=="Sample")[["y"]], rep(0, length(filter(mdata, pop=="Sample")[["y"]])))
mdata[["ymax"]] <- mdata[["ymin"]] + mdata[["y"]]
ggplot(data = mdata) +
geom_rect(
mapping = aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = class, alpha = pop),
colour = "white"
) +
geom_text(
mapping = aes(x = (xmin+xmax)/2, y = y + cum/2, label = label, colour = class),
size = 12,
hjust = 0.5,
vjust = 0.5,
show.legend = FALSE
) +
scale_alpha_manual(name = NULL, values = c(0.5, 1)) +
scale_fill_viridis_d(begin = 0.2, end = 0.8) +
scale_colour_viridis_d(begin = 0.2, end = 0.8) +
coord_polar() +
theme_void() +
guides(fill = "none")
Retourner vers « Questions en cours »
Utilisateurs parcourant ce forum : Google [Bot] et 1 invité