Home > Enterprise >  R ggplot2 geom_bar add labels with % sign
R ggplot2 geom_bar add labels with % sign

Time:08-16

Based on the data and code below, I want to add labels to the pyramid plot based on the Value column, add a % sign next to the value and remove the - sign from the label values.

Right now the labels are appearing on the x-axis without the % sign and with the - sign. How can I fix this?

Current output:

enter image description here

Data (pop_hisp_df):

structure(list(age_group = c("<  5 years", "5 - 14", "15  -  24", 
"25  -  34", "35  -  44", "45  -  54", "55  -  64", "65  -  74", 
"75  -  84", "85  ", "<  5 years", "5 - 14", "15  -  24", "25  -  34", 
"35  -  44", "45  -  54", "55  -  64", "65  -  74", "75  -  84", 
"85  "), Type = c("Males", "Males", "Males", "Males", "Males", 
"Males", "Males", "Males", "Males", "Males", "Females", "Females", 
"Females", "Females", "Females", "Females", "Females", "Females", 
"Females", "Females"), Value = c(-6, -13, -13, -15, -17, -15, 
-11, -6, -3, -1, 6, 12, 12, 14, 16, 15, 12, 7, 4, 2)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

Code:

library(tidyverse)
library(plotly)

    # Plot
        gg_pop_hisp = ggplot(pop_hisp_df, aes( x = forcats::as_factor(age_group), y = Value, fill = Type))  
          geom_bar(data = subset(pop_hisp_df, Type == "Females"), stat = "identity")   
          geom_text(aes(label = Value, "%"))   
          geom_bar(data = subset(pop_hisp_df, Type == "Males"), stat = "identity")  
          scale_y_continuous(limits=c(-20,20),
                             breaks=c(-15,-10,0,10,15),
                             labels=paste0(c(15,10,0,10,15),"%"))            # CHANGE
          scale_fill_manual(name = "", values = c("Females"="#FC921F", "Males"="#149ECE"), labels = c("Females", "Males"))  
          ggtitle("HISPANIC POPULATION BY GENDER AND AGE GROUP")  
          labs(x = "AGE GROUPS", y = "PERCENTAGE POPULATION", fill = "Gender")  
          theme_minimal()  
          theme(legend.position="bottom")  
          coord_flip()  
        
        # Interactive
        ggplotly(gg_pop_hisp) %>% 
          layout(
            legend = list(
              orientation = 'h', x = 0.3, y = -0.3, 
              title = list(text = '')

CodePudding user response:

You had a typo in the geom_text aes (you forgot a paste0), which caused the % to be interpreted as the desired y-value. Adding a paste0() fixes it. I needed to do abs in order for the minus sign to disappear on the left-hand side.

# Plot
gg_pop_hisp = ggplot(pop_hisp_df, aes( x = forcats::as_factor(age_group), y = Value, fill = Type))  
  geom_bar(data = subset(pop_hisp_df, Type == "Females"), stat = "identity")   
  geom_bar(data = subset(pop_hisp_df, Type == "Males"), stat = "identity")  
  geom_text(aes(label = paste0(abs(Value), "%")))   
  scale_y_continuous(limits=c(-20,20),
                     breaks=c(-15,-10,0,10,15),
                     labels=paste0(c(15,10,0,10,15),"%"))            # CHANGE
  scale_fill_manual(name = "", values = c("Females"="#FC921F", "Males"="#149ECE"), labels = c("Females", "Males"))  
  ggtitle("HISPANIC POPULATION BY GENDER AND AGE GROUP")  
  labs(x = "AGE GROUPS", y = "PERCENTAGE POPULATION", fill = "Gender")  
  theme_minimal()  
  theme(legend.position="bottom")  
  coord_flip()  

# Interactive
ggplotly(gg_pop_hisp) %>% 
  layout(
    legend = list(
      orientation = 'h', x = 0.3, y = -0.3, 
      title = list(text = '')
    )
  )

enter image description here

  • Related