R中的2层条形图 [英] 2-layer barplot in R

查看:70
本文介绍了R中的2层条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在R中构建一个(有点)复杂的条形图.这个想法是要覆盖两个图,其中一个条形图比另一个条形图要宽,这样两个条形图总是可见的.

这就是我现在拥有的:

 #生成数据年龄= c(50,55,60,65,70)#年龄组男性= c(15.4,24.3,37.0,54.6,71.1)#男性死亡率女性= c(8.4,13.6,19.3,35.1,50.0)#女性死亡率死亡率=矩阵(c(男性,女性),nrow =长度(年龄),ncol = 2,暗名=列表(年龄,c(男性",女性")))#生成数据barplot(Deathrate [,1],col ="red")par(new = TRUE)barplot(Deathrate [,2],space = 1,col ="blue") 

现在,如您所见,显示了两个图,但是尽管两个中间条都很好地重叠并居中,但其他所有条都没有居中.例如,最右边的蓝色条显示在最右边的红色条的边缘.

有人有一个简单的解决方案来将所有条形图居中吗?

谢谢菲利普

PS,我知道图表不太好(图例重叠等)....

解决方案

您可以将 add = TRUE 以及相应的 width space 值一起使用

代码>.

  barplot(Deathrate [,1],col ="red")barplot(Deathrate [,2],width = 0.5,space = c(0.9,1.4,1.4,1.4,1.4),col ="blue",add = TRUE) 

I am trying to build a (somewhat) complex barplot in R. The idea is to overlay two charts, whereby the bars in one are wider than in the other one, such that both bars are always visible.

Here's what I have right now:

# GENERATE THE DATA  
Age = c(50, 55, 60, 65, 70)                                     # Age groups  
Male = c(15.4, 24.3, 37.0, 54.6, 71.1)                          # Death rates for males  
Female = c(8.4, 13.6, 19.3, 35.1, 50.0)                         # Death rates for females  
Deathrate = matrix(c(Male,Female), nrow=length(Age), ncol=2, dimnames=list(Age, c("Male","Female")))         

# GENERATE THE DATA  
barplot(Deathrate[,1], col="red")
par(new=TRUE)
barplot(Deathrate[,2], space=1, col="blue")

Now, as you can see, the two plots are shown, but while both middle bars are nicely overlapping and centered, all other bars are not centered. For example, the rightmost blue bar is shown on the edge of the rightmost red bar.

Does anyone have a simple solution to center all bars?

Thanks, Philipp

PS I know that the chart isn't pretty (overlapping legends etc.)....

解决方案

You can use add=TRUE together with apropriate values for width and space.

barplot(Deathrate[,1], col="red")
barplot(Deathrate[,2], width=0.5, space=c(0.9, 1.4, 1.4, 1.4, 1.4), col="blue", add=TRUE)

这篇关于R中的2层条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆