Home > front end >  R ggplot2 add additional x-axis labels
R ggplot2 add additional x-axis labels

Time:08-13

Based on the code and data below, is there a way to add 15% after each 10% to show that the values are greater/less than or equal to /- 15% on the x-axis?

Please note that one of the datasets does not have 15 in the Value column

I tried using scale_x_discrete with the limits argument, but it doesn't work.

Desired x-axis order on both the plots:

15% 10% 0 10% 15%

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"))

pop_gen_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, -12, -12, -14, -13, -14, 
-13, -9, -4, -2, 6, 11, 11, 13, 13, 14, 13, 10, 5, 3)), 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_bar(data = subset(pop_hisp_df, Type == "males"), stat = "identity")   
      scale_y_continuous(labels = function(z) paste0(abs(z), "%"))            # 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 = "PERCENTAGE POPULATION", y = "AGE GROUPS", fill = "Gender")  
      theme_minimal()  
      theme(legend.position="bottom")  
      coord_flip()

gg_pop_gen = ggplot(pop_gen_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")   
  scale_y_continuous(labels = function(z) paste0(abs(z), "%"))            # CHANGE
  scale_fill_manual(name = "", values = c("Females"="#ED5151", "Males"="#6B6BD6"), labels = c("Females", "Males"))  
  ggtitle("TOTAL POPULATION BY AGE AND GENDER")  
  labs(x = "PERCENTAGE POPULATION", y = "AGE GROUPS", fill = "Gender")  
  theme_minimal()  
  theme(legend.position="bottom")  
  coord_flip()
    
    # Interactive and place legend at the bottom
    ggplotly(gg_pop_hisp) %>% 
      layout(
        legend = list(
          orientation = 'h', x = 0.3, y = -0.1, 
          title = list(text = '')
          )
        )

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

CodePudding user response:

You can change your scale_y_continuous for both plots to :

  scale_y_continuous(
    limits=c(-20,20),
    breaks=c(-15,-10,0,10,15),
    labels=paste0(c(15,10,0,10,15),"%")
  )

Full 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_bar(data = subset(pop_hisp_df, Type == "Males"), stat = "identity")   
  #scale_y_continuous(labels = function(z) paste0(abs(z), "%"))            # CHANGE
  scale_y_continuous(
    limits=c(-20,20),
    breaks=c(-15,-10,0,10,15),
    labels=paste0(c(15,10,0,10,15),"%")
  )   
  scale_fill_manual(name = "", values = c("Females"="#FC921F", "Males"="#149ECE"), labels = c("Females", "Males"))  
  ggtitle("HISPANIC POPULATION BY GENDER AND AGE GROUP")  
  labs(x = "PERCENTAGE POPULATION", y = "AGE GROUPS", fill = "Gender")  
  theme_minimal()  
  theme(legend.position="bottom")  
  coord_flip()

gg_pop_hisp

gg_pop_gen = ggplot(pop_gen_df, aes(x = forcats::as_factor(age_group), y = Value, fill = Type))   
  geom_bar(data = subset(pop_gen_df, Type == "Females"), stat = "identity")   
  geom_bar(data = subset(pop_gen_df, Type == "Males"), stat = "identity")   
  #scale_y_continuous(labels = function(z) paste0(abs(z), "%"))            # CHANGE
  scale_y_continuous(
    limits=c(-20,20),
    breaks=c(-15,-10,0,10,15),
    labels=paste0(c(15,10,0,10,15),"%")
  )   
  scale_fill_manual(name = "", values = c("Females"="#ED5151", "Males"="#6B6BD6"), labels = c("Females", "Males"))  
  ggtitle("TOTAL POPULATION BY AGE AND GENDER")  
  labs(x = "PERCENTAGE POPULATION", y = "AGE GROUPS", fill = "Gender")  
  theme_minimal()  
  theme(legend.position="bottom")  
  coord_flip()

# Interactive and place legend at the bottom
ggplotly(gg_pop_hisp) %>% 
  layout(
    legend = list(
      orientation = 'h', x = 0.3, y = -0.1, 
      title = list(text = '')
    )
  )

ggplotly(gg_pop_gen) %>% 
  layout(
    legend = list(
      orientation = 'h', x = 0.3, y = -0.3, 
      title = list(text = '')
    )
  )
  • Related