y轴切割成组的条形图 [英] Grouped barplot with cut y axis

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

问题描述

我正在尝试使用成组的条形图和一个y轴进行绘制.但是我似乎无法同时兼得.使用此数据:

I am trying to make a plot with grouped barplots and a cut y axis. However I don't seem to be able to get both. Using this data:

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

我可以得到分组的条形图,

I can get the grouped barplots like:

barplot( d, beside = TRUE)

然后我可以使用以下方法获取切割的y轴:

I can then get the cut y-axis using:

# 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 ...的标签.我怎样才能两者兼得?

However, then I loose the grouping and the A, B, C... labeling. How can I get both?

推荐答案

您可以手动执行此操作.像barplot一样,?gap.barplot返回条形图的中心位置.使用这些来添加标签.

You could do this manually. Like barplot, ?gap.barplot returns the center positions of the bars. Use these to add the labels.

像常规barplot一样,在组之间使用space间距似乎无效.我们可以使用一行NA来破解一个空间.

Using space for spacing between groups as in regular barplot does not seem to work. We can use a row of NAs to hack a space.

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

这篇关于y轴切割成组的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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