MYaseen208 - 7 months ago 68

R Question

I'd like to show data values on stacked bar chart in ggplot2. Here is my attempted code

`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(ggplot2)

p <- qplot(Year, Frequency, data = Data, geom = "bar", fill = Category, theme_set(theme_bw()))

p + geom_text(aes(label = Frequency), size = 3, hjust = 0.5, vjust = 3, position = "stack")

I'd like to show these data values in the middle of each portion. Any help in this regard will be highly appreciated. Thanks

Answer

Here is one approach, which calculates the midpoints of the bars.

```
library(ggplot2)
library(plyr)
# calculate midpoints of bars (simplified using comment by @DWin)
Data <- ddply(Data, .(Year),
transform, pos = cumsum(Frequency) - (0.5 * Frequency)
)
# library(dplyr) ## If using dplyr...
# Data <- group_by(Data,Year) %>%
# mutate(pos = cumsum(Frequency) - (0.5 * Frequency))
# plot bars and 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)
```

Source (Stackoverflow)