Age_pyramid with proportions plus labels for absolute frequencies

Hi guys. Is it possible to add labels of the absolute frequencies on the age_pyramid figure that shows proportional frequencies? I really appreciate any help you can provide.

# Example script  

# install and load packages
pacman::p_load(
  rio, 
  here, 
  janitor, 
  tidyverse,
  reprex,
  datapasta)

baza <- data.frame(
  stringsAsFactors = FALSE,
            Gender = c("male","male","male","male",
                       "female","male","female","female","male","male",
                       "male","male","male","female","female","female",
                       "male","female","male","male"),
               Age = c(0.254620123203285,
                       15.3456536618754,13.4127310061602,0.172484599589322,
                       11.4989733059548,15.7070499657769,0.783025325119781,46.7652292950034,
                       12.3613963039014,14.4531143052704,14.7049965776865,
                       13.6262833675565,10.5462012320329,28.1204654346338,
                       4.33949349760438,11.8302532511978,16.7173169062286,
                       51.6769336071184,15.2689938398357,14.6748802190281)
)                        # convert to stand-alone R code

#za kreiranje nove varijable uzrasnih grupa na osnovu varijable uzrast u godinama
baza["age_group"] = cut(baza$Age, c(0, 1,6,15,19,29,50,Inf), c("<1", "1-5", "6-14", "15-18","19-29","30-49","50+"), include.lowest=TRUE)

baza %>% 
  apyramid::age_pyramid(
    # Specify column containing age categories:
    age_group = "age_group",
    # Specify column containing sex:
    split_by = "Gender", 
    # Don't show midpoint on the graph:
    show_midpoint = FALSE,
    # Display percentages instead of absolute values:
    proportional = TRUE
  ) +
  labs(
    title    = "Agepyramid of reported x cases ",
    subtitle = "Period: ",
    x        = "Age groups",
    y        = "Percentage of reported cases"
  )

Created on 2025-10-08 with reprex v2.1.1

Session info

sessionInfo()
#> R version 4.4.2 (2024-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64
#> Running under: Windows 11 x64 (build 26100)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=Serbian (Latin)_Serbia.utf8 
#> [2] LC_CTYPE=Serbian (Latin)_Serbia.utf8   
#> [3] LC_MONETARY=Serbian (Latin)_Serbia.utf8
#> [4] LC_NUMERIC=C                           
#> [5] LC_TIME=Serbian (Latin)_Serbia.utf8    
#> 
#> time zone: Europe/Podgorica
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] datapasta_3.1.0 reprex_2.1.1    lubridate_1.9.4 forcats_1.0.0  
#>  [5] stringr_1.5.1   dplyr_1.1.4     purrr_1.0.4     readr_2.1.5    
#>  [9] tidyr_1.3.1     tibble_3.2.1    ggplot2_3.5.2   tidyverse_2.0.0
#> [13] janitor_2.2.1   here_1.0.1      rio_1.2.3      
#> 
#> loaded via a namespace (and not attached):
#>  [1] generics_0.1.4     xml2_1.3.8         stringi_1.8.7      hms_1.1.3         
#>  [5] digest_0.6.37      magrittr_2.0.3     evaluate_1.0.3     grid_4.4.2        
#>  [9] timechange_0.3.0   RColorBrewer_1.1-3 fastmap_1.2.0      rprojroot_2.0.4   
#> [13] scales_1.4.0       cli_3.6.3          rlang_1.1.4        apyramid_0.1.3    
#> [17] withr_3.0.2        yaml_2.3.10        tools_4.4.2        tzdb_0.5.0        
#> [21] pacman_0.5.1       curl_6.2.3         vctrs_0.6.5        R6_2.6.1          
#> [25] lifecycle_1.0.4    snakecase_0.11.1   fs_1.6.6           pkgconfig_2.0.3   
#> [29] pillar_1.10.2      gtable_0.3.6       glue_1.8.0         xfun_0.52         
#> [33] tidyselect_1.2.1   rstudioapi_0.17.1  knitr_1.50         dichromat_2.0-0.1 
#> [37] farver_2.1.2       htmltools_0.5.8.1  rmarkdown_2.29     compiler_4.4.2

Yes, it’s possible to add absolute frequency labels to the age pyramid. One way I can think right now is to calculate the counts and keep them as a separate column in your data frame. Then you can use that count column inside geom_text() to display the absolute values while plotting proportions.apyramid::age_pyramid() returns a ggplot object, so you can layer on top of it using + geom_text()

If you share the steps you used to produce the data frame you sent in the minimal example, I can try to help by showing you how to calculate the counts and include them as a column, so you can use them in geom_text() as I mentioned.

Thanks, I managed to do it!

1 Like