R:水平条形图,每个条形旁边都有y轴标签

时间:2013-06-06 12:34:21

标签: r plot label axis

我想设计一个包含36组3个水平条的条形图。每组3个旁边应该有一个标签。

我的代码非常混乱(我第一次使用R),所以我希望它可以使用一些虚拟数据......

反正:

Transcomp <- matrix(nrow=3, ncol=36)     # matrix
colnamesbarplot <- colnames(transComp)   # should be used as barplot labels
barplot <- 
barplot(transComp, 
    space=c(0,2),
    legend.text=TRUE,
    beside=TRUE,
    horiz=TRUE,
    density=NA,
    col=c("red1","red4","green3"),
    xlab="crimes per 100,000 inhabitants",
    ylab="districts and years",
    axes=TRUE
            )

我找不到允许我直接在栏旁边显示列名的参数(我不在乎它们是在栏的左边还是右边)...... 问题可能是条形图的数量?

add text to horizontal barplot in R, y-axis at different scale?labeling in barplot()以及Axis labels for each bar and each group in bar charts with dodged groups中的回答并没有让我得到我想要的地方......

感谢您的帮助!

1 个答案:

答案 0 :(得分:14)

查看?barplot个参数names.arg

一些示例数据:

transComp <- matrix(sample(3*36), nrow=3, ncol=36)     
colnamesbarplot <- as.character(1:36)

条形图:

barplot(transComp,space=c(0,2),legend.text=TRUE,beside=TRUE,horiz=TRUE,
    density=NA,
    col=c("red1","red4","green3"),
    xlab="crimes per 100,000 inhabitants",
    ylab="districts and years",
    axes=TRUE, names.arg=colnamesbarplot, cex.names=0.5, las=1)

由于您要绘制许多列,因此应设置cex.names以使标注更小。 参数las=1将标签旋转90度。

相关问题