ggPyramid is a package that facilitates the elaboration of population pyramids.
Install ggPyramid:
library(remotes)
install_github("musajajorge/ggPyramid")
library(readxl)
url <- "https://zenodo.org/record/8306939/files/popPER.xlsx?download=1"
destfile <- "popPER.xlsx"
curl::curl_download(url, destfile)
data <- read_excel(destfile)
data <- dplyr::filter(data, Year==2023)
Using the default parameters.
plotPyramid(df=data, age="gAge", group="Sex", pop="Population")
Modifying the colors, X and Y axis labels, as well as the rotation of the X axis labels.
plotPyramid(df=data, age="gAge", group="Sex", pop="Population",
labx="People", laby="Age group",
twocolors=c("steelblue","violetred3"),
rotation=45, n.breaks=15, value.labels=FALSE)
Modifying the position of the values in the bars.
plotPyramid(df=data, age="gAge", group="Sex", pop="Population",
value.labels=TRUE, position.value.labels="out",
size.value.labels=4)
Using the default parameters.
library(dplyr)
data <- data |>
group_by(gAge, Sex) |>
summarise(Population = sum(Population)) |>
group_by(Sex) |>
mutate(percPopulation = round(Population/sum(Population)*100, 4)) |>
select(gAge, Sex, percPopulation) |>
arrange(Sex, gAge)
plotPercPyramid(df=data, age="gAge", group="Sex", perpop="percPopulation")
Modifying the position of the values on the bars, the X and Y axis labels and the colors of the bars.
plotPercPyramid(df=data, age="gAge", group="Sex", perpop="percPopulation",
labx="% People", laby="Age group", n.breaks=10,
twocolors=c("steelblue","violetred3"),
value.labels=TRUE, position.value.labels="out",
size.value.labels=4)