Basic creating a Population Pyramid in R

To create a population pyramid, we use the coord_flip() function along with the geom_bar() function to create a horizontal bar plot, then we make the value of the male population negative using the mutate function thus creating the male population bars on the left side and female population bar on the right side giving us the required population pyramid.

Syntax:

ggplot( df, aes(x = age, y = population)) +   geom_bar(stat = “identity”) + coord_flip()

Parameters:

  • df: determines the data frame that contains population data.
  • gender, age and population: determines the columns of df data frame.

Example:

Here, is a basic population pyramid. The CSV file used in the example can be downloaded here.

R




# load sample data
sample_data <- read.csv("Population.CSV")
  
# load library ggplot2 and dplyr
library(ggplot2)
library(dplyr)
  
# change male population to negative
sample_data %>%mutate(
    population = ifelse(gender=="M", population*(-1),
                        population*1))%>%
    ggplot(aes(x = age,y = population)) + 
    geom_bar(stat = "identity") +
    coord_flip()


Output:

How to Create a Population Pyramid in R?

In this article, we will discuss how to create a population pyramid in the R Programming Language.

A population pyramid is also known as an age-sex pyramid. It helps us to visualize the distribution of a population by age group and sex. It generally takes the shape of a pyramid. In the population pyramid, males are usually depicted on the left and females on the right. This method of data visualization can be used to visualize the age of a particular population based on the number or percentage of male and female inhabitants.

To create a population pyramid in the R Language, we use the geom_bar() function of the ggplot2 package. The ggplot2 is a system for declaratively creating graphics, based on “The Grammar of Graphics”. The geom_bar() function is used to draw the bar plots, it makes the height of the bar proportional to the number of cases in each group. 

Similar Reads

Basic creating a Population Pyramid in R

To create a population pyramid, we use the coord_flip() function along with the geom_bar() function to create a horizontal bar plot, then we make the value of the male population negative using the mutate function thus creating the male population bars on the left side and female population bar on the right side giving us the required population pyramid....

Color Customization Population Pyramid in R

...

Label Customization Population Pyramid in R

To customize the color of male and female bars, we use the fill aesthetic property of the ggplot() function. We can either pass the variable according to which we want the colors to be or we can pass the exact colors that need to be put. We can even use the scale_fill_brewer() function to set the color to a predefined palette....