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

Setting proper title on Pyramid plot

I am trying to plot a pyramid plot. Below you can see data and code :

library(plotly)
library(dplyr)

df1<-data.frame(  
                Age=c("Y_0_4","Y_5_9","Y10_14","Y15_19","Y20_24","Y25_29","Y30_34","Y35_39","Y40_44","Y45_49","Y50_54","Y55_59","Y_0_4","Y_5_9","Y10_14","Y15_19","Y20_24","Y25_29","Y30_34","Y35_39","Y40_44","Y45_49","Y50_54","Y55_59"),
                variable=c("Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Female","Female","Female","Female","Female","Female","Female","Female","Female","Female","Female","Female"),
                value=c(158,196,168,156,140,693,854,892,904,925,817,257,170,204,178,150,817,763,784,818,823,766,779,349)
                
)
  
df1$color <- factor(df1$variable, labels = c('#1f77b4','#ff7f0e'))


df_pyramid<-df1%>% 
  mutate(value = ifelse(test = variable == "Male", yes = -value, no = value)) %>%
  mutate(abs_pop = abs(value)) %>%
  plot_ly(x= ~value, y=~Age, marker = list(color = ~color), name = ~variable) %>% 
  add_bars(orientation = 'h', hoverinfo = 'text', text = ~abs_pop) %>%
  layout(title = "Structure by age and sex",bargap = 0.1, barmode = 'overlay',
         xaxis = list(tickmode = 'array', tickvals = c(-15000, -10000, -5000, 0, 5000, 10000, 15000),
                      ticktext = c('15000', '10000', '5000', '0', '5000', '10000', '15000')),
         annotations =
           list(x = 0, y = -0.1,
                text = "Source: Some institution",
                showarrow = F,
                xref='paper',
                yref='paper'))
df_pyramid  

This code produce pyramid plot same as the picture below. But below of this chart on the x-axis, you can see the title of the axis value. So can anybody help me with how to remove this title but without changes on the x-axis?

enter image description here

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

>Solution :

You can use title="" argument in layout of xaxis to remove that title like this:

library(plotly)
library(dplyr)

df1<-data.frame(  
  Age=c("Y_0_4","Y_5_9","Y10_14","Y15_19","Y20_24","Y25_29","Y30_34","Y35_39","Y40_44","Y45_49","Y50_54","Y55_59","Y_0_4","Y_5_9","Y10_14","Y15_19","Y20_24","Y25_29","Y30_34","Y35_39","Y40_44","Y45_49","Y50_54","Y55_59"),
  variable=c("Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Female","Female","Female","Female","Female","Female","Female","Female","Female","Female","Female","Female"),
  value=c(158,196,168,156,140,693,854,892,904,925,817,257,170,204,178,150,817,763,784,818,823,766,779,349)
  
)

df1$color <- factor(df1$variable, labels = c('#1f77b4','#ff7f0e'))


df_pyramid<-df1%>% 
  mutate(value = ifelse(test = variable == "Male", yes = -value, no = value)) %>%
  mutate(abs_pop = abs(value)) %>%
  plot_ly(x= ~value, y=~Age, marker = list(color = ~color), name = ~variable) %>% 
  add_bars(orientation = 'h', hoverinfo = 'text', text = ~abs_pop) %>%
  layout(title = "Structure by age and sex",bargap = 0.1, barmode = 'overlay',
         xaxis = list(tickmode = 'array', tickvals = c(-15000, -10000, -5000, 0, 5000, 10000, 15000),
                      ticktext = c('15000', '10000', '5000', '0', '5000', '10000', '15000'),
                      title = ""),
         annotations =
           list(x = 0, y = -0.1,
                text = "Source: Some institution",
                showarrow = F,
                xref='paper',
                yref='paper'))
df_pyramid  

Created on 2022-07-29 by the reprex package (v2.0.1)

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