Adding labels to individual % inside geom_bar() using R / ggplot2

wwins
bgraph <- ggplot(data = data, aes(x = location)) +
  geom_bar(aes(fill = success))

success is a percentage calculated as a factor of 4 categories with the varying 4 outcomes of the data set. I could separately calculate them easily, but as the ggplot is currently constituted, they are generated by the geom_bar(aes(fill=success)).

data <- as.data.frame(c(1,1,1,1,1,1,2,2,3,3,3,3,4,4,4,4,4,4,
                        4,4,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7))
data[["success"]] <- c("a","b","c","c","d","d","a","b","b","b","c","d",
                       "a","b","b","b","c","c","c","d","a","b","c","d",
                       "a","b","c","c","d","d","a","b","b","c","d")
names(data) <- c("location","success")
bgraph <- ggplot(data = data, aes(x = location)) +
  geom_bar(aes(fill = success))
bgraph

How do I get labels over the individual percentages? More specifically, I wanted 4 individual percentages for each bar. One for yellow, light orange, orange, and red, respectively. %'s all add up to 1.

Bar Graph

Ronak Shah

Maybe there is a way to do this in ggplot directly but with some pre-processing in dplyr, you'll be able to achieve your desired output.

library(dplyr)
library(ggplot2)

data %>%
  count(location, success) %>%
  group_by(location) %>%
  mutate(n = n/sum(n) * 100) %>%
  ggplot() + aes(x = location, n, fill = success,label = paste0(round(n, 2), "%")) +
  geom_bar(stat = "identity") +
  geom_text(position=position_stack(vjust=0.5))

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

R ggplot2: labels inside bars, no stacked geom_bar

R ggplot2 geom_bar add labels with % sign

R: ggplot2 geom_text data labels for binned geom_bar

How to put labels over geom_bar for each bar in R with ggplot2

Adding data labels to geom_bar when using proportions

Adding labels to a plot using ggplot2 in R

Set starting point for breaks for labels in ggplot using geom_bar [R]

How to add custom labels from a dataset on top of bars using ggplot/geom_bar in R?

R & ggplot2: 100% geom_bar + geom_line for average using secondary y axis

Adding a statistics table to the bottom of a ggplot, geom_bar chart in R

How to vertically center labels with geom_bar (ggplot2)

R: change bar labels in geom_bar

Highlighting individual axis labels in bold using ggplot2

Justify individual axis labels in bold using ggplot2

ggplot grouped geom_bar - adding labels of factor categories to the x-axis label

using multiple variables in geom_bar with ggplot at same X (R)

R ggplot2 change geom_bar to a single color

ggplot2: plot correct proportions using geom_bar

ggplot: geom_bar stacking order and labels

Adding exponent to axes labels (R / ggplot2)

Adding percentage labels to a bar chart in ggplot2

Adding labels on each bar in ggplot2 correctly

R geom_bar and geom_line labels for grouped bars

Points in a scatterplot with individual ellipses using ggplot2 in R

How to add labels into the bars in a bar graph using ggplot2 in R?

ggplot2: Value labels in stacked barplot with geom_bar(position = 'fill')

How can I put labels on geom_bar() in ggplot2 (without a manual y_position)

R ggplot geom_bar: Change transparency inside bars, keeping contour/boundary the same

R - ggplot2 'dodge' geom_step() to overlap geom_bar()