0

Currently i have plotted a stacked bargraph to display the amount of people having delays in each month with the following code

q1%>%
  mutate(DepDelay1 =factor(x=DepDelay1, levels=c(0,1), labels=c("No","Yes"))) %>% 
  filter(Year==2005)%>%
  ggplot(aes(x=factor(Month), fill=DepDelay1)) +
  geom_bar(position="stack") + 
  ggtitle("DepDelay of Flight") +
  guides(fill=guide_legend("Delay")) +
  xlab ("Month")+
  ylab ("Flight Count")+
  geom_text(aes(label=..count..),
  stat='count',
  colour = "white", 
  size = 2.5,
  position = position_stack(vjust = 0.5))

Graph Example for position="stack"

However i cannot seem to produce a 100% stacked bargraph after changing from

geom_bar(position="stack")

to

geom_bar(position="fill")

Graph Example for position="fill"

This method works before i adjust the geom_text and adding the filter to my current code.

Anyone know what's the issue ? From what i understand, a Y-variable is needed however i don't have a suitable value to input it from the dataset and i'm trying to plot the count per month.

TYMING
  • 1
  • 2
  • Try with `geom_bar(position="fill")`. You missed the first "i". – stefan Jan 28 '22 at 10:02
  • My bad, i spelt position wrongly in the question, however the plot it reflects shows only the same y and x values as the picture provided and no graph is produced. – TYMING Jan 28 '22 at 10:14
  • Switch to `position_fill` in geom_text. The bars with pos fill are shown as values from 0 to 1, while your labels are placed at the the counts and the range of the scale will reflect that. Hence your bars are nearly invisible. – stefan Jan 28 '22 at 10:24
  • 1
    It works, thanks alot ! Didn't expect geom_text will affect the range of the graph as i thought it only meddles with the label. – TYMING Jan 28 '22 at 10:28
  • Yep. Had to check that out too. Reminder for the next time: Try to add a snippet of your data or some fake data the next time, e.g. in your case `nycflights13::flights` may serve as example data. See [how to make a great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – stefan Jan 28 '22 at 10:32
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 02 '22 at 09:52

0 Answers0