Plotrix , ggplot . @eipi10 () drawing-pyramid-plot-using-r-and-ggplot2, "", "" "". , . , - mirroring-axis-ticks-in-ggplot2. ( "" , , " " ) gtable.
: ggplot2 2.2.1
library(plyr)
library(ggplot2)
library(scales)
library(gtable)
library(stringr)
library(grid)
mov <-c(23.2,33.5,43.6,33.6,43.5,43.5,43.9,33.7,53.9,43.5,43.2,42.8,22.2,51.8,
41.5,31.3,60.7,50.4)
fov<-c(13.2,9.4,13.5,13.5,13.5,23.7,8,3.18,3.9,3.16,23.2,22.5,22,12.7,12.5,
12.3,10,0.8)
fob<-c(23.2,33.5,43.6,33.6,43.5,23.5,33.9,33.7,23.9,43.5,18.2,22.8,22.2,31.8,
25.5,25.3,31.7,28.4)
mob<-c(13.2,9.4,13.5,13.5,13.5,23.7,8,3.18,3.9,3.16,23.2,22.5,22,12.7,12.5,
12.3,10,0.8)
labs<-c("uk","scotland","france","ireland","germany","sweden","norway",
"iceland","portugal","austria","switzerland","australia",
"new zealand","dubai","south africa",
"finland","italy","morocco")
df = data.frame(labs=rep(labs,4), values=c(mov, mob, fov, fob),
sex=rep(c("Male", "Female"), each=2*length(fov)),
bmi = rep(rep(c("Overweight", "Obese"), each=length(fov)),2))
labs.order = ddply(df, .(labs), summarise, sum=sum(values))
labs.order = labs.order$labs[order(labs.order$sum)]
df$labs = factor(df$labs, levels=labs.order)
theme = theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
plot.title = element_text(size = 10, hjust = 0.5))
ggM <- ggplot(data = subset(df, sex == 'Male'), aes(x=labs)) +
geom_bar(aes(y = values/100, fill = bmi), stat = "identity") +
scale_y_continuous('', labels = percent, limits = c(0, 1), expand = c(0,0)) +
labs(x = NULL) +
ggtitle("Male") +
coord_flip() + theme +
theme(plot.margin= unit(c(1, 0, 0, 0), "lines"))
gtM <- ggplotGrob(ggM)
leg = gtM$grobs[[which(gtM$layout$name == "guide-box")]]
legPos = gtM$layout$l[grepl("guide", gtM$layout$name)]
gtM = gtM[, -c(legPos-1,legPos)]
ggF <- ggplot(data = subset(df, sex == 'Female'), aes(x=labs)) +
geom_bar(aes(y = values/100, fill = bmi), stat = "identity") +
scale_y_continuous('', labels = percent, trans = 'reverse',
limits = c(1, 0), expand = c(0,0)) +
labs(x = NULL) +
ggtitle("Female") +
coord_flip() + theme +
theme(plot.margin= unit(c(1, 0, 0, 1), "lines"))
gtF <- ggplotGrob(ggF)
gtF = gtF[, -c(legPos-1,legPos)]
rn <- which(gtF$layout$name == "axis-l")
axis.grob <- gtF$grobs[[rn]]
axisl <- axis.grob$children[[2]]
yaxis = axisl$grobs[[2]]
yaxis$x = yaxis$x - unit(1, "npc") + unit(2.75, "pt")
panelPos = gtF$layout[grepl("panel", gtF$layout$name), c('t','l')]
gtF <- gtable_add_cols(gtF, gtF$widths[3], panelPos$l)
gtF <- gtable_add_grob(gtF, yaxis, t = panelPos$t, l = panelPos$l+1)
gtF = gtF[, -c(2,3)]
fontsize = 3
ggC <- ggplot(data = subset(df, sex == 'Male'), aes(x=labs)) +
geom_bar(stat = "identity", aes(y = 0)) +
geom_text(aes(y = 0, label = labs), size = fontsize) +
ggtitle("Country") +
coord_flip() + theme_bw() + theme +
theme(panel.border = element_rect(colour = NA))
gtC <- ggplotGrob(ggC)
Title = gtC$grobs[[which(gtC$layout$name == "title")]]
gtC = gtC$grobs[[which(gtC$layout$name == "panel")]]
gt = cbind(gtF, gtM, size = "first")
maxlab = labs[which(str_length(labs) == max(str_length(labs)))]
gt = gtable_add_cols(gt, sum(unit(1, "grobwidth", textGrob(maxlab, gp = gpar(fontsize = fontsize*72.27/25.4))), unit(5, "mm")),
pos = length(gtF$widths))
gt = gtable_add_grob(gt, gtC, t = panelPos$t, l = length(gtF$widths) + 1)
titlePos = gtF$layout$l[which(gtF$layout$name == "title")]
gt = gtable_add_grob(gt, Title, t = titlePos, l = length(gtF$widths) + 1)
gt = gtable_add_cols(gt, sum(leg$width), -1)
gt = gtable_add_grob(gt, leg, t = panelPos$t, l = length(gt$widths))
grid.newpage()
grid.draw(gt)
