holtzy / D3-graph-gallery

A collection of simple graphics made with D3.js
https://www.d3-graph-gallery.com
MIT License
790 stars 237 forks source link

Circular Stacked Barplots - Labels inside the stacked bars #29

Closed JuanAlonsoBoat closed 4 years ago

JuanAlonsoBoat commented 4 years ago

I have read this great article and Im trying to do my own circular stacked barplot.

I have one problem. I dont know how to add the labels inside the stacked bars in order to create the polar coord afterwards.

I paste the code I took from your article:

Make the plot

p <- ggplot(data) +

Add the stacked bar

geom_bar(aes(x=as.factor(id), y=value, fill=observation), stat="identity", alpha=0.5) +

scale_fill_manual(values=c("#1E8E6B", "#6FBC84", "#FEEF51", "#EF6541", "#E81329")) +

Add a val=100/75/50/25 lines. I do it at the beginning to make sur barplots are OVER it.

geom_segment(data=grid_data, aes(x = end, y = 0, xend = start, yend = 0), colour = "grey", alpha=0, size=0 , inherit.aes = FALSE ) + geom_segment(data=grid_data, aes(x = end, y = 25, xend = start, yend = 25), colour = "grey", alpha=0, size=0 , inherit.aes = FALSE ) + geom_segment(data=grid_data, aes(x = end, y = 50, xend = start, yend = 50), colour = "grey", alpha=0, size=0 , inherit.aes = FALSE ) + geom_segment(data=grid_data, aes(x = end, y = 75, xend = start, yend = 75), colour = "grey", alpha=0, size=0 , inherit.aes = FALSE ) + geom_segment(data=grid_data, aes(x = end, y = 100, xend = start, yend = 100), colour = "grey", alpha=0, size=0 , inherit.aes = FALSE ) +

Add text showing the value of each 100/75/50/25 lines

ggplot2::annotate("text", x = rep(max(data$id),5), y = c(0, 25, 50, 75, 100), label = c("0", "25", "50", "75", "100") , color="grey", size=5 , angle=0, fontface="bold", hjust=0) +

ylim(0,max(label_data$tot, na.rm=T)) + theme_minimal() + theme( legend.position = "none", axis.text = element_blank(), axis.title = element_blank(), panel.grid = element_blank(), plot.margin = unit(rep(-1,4), "cm") ) + coord_polar() +

Add labels on top of each bar

geom_text(data=label_data, aes(x=id, y=tot+10, label=Area, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE ) +

Add base line information

geom_segment(data=base_data, aes(x = start, y = -5, xend = end, yend = -5), colour = "black", alpha=0.8, size=0.6, inherit.aes = FALSE ) + geom_text(data=base_data, aes(x = title, y = -18, label=group), hjust=c(1,1,0,0), colour = "black", alpha=0.8, size=4, fontface="bold", inherit.aes = FALSE)

Best,

Juan

JuanAlonsoBoat commented 4 years ago

I could solve it with an adjustment at the beginning of the script.

ggplot(data, aes(x = as.factor(id), y=value, fill = cat, label = value)) + geom_bar(stat = "identity", alpha=0.5) + geom_text(size = 2.5, position = position_stack(vjust = 0.7)) + scale_fill_manual(values=c("#1E8E6B", "#6FBC84", "#FEEF51", "#EF6541", "#E81329")) +

geom_segment(data=grid_data, aes(x = end, y = 0, xend = start, yend = 0), colour = "grey", alpha=0, size=0 , inherit.aes = FALSE ) + geom_segment(data=grid_data, aes(x = end, y = 25, xend = start, yend = 25), colour = "grey", alpha=0, size=0 , inherit.aes = FALSE ) + geom_segment(data=grid_data, aes(x = end, y = 50, xend = start, yend = 50), colour = "grey", alpha=0, size=0 , inherit.aes = FALSE ) + geom_segment(data=grid_data, aes(x = end, y = 75, xend = start, yend = 75), colour = "grey", alpha=0, size=0 , inherit.aes = FALSE ) + geom_segment(data=grid_data, aes(x = end, y = 100, xend = start, yend = 100), colour = "grey", alpha=0, size=0 , inherit.aes = FALSE ) +

ylim(0,max(label_data$tot, na.rm=T)) + theme_minimal() + theme( legend.position = "none", axis.text = element_blank(), axis.title = element_blank(), panel.grid = element_blank(), plot.margin = unit(rep(-1,4), "cm") ) + coord_polar()