0

I am trying to reproduce the simple population pyramid using ggplot2. I have a question following this thread: Question on how to draw back-to-back plot using R and ggplot2

What I have works almost fine but the 2 plots are not aligned to the center scale. How can I correct this on a similar scale?

Below my code, data structure (reads in as a text file) and output plot? Also I introduced a reproducible example. How can I make 'bold' the scale (eg. center values)?

    library(ggplot2)
    library(plyr)
    library(gridExtra)


    structure(list(serial = c(11080911, 19210711, 45051018, 15110802, 
13190105, 12140718, 14300612, 18131002, 20011206, 17031104), 
    DMSex = c(2, 2, 1, 2, 2, 2, 2, 2, 2, 1), dtotac = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("[3,20]", 
    "(20,30]", "(30,40]", "(40,50]", "(50,60]", "(60,70]", "(70,80]", 
    "(80,90]", "(90,100]", "(100,110]", "(110,120]"), class = c("ordered", 
    "factor"))), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))



WorkFactor<- ordered(cut(WorkHours$dtotac, breaks = c(3,seq(20,120,10)), include.lowest = TRUE))
WorkHours$dtotac <- WorkFactor


ggplotWrk <- ggplot(data =WorkHours, aes(x=dtotac))+ggtitle("How many hours do man and women usually work on the week?") + theme(plot.title = element_text(hjust = 0.5)) + theme_bw()

ggplotWrk.female<- ggplotWrk + geom_bar(data=subset(WorkHours, DMSex =='2'), aes( y = ..count../sum(..count..), fill = dtotac)) +
           scale_y_continuous('', labels = scales::percent) +  
          theme(legend.position = 'none', 
                axis.title.y = element_blank(),
                plot.title = element_text(size = 12),
                plot.margin=unit(c(0.1,0.2,0.03, -0.3),"cm"), 
                axis.ticks.y = element_blank(), 
                axis.text.y = theme_bw()$axis.text.y) + 
  ggtitle("Female") + theme(plot.title = element_text(hjust = 0.5)) + coord_flip()

ggplotWrk.male <-  ggplotWrk + geom_bar( data=subset(WorkHours, DMSex == '1'), aes( y = ..count../sum(..count..), fill = dtotac)) +
          scale_y_continuous('', labels = scales::percent, 
                     trans = 'reverse') + theme(legend.position = 'none',
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(), 
        plot.title = element_text(size = 12),
        plot.margin=unit(c(0.1,0.4,0.2,0),"cm")) + 
  ggtitle("Male") + theme(plot.title = element_text(hjust = 0.5))+ 
  coord_flip() + xlab("") 


## Plutting it together


grid.arrange( ggplotWrk.male, ggplotWrk.female,
             widths=c(0.4, 0.4), ncol=2,
             top = textGrob("How many hours do man and women usually work on the week?",gp=gpar(fontsize=18,font=2), vjust=1))

Output:

enter image description here

Also below a reproducible example

    library(ggplot2)
    library(plyr)
    library(gridExtra)

    SerialGenderWork <- data.frame(Type = sample(c('Male', 'Female', 'Female'), 
                                                  11421, replace=TRUE),
                                    dtotac = sample (0:60, 11421, replace=TRUE))

    WrkFactor <- ordered(cut(SerialGenderWork$dtotac, breaks = c(0, seq(20, 60, 15)), 
                             include.lowest = TRUE))

    SerialGenderWorkN$dtotac <- WrkFactor 

    ggplotWrk <- ggplot(data =SerialGenderWorkN, aes(x=dtotac))

    ggplotWrk.female <- ggplotWrk + 
      geom_bar(data=subset(SerialGenderWorkN, Type == 'Female'), 
               aes( y = ..count../sum(..count..), fill = dtotac)) +
      scale_y_continuous('', labels = scales::percent) +
      theme(legend.position = 'none', 
            axis.title.y = element_blank(),
            plot.title = element_text(size = 11.5),
            plot.margin=unit(c(0.1,0.2,0.1,-.1),"cm"), 
            axis.ticks.y = element_blank(), 
            axis.text.y = theme_bw()$axis.text.y) + 
      ggtitle("Female") + 
      theme(plot.title = element_text(hjust = 0.5)) + 
      coord_flip()

    ggplotWrk.male <- ggplotWrk + 
      geom_bar(data=subset(SerialGenderWorkN,Type == 'Male'), 
               aes( y = ..count../sum(..count..), fill = dtotac)) +
      scale_y_continuous('', labels = scales::percent, 
                         trans = 'reverse') + 
      theme(legend.position = 'none',
            axis.text.y = element_blank(),
            axis.ticks.y = element_blank(), 
            plot.title = element_text(size = 11.5),
            plot.margin=unit(c(0.1,0.2,0.1,-.1),"cm")) + 
      ggtitle("Male") + 
      theme(plot.title = element_text(hjust = 0.5)) + 
      coord_flip() + 
      xlab("Work Hours")

    ## Plutting it together
    grid.arrange(ggplotWrk.male, ggplotWrk.female,
                 widths=c(0.4, 0.4), ncol=2)

Output2

M--
  • 25,431
  • 8
  • 61
  • 93
Rfanatic
  • 2,224
  • 1
  • 5
  • 21
  • 2
    I am not exactly sure what should be fixed in your reproducible example. For your example output, it seems that the male part has more categories than the female part on the y-axis, which may explain why they are not aligned. – teunbrand May 30 '19 at 15:28
  • @teunbrand truly appreciate your comment but what do you mean by categories? Do you think is there a better way to represent the findings? – Rfanatic May 30 '19 at 15:35
  • I mean the age brackets for the males have more categories than the females. I count 11 bars (maybe 12 if you count the empty top one) for males and 10 for females. A population pyramid seems a fine representation of your data. – teunbrand May 30 '19 at 15:39
  • @teunbrand How can I remove the WorkFactor variable, I assume this makes everything harder? – Rfanatic May 30 '19 at 15:39
  • @teunbrand sorry for the message but if you look at the (30, 40] interval the plot is not alligned? why? thanks – Rfanatic May 30 '19 at 15:41
  • 2
    probably because your males range from 3-120 and your females from 3-110. I think ggplot might drop unused factor variables from a discrete axis. Could you try adding `scale_x_discrete(drop = FALSE)` to both plots? – teunbrand May 30 '19 at 15:47
  • @teunbrand it works! thank you!!!! – Rfanatic May 30 '19 at 15:51
  • @teunbrand sorry my last question how can I make the scale "bold" – Rfanatic May 30 '19 at 15:52
  • I think by replacing `axis.text.y = theme_bw()$axis.text.y` with `axis.text.y = element_text(face = "bold")`, but I don't know if you want to part with the `theme_bw()` y-axis text format. – teunbrand May 30 '19 at 15:56
  • @teunbrand sorry..if you have time how can I introduce a text above the scale (between male and female) as the code that i am using is grid.arrange( ggplotWrk.male, ggplotWrk.female,widths=c(0.6, 0.4), ncol=2,top = textGrob("How many hours do man and women usually work on the week?",gp=gpar(fontsize=18,font=2), vjust=1)) – Rfanatic May 30 '19 at 16:09

0 Answers0