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