Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R ggplot2 add additional x-axis labels

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 = '')
      )
    )

>Solution :

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),"%")
  )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading