I have a 100% stacked bar chart with variable width bars, but I would like for there to be no space gaps between the bars or overlap between the bars. Here is my sample data and output without the scale_x_continuous line.
library(ggplot2)
df <- data.frame(x = c("A","A","B","B","C","C"), group = c("L","R","L","R","L","R"),width = c(0.1,0.1,0.50,0.50,0.40,0.40), y = c(0.75,0.25,0.5,0.5,0.9,0.1))
pos <- 0.5 * (cumsum(df$width) + cumsum(c(0, df$width[-length(df$width)])))
g <-ggplot(df, aes(x = x, y = y, width = width*2, fill = group)) +
geom_bar(stat = "identity", color = "white" ) +
scale_x_continuous(breaks = pos)
g
I have referenced the following threads and wasn't able to get the solutions to work for my scenario:
How to make variable bar widths in ggplot2 not overlap or gap
Spineplots in ggplot or at least vertical labels
The 1st link isnt using a stacked chart, but solves for the gap issue on a standard bar chart. The 2nd link is exactly what I am looking for, but I cant recreate the solution with my sample data. Am I missing something obvious?