使用R和ggplot2绘制金字塔图 [英] drawing pyramid plot using R and ggplot2
问题描述
我需要绘制一张金字塔图,就像附上的那张图一样。
我从 ,任何人都可以给我一些使用ggplot做这个提示吗?感谢!
这实质上是一个背靠背的barplot,类似于使用 ggplot2
在优秀的学习者博客中: http://learnr.wordpress.com/2009/09/24/ggplot2-back-to-back-bar-charts/
你可以在这些图中使用 coord_flip
,但我不确定你是如何得到它以共享上面两张图之间的y轴标签的。下面的代码应该让你足够接近原始数据:
首先创建一个数据样本数据框,将Age列转换为具有所需断点的因子:
require(ggplot2)
df < - data.frame(Type = sample(c('Male', '女','女'),1000,替换= TRUE),
年龄=样本(18:60,1000,替换= TRUE))
AgesFactor< - ordered (df $ Age,break = c(18,seq(20,60,5)),
include.lowest = TRUE))
df $年龄< - AgesFactor
现在开始构建情节:使用相应的数据子集创建男性和女性情节,压制图例等
gg < - ggplot(data = df,aes(x = Age))
gg.male< - gg +
geom_bar(subset =。(Type =='Male'),
aes(y = ..count ../ sum(.. count ..),fill =年龄))+
scale_y_continuous('',formatter ='perc ')+
opts(legend.position ='none')+
opts(axis.text.y = theme_blank(),axis.title.y = theme_blank())+
opts(title ='Male',plot.title = theme_text(size = 10))+
coord_flip()
对于女性情节,使用 trans =reverse
...颠倒'Percent'轴。
gg.female< - gg +
geom_bar(subset =。(Type =='Female'),
aes(y = .. count ../sum(..count ..),fill = Age))+
scale_y_continuous('',formatter ='percent',trans ='reverse')+
opts(legend.position = 'none')+
opts(axis.text.y = theme_blank(),
axis.title.y = theme_blank(),
title ='女')+
opts(plot.title = theme_text(size = 10))+
coord_flip()
现在使用 geom_text
创建一个只显示年龄段的绘图,但也可以使用一个虚拟的 geom_bar
以确保此图中年龄轴的缩放比例与男性和女性情节中的缩放一致:
gg.ages < - gg +
geom_bar(subset =。(Type =='Male'),aes(y = 0,fill = alpha('white',0)))+
geom_text(aes(y = 0,label = as.character(Age)),size = 3)+
coord_flip()+
opts(title ='Ages',
legend。 position ='none',
axis.text.y = theme_blank(),
axis.title.y = theme_blank(),
axis.text.x = theme_blank(),
axis.ticks = theme_blank(),
plot.title = theme_text(size = 10))
最后,使用Hadley Wickham的书中的方法在网格上排列图:
grid.newpage()
pushViewport(viewport(layout = grid.layout(1,3,widths = c(.4,.2,.4))))
vplayout< - function(x,y)viewport(layout.pos.row = x,layout.pos.col = y)
pri nt(gg.female,vp = vplayout(1,1))
print(gg.ages,vp = vplayout(1,2))
print(gg.male,vp = vplayout(1, 3))
I need to draw a pyramid plot, like the one attached.
I found an example using R (but not ggplot) from here, can anyone give me some hint on doing this using ggplot? Thanks!
This is essentially a back-to-back barplot, something like the ones generated using ggplot2
in the excellent learnr blog: http://learnr.wordpress.com/2009/09/24/ggplot2-back-to-back-bar-charts/
You can use coord_flip
with one of those plots, but I'm not sure how you get it to share the y-axis labels between the two plots like what you have above. The code below should get you close enough to the original:
First create a sample data frame of data, convert the Age column to a factor with the required break-points:
require(ggplot2)
df <- data.frame(Type = sample(c('Male', 'Female', 'Female'), 1000, replace=TRUE),
Age = sample(18:60, 1000, replace=TRUE))
AgesFactor <- ordered( cut(df$Age, breaks = c(18,seq(20,60,5)),
include.lowest = TRUE))
df$Age <- AgesFactor
Now start building the plot: create the male and female plots with the corresponding subset of the data, suppressing legends, etc.
gg <- ggplot(data = df, aes(x=Age))
gg.male <- gg +
geom_bar( subset = .(Type == 'Male'),
aes( y = ..count../sum(..count..), fill = Age)) +
scale_y_continuous('', formatter = 'percent') +
opts(legend.position = 'none') +
opts(axis.text.y = theme_blank(), axis.title.y = theme_blank()) +
opts(title = 'Male', plot.title = theme_text( size = 10) ) +
coord_flip()
For the female plot, reverse the 'Percent' axis using trans = "reverse"
...
gg.female <- gg +
geom_bar( subset = .(Type == 'Female'),
aes( y = ..count../sum(..count..), fill = Age)) +
scale_y_continuous('', formatter = 'percent', trans = 'reverse') +
opts(legend.position = 'none') +
opts(axis.text.y = theme_blank(),
axis.title.y = theme_blank(),
title = 'Female') +
opts( plot.title = theme_text( size = 10) ) +
coord_flip()
Now create a plot just to display the age-brackets using geom_text
, but also use a dummy geom_bar
to ensure that the scaling of the "age" axis in this plot is identical to those in the male and female plots:
gg.ages <- gg +
geom_bar( subset = .(Type == 'Male'), aes( y = 0, fill = alpha('white',0))) +
geom_text( aes( y = 0, label = as.character(Age)), size = 3) +
coord_flip() +
opts(title = 'Ages',
legend.position = 'none' ,
axis.text.y = theme_blank(),
axis.title.y = theme_blank(),
axis.text.x = theme_blank(),
axis.ticks = theme_blank(),
plot.title = theme_text( size = 10))
Finally, arrange the plots on a grid, using the method in Hadley Wickham's book:
grid.newpage()
pushViewport( viewport( layout = grid.layout(1,3, widths = c(.4,.2,.4))))
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
print(gg.female, vp = vplayout(1,1))
print(gg.ages, vp = vplayout(1,2))
print(gg.male, vp = vplayout(1,3))
这篇关于使用R和ggplot2绘制金字塔图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!