Data
Here is my dput:
structure(list(Workout_Y_N = c("Y", "Y", "Y", "N", "N", "N",
"N", "Y", "Y", "N", "N", "N", "N", "Y", "Y", "Y", "N", "N", "Y",
"Y", "N", "N", "N", "N", "N", "N", "Y", "N", "N", "N", "N", "Y",
"Y", "Y", "N", "Y", "Y", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "Y", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "Y", "N", "N", "N", "N",
"N", "N", "N"), Consec_Month = c("1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3",
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3",
"3", "3", "3", "3", "3", "3", "3", "4", "4", "4", "4", "4", "4",
"4", "4", "4"), Month_Name = c("September", "September", "September",
"September", "September", "September", "September", "September",
"September", "September", "September", "September", "September",
"September", "September", "September", "September", "September",
"September", "September", "September", "September", "September",
"September", "September", "September", "September", "September",
"September", "September", "October", "October", "October", "October",
"October", "October", "October", "October", "October", "October",
"October", "October", "October", "October", "October", "October",
"October", "October", "October", "October", "October", "October",
"October", "October", "October", "October", "October", "October",
"October", "October", "October", "November", "November", "November",
"November", "November", "November", "November", "November", "November",
"November", "November", "November", "November", "November", "November",
"November", "November", "November", "November", "November", "November",
"November", "November", "November", "November", "November", "November",
"November", "November", "November", "December", "December", "December",
"December", "December", "December", "December", "December", "December"
)), class = "data.frame", row.names = c(NA, -100L))
Problem
I am trying to create a bar graph that includes an identifier for which month of the year workouts were done more consistently, and labeling the tops of the bars with a label of the specific month. So far I have this:
work %>%
ggplot(aes(x=Consec_Month,
fill=factor(Workout_Y_N)))+
geom_bar(position = "dodge")+
scale_fill_manual(values = c("steelblue","navyblue"),
labels = c("No","Yes"))+
theme(legend.position = "bottom")+
labs(title = "Frequency of Workout",
x="",
y="Count",
fill="Workout?")
Which gives me this:
However, I would like to add the month name labels above each dodged bar. Something like this but with the full name:
I believe this can be done with something like geom_text but for some reason my brain isn’t helping me figure out how to make that happen with the arguments in the geom_text function. Any help would be appreciated.
>Solution :
One option to achieve your desired result via geom_text would be to use stat="count" which like geom_bar will compute the counts and the y position for the labels. Additionally use position = position_dodge(width = .9) to align the labels with bars and to add some padding I use vjust = -.5:
library(ggplot2)
ggplot(work, aes(
x = Consec_Month,
fill = factor(Workout_Y_N)
)) +
geom_bar(position = "dodge") +
geom_text(aes(label = Month_Name), stat = "count", position = position_dodge(width = .9), vjust = -.5) +
scale_fill_manual(
values = c("steelblue", "navyblue"),
labels = c("No", "Yes")
) +
theme(legend.position = "bottom") +
labs(
title = "Frequency of Workout",
x = "",
y = "Count",
fill = "Workout?"
)


