r - create stack bar with counts and fill/group levels in the same graph -
i trying create stack bar counts , group levels in same graph (inside of plot). since group variable has 25 levels, prefer bring name of group levels inside of plot (since have 25 different colour , difficult visualization).i took "showing data values on stacked bar chart in ggplot2". wondering how can add name of each group level inside of graph.
year <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4)) category <- c(rep(c("a", "b", "c", "d"), times = 4)) frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251) data <- data.frame(year, category, frequency) library(dplyr) data <- group_by(data,year) %>% mutate(pos = cumsum(frequency) - (0.5 * frequency)) library(ggplot2) #plot bars , add text p <- ggplot(data, aes(x = year, y = frequency)) + geom_bar(aes(fill = category), stat="identity") + geom_text(aes(label = frequency, y = pos), size = 3)
something along these lines trick:
p <- ggplot(data, aes(x = year, y = frequency)) + geom_bar(aes(fill = category), stat="identity", show.legend = false) + geom_text(aes(label = frequency, y = pos), size = 3, nudge_y = -25) + geom_text(aes(label = category, y = pos), size = 3, nudge_y = 25)
which plots
customize like.
Comments
Post a Comment