0

I have a grouped bar chart similar to this one

I would like to add the count of each group e.g. number of actors in smaller text below the "Actor" label in X-axis. I am not able to put the count of each group.

Following is what I have so far

data <- read.csv("employee.txt", sep = "\t" , header = TRUE)
df1<-tibble(data)
df1<- mutate(df1, emp_class = cut(emp_type))
df1<- mutate(df1, emp_class = cut(LPA_CN, breaks = seq(10, 80, by = 20))) 
my_ggp <- ggplot(df1, aes(x=as.factor(Employee), fill=as.factor(emp_class)))+geom_bar(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..]*100), position="dodge") + ylab('% of total') +xlab("") + labs(fill = "Types") 
my_ggp + theme(text = element_text(size = 20)) 

I would appreciate any help.

BeginneR
  • 69
  • 7
  • To help us to help you, would you mind providing a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data and the code you have tried. – stefan Aug 13 '21 at 18:56
  • https://stackoverflow.com/q/6644997/6503141 ? – Bernhard Aug 13 '21 at 18:59
  • Seems like you're using pretty old syntax. The tidyverse crew has gone through a few renames, but the `..count..` syntax has been replaced by `after_stat` for a while. It would be helpful if you could explain what you're trying to do there – camille Aug 17 '21 at 20:53
  • I am trying to plot a grouped bar chart like [this](https://i.stack.imgur.com/FU73z.png). Inside, each group of employees, I have different age groups and their percentage. – BeginneR Aug 17 '21 at 21:21

1 Answers1

1

Here is an example with the iris dataset: The method used here is to add a column with the labels: mutate(x_axis = paste(Species, n, sep = "\n"))

# preparing data:

iris1 <- iris %>% 
    group_by(Species) %>% 
    add_count() %>%  
    mutate(x_axis = paste(Species, n, sep = "\n")) %>% 
    pivot_longer(-c(Species, n, x_axis))

# plot
ggplot(iris1, aes(x=x_axis, fill=Species)) + 
    geom_bar(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..]*100), 
             position="dodge") +
    ylab('% of total') +
    xlab("") + labs(fill = "Types") 

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Thanks !! It works fine without `pivot_longer(-c(Ethinicity, n, x_axis))`. I am getting this error when I used that `Error: Can't combine `emp_sal` and `emp_class` >.` Also, Is there a way to make the size of count bit smaller e.g. `setosa` has size 30 but `50` has font size 15? – BeginneR Aug 17 '21 at 19:42