首页 文章

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

提问于
浏览
1

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

Edit:

# 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 回答

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

相关问题