1
votes

I am trying to plot a histogram with an overall count of each bin on the top. Following is my data: enter image description here

You can use the following sample data:

histData <- data.frame("UserId" = 1:20, "age" = c(replicate(20,sample(10:20,20,rep=TRUE))), "Gender" = c("Male", "Female"))

I am using ggplot as shown below:

ggplot(histData, aes(x = age, color = Gender, fill = Gender)) +
  geom_histogram(binwidth = 1,
                 alpha = 0.2,
                 position = "identity", aes(y = 100*(..count..)/sum(..count..))) +
  scale_color_manual(values = rainbow(3)) +
  geom_vline(
    aes(xintercept = mean(age)),
    color = "black",
    linetype = "dashed",
    size = 1
  ) +
  labs(title = "Age histogram plot", x = "Age", y = "Percentage") +
  theme_minimal() + theme(plot.title = element_text(hjust = 0.5))+
  stat_bin(aes(y=round(100*(..count..)/sum(..count..),1), label=round(100*(..count..)/sum(..count..),1)), geom="text", vjust=0, binwidth = 1) 

which results in the plot as shown below: enter image description here

In the plot, count for each gender is displayed separately, on the top of their respective bins. However, I do not want gender specific count, I just want the overall count on top of the bin stacks (i.e. I just want the red numbers which says the overall count). How do I achieve that while having aes(x = age, color = Gender, fill = Gender) aesthetics in my ggplot2 for classes of gender?

EDIT: Based on the answer below, tried the following

ageGroupCount <- histData[, -1]
ageGroupCount$age <- as.integer(df$age)
ageGroupCount$Gender <- as.factor(df$Gender)
ageGroupCount <-
  ageGroupCount %>%  group_by(age, Gender) %>% count()

ageCount <- histData[2] %>% count()

ageGroupCount %>%
  ggplot(aes(x = age, y = freq, label = freq)) +
  geom_col(aes(fill = Gender, color = Gender), alpha = 0.65) +
  scale_y_continuous(labels = percent) +
  geom_text(
    data = ageCount,
    size = 3,
    position = position_dodge(width = 1),
    vjust = -0.5
  ) + geom_vline(
    aes(xintercept = mean(age)),
    color = "black",
    linetype = "dashed",
    size = 1
  ) + scale_color_manual(values = rainbow(3)) +
  labs(title = "Age histogram plot", x = "Age", y = "Percentage") +
  theme_minimal() + theme(plot.title = element_text(hjust = 0.5))

which resulted in the following plot: enter image description here How do I get rid of the trailing zeros in the scale, and how do I put up the percent values on the top of each bar, instead of the absolute numbers?

ANSWER: I was able to do it using the code below

ageGroupCount <- histData[, -1]
ageGroupCount$age <- as.integer(ageGroupCount$age)
ageGroupCount$Gender <- as.factor(ageGroupCount$Gender)
ageGroupCount <-
  ageGroupCount %>%  group_by(age, Gender) %>% count()
ageGroupCount <- mutate(ageGroupCount, freq = round(100*freq / sum(freq),1))

ageCount <- histData[2] %>% count()
ageCount$age <- as.integer(ageCount$age)
ageCount <- mutate(ageCount, freq = round(100*freq / sum(freq),1))

ageGroupCount %>%
  ggplot(aes(x = age, y = freq, label = freq)) +
  geom_col(aes(fill = Gender, color = Gender), alpha = 0.65) +
  geom_text(
    data = ageCount,
    size = 3,
    position = position_dodge(width = 1),
    vjust = -0.5
  ) + geom_vline(
    aes(xintercept = mean(age)),
    color = "black",
    linetype = "dashed",
    size = 1
  ) + scale_color_manual(values = rainbow(3)) +
  scale_y_continuous(labels = function(x) paste0(x, "%"))+
  labs(title = "Age histogram plot", x = "Age", y = "Percentage") +
  theme_minimal() + theme(plot.title = element_text(hjust = 0.5))

enter image description here

1

1 Answers

1
votes

Okay, first let's simplify this a little bit by getting a summary data frame with counts by age and gender.

df <-
  histData %>% 
  group_by(age, Gender) %>% 
  count()

df
# A tibble: 22 x 3
# Groups:   age, Gender [22]
     age Gender     n
   <int> <fct>  <int>
 1    10 Female    20
 2    10 Male      22
 3    11 Female    22
...

Then we can use geom_col to plot the results directly rather than having geom_histogram calculate them with a lot of nasty syntax. The text labels come from a second group-by/count operation, using the gender numbers as weights:

df %>% 
  ggplot(aes(x = age, y = n)) +
  geom_col(aes(fill = Gender)) +
  geom_text(
    data = . %>% group_by(age) %>% count(wt = n),
    aes(y = n + 2, label = n)
  )

ggplot graph

This gets the core portion of the graph done - it looks like you should be able to handle the formatting and other additions from here.