控制堆叠条形图中条形之间的间距

时间:2013-03-21 08:16:14

标签: r plot

当我在R中绘制堆积的条形图时,条形图之间的间隙相等,x轴值仅用作标签。我希望根据x轴值将条形放置得更近或更远。有人可以帮我用R?

来获得这个情节

修改

# data.frame newtest
      A   B   C   D
100  0.2 0.3 0.1 0.4
400  0.3 0.5 0.1 0.1
500  0.1 0.3 0.4 0.2
600  0.4 0.2 0.2 0.2
1000 0.1 0.5 0.1 0.3
1500 0.3 0.2 0.2 0.3
1600 0.4 0.1 0.3 0.2
1700 0.1 0.1 0.7 0.1
2500 0.3 0.2 0.1 0.4

# plot
barplot(t(as.matrix(newtest)), col = c("cyan", "lightblue", "yellow", "green"), 
          legend = colnames(newtest), cex.main = 0.5, cex.axis = 0.5, 
          cex.lab = 0.5, lwd = 0.02)

这是情节: barplot

条形图按行名称标记。但我希望400,500,600的柱子彼此靠近,空的空间代表700,800,900 没有块,然后是1000的栏,然后空的空间到1500,1500,1600的栏,1700

1 个答案:

答案 0 :(得分:2)

鉴于barplot中的x轴代表一个分类变量,我认为除了在数据中引入额外的虚拟观察之外,还有其他解决方案:

extracolnames <- setdiff(seq(100,2500,by=100) ,rownames(newtest))
extracols <- replicate(length(extracolnames), rep(0,4))
colnames(extracols) <- extracolnames
dat <- cbind(t(as.matrix(newtest)), extracols)
dat <- dat[,order(as.numeric(colnames(dat)))]
barplot(dat, col=c("cyan","lightblue","yellow","green"), legend=colnames(newtest), cex.main=0.5, cex.axis=0.5, cex.lab=0.5, lwd=0.02)