I'm trying to put labels for each value within each sector in a pie chart with ggplot2
My data:
simulpie1 <- data.frame(
segtos = c("Perfil 1", "Perfil 2", "Perfil 3", "Perfil 4"),
percents = c(26, 34, 30, 10)
)
The colors for each sector:
c("steelblue", "magenta", "orange", "lightgreen")
So...
26 - steelblue
34 - magenta
30 - orange
10 - lightgreen
I keep the order with this function:
simulpie1$segtos <- factor(simulpie1$segtos, levels = simulpie1$segtos)
And I used this argument (inside of geom_label()
) to try to center the values to the center of each sector:
position = ggplot2::position_stack(vjust = .5)
But, this result:
My plot code:
ggplot2::ggplot(data = simulpie1, ggplot2::aes(x = "", y = percents, fill = segtos))
ggplot2::geom_col(color = "black")
ggplot2::coord_polar(theta = "y")
ggplot2::scale_fill_manual(values = c("steelblue", "magenta", "orange", "lightgreen"))
ggplot2::geom_label(
ggplot2::aes(label = scales::number(
percents, suffix = "%", accuracy = .1
)
),
fill = "#f2f2f2",
colour = "#020202",
size = 6,
fontface = "bold",
position = ggplot2::position_stack(vjust = .5)
)
ggplot2::theme(
legend.title = ggplot2::element_text(face = "bold"),
legend.box.spacing = ggplot2::unit(-.7, "cm"), # distance between plot-legend
legend.spacing.y = ggplot2::unit(.2, 'cm'), # distance between legend.title and all labels
legend.key.size = ggplot2::unit(.5, "cm"), # size of labels
legend.title.align = 0.5, # align legend.title
axis.title = ggplot2::element_blank(),
axis.text = ggplot2::element_blank(),
axis.ticks = ggplot2::element_blank(),
panel.grid = ggplot2::element_blank(),
panel.background = ggplot2::element_rect(fill = "#f2f2f2", colour = "#f2f2f2"),
plot.background = ggplot2::element_rect(fill = "#f2f2f2", colour = "#f2f2f2"),
legend.background = ggplot2::element_rect(fill = "#f2f2f2", colour = "#008cba")
)
# distance between legend.title and all labels
ggplot2::guides(fill = ggplot2::guide_legend(byrow = TRUE))
ggplot2::labs(fill = "Segmentos")
I would like the values to be centered in their respective sectors (colors).
CodePudding user response:
As quite often the issue is the grouping. For the geom_col
the grouping and the order of "stack" aka the slices of your pie are defined by the fill
aes. However, for the geom_label
you are overriding the grouping by setting the fill
as an argument. Hence, to achieve the same grouping and to align the labels with the "slices" you have to be more explicit by mapping segtos
on the group
aes in geom_label
:
library(ggplot2)
ggplot(data = simulpie1, aes(x = "", y = percents, fill = segtos))
geom_col(color = "black")
coord_polar(theta = "y")
scale_fill_manual(values = c("steelblue", "magenta", "orange", "lightgreen"))
geom_label(
aes(label = scales::number(
percents,
suffix = "%", accuracy = .1
), group = segtos),
fill = "#f2f2f2",
colour = "#020202",
size = 6,
fontface = "bold",
position = position_stack(vjust = .5)
)
theme(
legend.title = element_text(face = "bold"),
legend.box.spacing = unit(-.7, "cm"), # distance between plot-legend
legend.spacing.y = unit(.2, "cm"), # distance between legend.title and all labels
legend.key.size = unit(.5, "cm"), # size of labels
legend.title.align = 0.5, # align legend.title
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
panel.background = element_rect(fill = "#f2f2f2", colour = "#f2f2f2"),
plot.background = element_rect(fill = "#f2f2f2", colour = "#f2f2f2"),
legend.background = element_rect(fill = "#f2f2f2", colour = "#008cba")
)
# distance between legend.title and all labels
guides(fill = guide_legend(byrow = TRUE))
labs(fill = "Segmentos")