I am trying to create a bar chart in R from a data frame, which has counts in the y-axis but displays as labels a concatenation of percentages and counts.
My data frame looks as below:
ID Response
1 No
2 Yes
3 No
.. ..
The end result I would like to have would be a chart as the one below
This should get you going:
library(tidyverse)
df %>%
group_by(Response) %>%
summarise(count = n()) %>%
mutate(Label = paste0(count, " - ", round(count / sum(count) * 100, 2), "%")) %>%
ggplot(aes(x = Response, y = count)) +
geom_bar(stat = 'identity', fill = 'lightblue') +
geom_text(aes(label = Label)) +
theme_minimal()
A solution as above can be to create a Label
column which you can then pass to geom_text
if needed.
A dummy data frame:
df <- data.frame(
ID = c(1:100),
Response = c(rep("Yes", 60), rep("No", 40))
)