如何在R(堆叠式barplot)的barplot中合并列标签? [英] How to merge column labels in barplot in R (a stacked barplot)?

查看:78
本文介绍了如何在R(堆叠式barplot)的barplot中合并列标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中的数据如下:

    1-12 1-12 1-15 1-15 1-20 1-20 2-6 2-6 3-1-1 3-1-1 3-1 3-1 3-2 3-2 3-3 3-3
N    0    0   14    0   17    0   9   0    27     0   9   0  13   0  33   0
P    0    0    0   12    0   12   0   5     0    13   0   6   0   0   0   9
F    0    0    0    0    0    0   0   2     0    14   0   0   0   6   0  20

根据dput(data),R中的结果为:

By dput(data) the result in R is:

    structure(c(0L, 0L, 0L, 0L, 0L, 0L, 14L, 0L, 0L, 0L, 12L, 0L, 
17L, 0L, 0L, 0L, 12L, 0L, 9L, 0L, 0L, 0L, 5L, 2L, 27L, 0L, 0L, 
0L, 13L, 14L, 9L, 0L, 0L, 0L, 6L, 0L, 13L, 0L, 0L, 0L, 0L, 6L, 
33L, 0L, 0L, 0L, 9L, 20L), .Dim = c(3L, 16L), .Dimnames = list(
c("N", "P", "F"), c("1-12", "1-12", "1-15", "1-15", "1-20", 
"1-20", "2-6", "2-6", "3-1-1", "3-1-1", "3-1", "3-1", "3-2", 
"3-2", "3-3", "3-3")))

我的代码是:

barplot(data,space=c(1,0.25),legend=rownames(data),col=c('white','black','grey'),las=2)

它看起来像一个普通的条形图,每列底部都有一个标签...

it looks like a normal bar plot, each column has a label at the bottom...

但是,x轴上的标签太多,我想将两列的名称合并为一个(因为它们具有相同的名称),即在前两列的中间,只有底部有一个标签"1-12",因此总共有八个标签.我该如何进行更改?

But, there are too many labels in the x-axis, and I want to merge the names of two columns into one(since they have the same name), i.e, in the middle of first two columns, there is only one label "1-12" at the bottom, so there will be eight labels in total. How can I make this change?

非常感谢!

推荐答案

如果只希望在每个组下放置居中标签,则可以执行以下操作:

If you just wish to have the centred labels under each group, you could do something like:

# suppress the x-axis and save your original plot's bar locations
bp <- barplot(data,space=c(1,0.25),legend=rownames(data),
             col=c('white','black','grey'),las=2,
             xaxt="n")

# draw a new axis using these values
axis(1,at=rowMeans(matrix(bp,ncol=2,byrow=TRUE)),
     labels=unique(colnames(data)),lty=0)

当然可以忽略重叠的图例...可以通过在原始的 barplot 调用中使用 legend.args 更好地将其放置来解决.

Ignoring the overlapping legend of course... which can be fixed by placing it better using legend.args in the original barplot call

这篇关于如何在R(堆叠式barplot)的barplot中合并列标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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