TeoK
TeoK

Reputation: 501

add value total of column and per group in histogram ggplot2

df<-data.frame(age=c(58, 50, 71, 39, 70, 65, 43, 62, 79, 68, 67, 61, 44, 67), 
              gender=c( "Male", "Male", "Female", "Female", "Male", "Male", "Female", "Female", "Male", "Female", "Male", "Female", "Female", "Male"))
bw <-round( 2*IQR(mydata$age, na.rm = T)/length(mydata$age)^(1/3),0)
mybreaks=seq(min(df$age-bw),max(df$age)+bw,bw)

I try create plot with ggplot2 that include count per group and total of column.

ggplot(df,aes(age,fill=gender))+
  geom_histogram(binwidth = bw, color="black")+
  scale_x_continuous(breaks = mybreaks)+
  stat_bin(binwidth = bw, geom = "text", color="white", aes(label=..count..), position = position_stack(vjust=0.5))+
  geom_text(mydata=df %>% filter(!is.na(age)) %>%  group_by(gender) %>% mutate(n=sum(age)), aes(label=n, y=n+2))

And I get errors:

non-numeric argument to binary operator

What I expect: enter image description here

Upvotes: 0

Views: 709

Answers (1)

stefan
stefan

Reputation: 123978

You could add your totals labels via a second stat_bin. To get the count for both genders set the group aesthetic to e.g. 1. Additionally remove the position argument or set it to identity:

library(ggplot2)

ggplot(df, aes(age, fill = gender)) +
  geom_histogram(binwidth = bw, color = "black") +
  scale_x_continuous(breaks = mybreaks) +
  stat_bin(binwidth = bw, geom = "text", color = "white", 
           aes(label = ..count..), position = position_stack(vjust = 0.5)) +
  stat_bin(binwidth = bw, geom = "text", color = "black", 
           aes(label = ..count.., group = 1), vjust = -.25, position = "identity")

Upvotes: 1

Related Questions