I have made a chart with the following data:
library(dplyr)
library(forcats)
library(ggplot2)
library(scales)
mydf <- tibble(type = c(rep(c(rep("TypeA", 4), rep("TypeB", 4)), 2)),
Ratings = rep(c("Top", "Middle", "Low", "NA"), 4),
Prop = c(0.62, 0.15, 0.15, 0, 0.32, 0.16, 0.47, 0, 0.38, 0.31, 0.31, 0, 0.16, 0.16, 0.63, 0.05),
Question = c(rep("Question1", 8), rep("Question2", 8)))
mydf$Ratings <- factor(mydf$Ratings) %>% fct_inorder() %>% fct_rev()
And here's my code to make the chart:
mydf %>% filter(Prop > 0) %>% ggplot(aes(x = Question, y = Prop, fill = Ratings)) +
geom_bar(position = "fill", stat = "identity") +
geom_text(aes(label = percent(round(Prop,2))), position = position_stack(vjust = 0.5)) +
facet_grid(~ type) + scale_y_continuous(labels = percent) +
guides(fill = guide_legend(reverse = TRUE))
It produced the following chart. I specifically used position = position_stack(vjust = 0.5)
to center the labels halfway through the bars. It clearly doesn't look right for the labels for Question1. Is this a bug? Have I set my data up incorrectly?