Pyramid Chart

Authors

[Editor] Hu Zheng;

[Contributors]

Note

Hiplot website

This page is the tutorial for source code version of the Hiplot Pyramid Chart plugin. You can also use the Hiplot website to achieve no code ploting. For more information please see the following link:

https://hiplot.cn/basic/pyramid-chart?lang=en

The pyramid chart is a pyramid-like figure that distributes data on both sides of a central axis.

Setup

  • System Requirements: Cross-platform (Linux/MacOS/Windows)

  • Programming language: R

  • Dependent packages: ggcharts

# Install packages
if (!requireNamespace("ggcharts", quietly = TRUE)) {
  install.packages("ggcharts")
}

# Load packages
library(ggcharts)

Data Preparation

The loaded data are age, gender, and the number of people after the combination of the first two variables .

# Load data
data <- read.delim("files/Hiplot/144-pyramid-chart-data.txt", header = T)

# View data
head(data)
    age    sex    pop
1   0-4   male 225817
2   0-4 female 213072
3   5-9   male 223656
4   5-9 female 210508
5 10-14   male 214782
6 10-14 female 201672

Visualization

# Pyramid Chart
p <- pyramid_chart(data = data, x = age, y = pop, group = sex, 
                   title = "", sort = "no", bar_colors = c("#C20B01","#196ABD")) +
  theme(text = element_text(family = "Arial"),
        plot.title = element_text(size = 12,hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        axis.text.x = element_text(angle = 0, hjust = 0.5,vjust = 1),
        legend.position = "right",
        legend.direction = "vertical",
        legend.title = element_text(size = 10),
        legend.text = element_text(size = 10))

p
FigureΒ 1: Pyramid Chart

The graph shows the age groups from bottom to top in order on the central axis, the left side represents the number of men, the right side represents the number of women, and the X-axis represents the number of people. The graph clearly shows the proportion of men and women in different age groups and the proportion of different age groups in the same gender.