Home > Software engineering >  Is there a way to label characters above this dodged geom_bar plot?
Is there a way to label characters above this dodged geom_bar plot?

Time:09-18

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:

enter image description here

However, I would like to add the month name labels above each dodged bar. Something like this but with the full name:

enter image description here

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.

CodePudding user response:

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?"
  )

enter image description here

CodePudding user response:

First we change month names to abbreviated, then with add_count we get the counts (for the y in geom_text). At the end we add geom_text to your code.

library(tidyverse)
df %>% 
  mutate(Month_Name = month.abb[match(Month_Name, month.name)]) %>% 
  add_count(Consec_Month, Workout_Y_N) %>% 
  ggplot(aes(x=Consec_Month, y=n, fill=factor(Workout_Y_N))) 
  geom_bar(stat = "identity", 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?")   
  geom_text(aes(Consec_Month, label = Month_Name),
            position = position_dodge(width = 1), color = "red", vjust = -0.5, size = 4)   
  theme_bw()

enter image description here

  • Related