-1

Hi I have been trying to summarize my data set by sum of a particular column and using ggplot and geom_bar() directly to do so instead of using tidyvers and dplyr libraries.

I read about weight aesthetic and that it can help plotting bar graph as sum of a particular feature instead of count and it worked.

Problem is I am unable to plot the y value on top of the bars as I do not know how should I write geom_text(????) code:

p1<-ggplot(df, aes(x= ageBracket, fill= movement, **weight= amount**))+ geom_bar(width= 0.5, position= 'dodge')+ theme_hc()+scale_x_discrete('Age Group', limits=c("16 to 25 years", "26 to 35 years", "36 to 45 years", "> 46 years"))+ theme(legend.position= "bottom",legend.background = element_rect(fill="lightblue", size=0.2, linetype="solid"))+ scale_fill_brewer(palette = "Dark2")+ scale_y_continuous("Amount per Age Group",labels = comma)+ **geom_text(?????)**
print(p1)

Blockquote

Here fill has helped me to segregate the bars that I have dodged, weight has helped me to sum data based on the filters of : movement & ageBracket, I just want the y axis values on top of the respective bar and not want to create a separate data frame. What should I do ??

  • You should go back to summarizing your dataset and then adding labels in the normal way. `geom_text` requires the following aesthetics: __x__, __y__, and __label__. If you don't supply them in the ggplot(), you can't use them later. – Edward May 03 '20 at 08:39
  • It would help if you made your question reproducible and included a sample dataset you use `dput(head(df, n))` to avoid an huge data drop. [reprex] is a useful link to write a good quesiton. – Peter May 03 '20 at 08:51

1 Answers1

1

The current version of ggplot2 doesn't allow labels in geom_text() when the weight aesthetic is used. You'll have to summarise the data frame and then do it the usual way using the label aesthetic.

Actually, there's not that much extra work required as shown using the mtcars dataset. Only two extra lines required, and you don't need to create a new data frame if you pipe the results forward.

library(ggplot2)
library(dplyr)
data(mtcars)

(1) Using weight aesthetic to sum the mpg variable by am and cyl. Cannot add labels to bars (6 lines).

mtcars %>%
  mutate(am=factor(am, labels=c("auto","manual")), cyl=factor(cyl)) %>%
  ggplot(aes(x=cyl, fill=am, weight=mpg)) + 
  geom_bar(width=0.5, position='dodge') +
  labs(y="Total miles/gallon", x="Cylinders", fill="Transmission") +
  theme_minimal()

(2) Summarising data first, then using label aesthetic with geom_text, can add labels to bars (2 extra lines).

mtcars %>%
  mutate(am=factor(am, labels=c("auto","manual")), cyl=factor(cyl)) %>%
  group_by(cyl, am) %>%  # extra
  summarise(n = sum(mpg)) %>% # extra
  ggplot(aes(x=cyl, fill=am, y=n, label=n)) + 
  geom_col(width=0.5, position='dodge') +
  geom_text(position=position_dodge(0.5), vjust=-0.25) + 
  labs(y="Total miles/gallon", x="Cylinders", fill="Transmission") +
  theme_minimal()

enter image description here

Edward
  • 10,360
  • 2
  • 11
  • 26