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

Time:07-28

Based on the code below I am trying to create a pyramid plot. The Value column is in percent so I added the % sign to this column. And, now the code doesn't work.

Error: Discrete value supplied to continuous scale

The reason behind adding the % sign is that this way it will also be in the x-axis labels.

How can I fix this such that the % sign appears on the x-axis and the - sign doesn't appear on x-axis as well?

Code data:

library(tidyverse)

# Data
pop = sstructure(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"), problems = <pointer: 0x0000029a145331e0>, class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

# Draw a pyramid plot

# Clean data
pop_df = pop %>% 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)

# Add % sign to the Value column
pop_df$Value = paste0(pop_df$Value, "%")


# Plot
gg_pop = 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)  
  ggtitle("Male vs Female Population Comparison")  
  labs(x = "Age group", y = "Percentage", fill = "Gender")  
  coord_flip() 

# Interactive
ggplotly(gg_pop)

CodePudding user response:

Don't add % to the column data, add it in the label.

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 = function(z) paste0(abs(z), "%"))            # CHANGE
  ggtitle("Male vs Female Population Comparison")  
  labs(x = "Age group", y = "Percentage", fill = "Gender")  
  coord_flip() 

ggplot with % in the axis labels

  • Related