I am trying to normalize a stacked plot and put the percentage of each fill in the middle of each fill. This is an easy question, but I don't have a handle on what the y-value is, since it is implicit, so I am not sure how to set: geom_text(aes(y=??, label=paste0(??,"%")))
.
As for normalizing, I have seen people collapse and normalize dt
beforehand, but I was hoping to learn a ggplot-ish way to do it.
Do I just need to convert my dt
to a summary table of time-grpmember percentages? That would give me a direct handle on y-values (e.g. to compute 'middle of each fill').
dt <- as.data.table(read.table(text = "time grpmember
1 TRUE
1 TRUE
1 TRUE
1 FALSE
1 FALSE
1 FALSE
2 FALSE
2 TRUE
2 TRUE
2 TRUE", header=TRUE))
dt$time <- as.factor(dt$time)
ggplot(dt, aes(x=time)) + geom_bar(aes(fill=grpmember)) + geom_text(aes(y = 2, label=paste0("?", "%")))