0

As you can see the percentages are on the wrong stacked bar which I'd like to change. I have tried position_fill(reverse = TRUE) however it just wipes out my graph completely.

df2 <- data.frame(table(food$BusinessType, food$classification))
names(df2) <- c('Type', 'Rating', 'Count')
df2

df2 <- ddply(df2, .(Type), transform, percent=Count/sum(Count) *100)
df2 <- ddply(df2, .(Type), transform, pos = (cumsum(Count) - 0.9 * Count))
df2$label <- paste0(sprintf('%.0f', df2$percent), '%')

ggplot(df2, aes(x=Type, y=Count, fill=Rating)) +
  geom_bar(stat = 'identity') +
  geom_text(aes(y = pos, label = label), size =4) +
  ggtitle('Business Type Ratings')

enter image description here

newb1001
  • 23
  • 1
  • 4
  • 5
    It's hard to know why this would be the case without a [reproducible example](https://stackoverflow.com/q/5963269/5325862), but you most likely just need to change the order of factor levels – camille Apr 13 '22 at 14:56
  • 1
    Also worth noting that **plyr** has been deprecated for years and replaced by **dplyr** and the larger **tidyverse** package set. – jdobres Apr 13 '22 at 14:59
  • 1
    Hard to say without a reprex, though try `geom_bar(position_stack(reverse = TRUE))` instead. – glenn_in_boston Apr 13 '22 at 16:11

0 Answers0