【问题标题】:R: horizontal barplot with y-axis-labels next to every barR:水平条形图,每个条形旁边都有 y 轴标签
【发布时间】:2013-06-02 11:15:30
【问题描述】:

我想设计一个包含 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 中的答案无法让我到达我想得到的地方...

感谢您的帮助!

【问题讨论】:

    标签: r plot label axis


    【解决方案1】:

    查看?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 度。

    【讨论】:

    • 是的!所以las是我让它工作所缺少的! Stackoverflow,你太棒了!
    猜你喜欢
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    相关资源
    最近更新 更多