I'm using geom_textpath
to add labels to a double donut chart but the labels are not aligned to the middle.
How can I align the labels in the middle or the center of each segment?
library(magrittr)
library(ggplot2)
library(geomtextpath)
pie_data <- data.frame(category=c('A','B','C','A','B','C'),
year=c(2020,2020,2020,2021,2021,2021),
sales=c(40,30,20,10,15,10))
pie_data %>% ggplot(aes(x=year,y=sales,fill=category))
geom_col(position='fill',width=1,color='white')
lims(x=c(2018,2023))
geom_textpath(position='fill',angle=90,hjust=2,alpha=1,
aes(color=factor(year),
label=paste0(category,':',sales)))
coord_polar(theta = 'y')
theme_void()
CodePudding user response:
To position the labels when using position="fill"
it's good to know that position_fill
has a argument vjust
to position the labels in the middle of the bars using vjust = .5
:
library(magrittr)
library(ggplot2)
library(geomtextpath)
pie_data <- data.frame(
category = c("A", "B", "C", "A", "B", "C"),
year = c(2020, 2020, 2020, 2021, 2021, 2021),
sales = c(40, 30, 20, 10, 15, 10)
)
pie_data %>% ggplot(aes(x = year, y = sales, fill = category))
geom_col(position = "fill", width = 1, color = "white")
lims(x = c(2018, 2023))
geom_textpath(
position = position_fill(vjust = .5), angle = 90, alpha = 1,
aes(
color = factor(year),
label = paste0(category, ":", sales)
)
)
coord_polar(theta = "y")
theme_void()