Home > Net >  R ggplot2 create a pyramid plot
R ggplot2 create a pyramid plot

Time:07-28

Based on the code below I am trying to create a pyramid plot by following the 3rd answer to this question:

enter image description here

CodePudding user response:

You could make one gender negative to create a pyramid plot and use two geom_bar, one per gender like this:

library(tidyverse)
library(janitor)
library(lemon)
pop = structure(list(age_group = c("<  5 years", "5 - 9", "10 - 14", 
                                   "15  -  19", "20  -  24", "25  -  29", "30  -  34", "35  -  44", 
                                   "45  -  54", "55  -  64", "65  -  74", "75  -  84", "85  "), 
                     males = c(6, 6, 7, 6, 7, 7, 8, 17, 15, 11, 6, 3, 1), females = c(6, 
                                                                                      5, 6, 6, 6, 7, 7, 16, 15, 12, 7, 4, 2)), row.names = c(NA, 
                                                                                                                                             -13L), spec = structure(list(cols = list(`AGE GROUP` = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                                                "collector")), MALES = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                   "collector")), FEMALES = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                        "collector"))), default = structure(list(), class = c("collector_guess", 
                                                                                                                                                                                                                                                                                                                                                                                              "collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    "tbl_df", "tbl", "data.frame"))

# Draw a pyramid plot

pop_df = pop %>% 
  dplyr::select(age_group,males,females) %>% 
  gather(key = Type, value = Value, -c(age_group))

# Make male values negative
pop_df$Value <- ifelse(pop_df$Type == "males", -1*pop_df$Value, pop_df$Value)

ggplot(pop_df, aes(x = age_group, y = Value, fill = Type))  
  geom_bar(data = subset(pop_df, Type == "females"), stat = "identity")   
  geom_bar(data = subset(pop_df, Type == "males"), stat = "identity")   
  scale_y_continuous(labels = abs)  
  labs(x = "Age group", y = "Value", fill = "Gender")  
  coord_flip() 

Created on 2022-07-27 by the enter image description here

Update:

In order to make remove the negative sign on the labels, use the absolute value. However, it is already part of your code: scale_x_symmetric(labels = abs) (from the lemon package).

scale_x_continuous(labels = abs)
  • Related