0

I am doing a workforce mobility analysis and would like to create a pyramid chart where each level of the pyramid corresponds to job level, width corresponds to number of people at that level, and height corresponds to average number of years spent in that position.

Sample data:

employee_lvl <- c('entry','mid','supervisor', 'boss')
num_emp <- c(40, 30, 7, 1)
ave_dwell <- c(2,8, 10, 15)

df <- data.frame(employee_lvl, num_emp, ave_dwell)

Ideal Output:

varied height pyramid chart

I have looked at similar problems like Population pyramid in ggplot and Pyramid plot in R, but they don't vary the height and both split their populations. I have also looked at violin charts but haven't found a way to do this. Is there an easier way to create a graph like this without individually creating and stacking rectangles?

Apologies for any information or formatting errors, this is my first post.

teunbrand
  • 33,645
  • 4
  • 37
  • 63

1 Answers1

1

If you know how to the rectangles should be parameterised, it is quite easy to do this with a plain geom_rect(). Example below assumes that the order of the data is correct.

I know this could be considered 'individually creating and stacking rectangles', but at least you don't have to do it manually for every rectangle.

library(ggplot2)

employee_lvl <- c('entry','mid','supervisor', 'boss')
num_emp <- c(40, 30, 7, 1)
ave_dwell <- c(2,8, 10, 15)

df <- data.frame(employee_lvl, num_emp, ave_dwell)

ggplot(df) +
  geom_rect(
    aes(xmin = -0.5 * num_emp,
        xmax = 0.5 * num_emp,
        ymin = head(cumsum(c(0, ave_dwell)), -1),
        ymax = cumsum(ave_dwell),
        fill = employee_lvl)
  )

teunbrand
  • 33,645
  • 4
  • 37
  • 63