首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >切y轴成组桶形图

问切y轴成组桶形图
EN

Stack Overflow用户
提问于 2014-06-13 09:39:57
回答 2查看 9.7K关注 0票数 7

我试图用成组的条形图和切成的y轴来制作一个图。然而,我似乎不能两者兼得。使用这些数据:

代码语言:javascript
复制
d = t(matrix( c(7,3,2,3,2,2,852,268,128,150,
              127,74,5140,1681,860,963,866,
              470,26419,8795,4521,5375,4514,2487),
            nrow=6, ncol=4 ))
colnames(d)=c("A", "B", "C", "D", "E", "F")

我可以得到这样的分组酒吧间:

代码语言:javascript
复制
barplot( d, beside = TRUE)

​

​

然后,我可以得到切割y轴使用:

代码语言:javascript
复制
# install.packages('plotrix', dependencies = TRUE)
require(plotrix)
gap.barplot( as.matrix(d), 
             beside = TRUE, 
             gap=c(9600,23400), 
             ytics=c(0,3000,6000,9000,24000,25200,26400) )

​

​

然后我就把A,B,C……贴标签。我怎么能两者兼得?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-13 09:55:45

你可以手动完成。与barplot一样,?gap.barplot返回条形图的中心位置。使用这些来添加标签。

使用space作为组之间的间隔(就像在常规barplot中那样)似乎不起作用。我们可以用一行NAs来黑一个空间。

代码语言:javascript
复制
d = t(matrix( c(7,3,2,3,2,2,852,268,128,150,
                                127,74,5140,1681,860,963,866,
                                470,26419,8795,4521,5375,4514,2487),
                            nrow=6, ncol=4 ))
colnames(d)=c("A", "B", "C", "D", "E", "F")

# add row of NAs for spacing
d=rbind(NA,d)

# install.packages('plotrix', dependencies = TRUE)
require(plotrix)

# create barplot and store returned value in 'a'
a = gap.barplot(as.matrix(d), 
                gap=c(9600,23400), 
                ytics=c(0,3000,6000,9000,24000,25200,26400),
                xaxt='n') # disable the default x-axis

# calculate mean x-position for each group, omitting the first row 
# first row (NAs) is only there for spacing between groups
aa = matrix(a, nrow=nrow(d))
xticks = colMeans(aa[2:nrow(d),])

# add axis labels at mean position
axis(1, at=xticks, lab=LETTERS[1:6])
票数 7
EN

Stack Overflow用户

发布于 2014-06-13 11:51:53

在考肯巴克尔的回答的帮助下,我终于想到了这一点:

代码语言:javascript
复制
# install.packages('plotrix', dependencies = TRUE)
require(plotrix)

d = t(matrix( c(7,3,2,3,2,2,852,268,128,150,
                127,74,5140,1681,860,963,866,
                470,26419,8795,4521,5375,4514,2487),
              nrow=6, ncol=4 ))

# Hack for grouping (leaves the extra space at the end)
e = as.vector(rbind(d, rep(NA, 6)))[1:29]

a = gap.barplot(ceiling(as.matrix(e/60)), 
                gap=c(160,390),
                col=rep(c(grey.colors(4), 1), 6),
                #space=rep(c(rep(0,3), 1), 6),
                ytics=c(0,50,100,150,400,420,440),
                xaxt='n') # disable the default x-axis

xticks=c(2.5, 7.5, 12.5, 17.5, 22.5, 27.5)

# add axis labels at mean position
axis(1, at=xticks, LETTERS[1:6] )

legend("topright", LETTERS[7:10],
       bty="n",  
       fill=grey.colors(4)) 

​

​

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24202245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档