【问题标题】:How to change the order of grouped barplots?如何更改分组条形图的顺序?
【发布时间】:2019-02-22 08:30:39
【问题描述】:

如何将条形图以正确的顺序从非常低到非常高和人为的顺序排列? 以及如何更改图例的位置?

这是我的 R 代码:

barplot(bartab, col=colors()[c(23, 89)], beside=TRUE, legend=rownames(bartab))

【问题讨论】:

  • 使用barplot,您可以更改数据框中组的顺序,这应该会更改条形的顺序。将来尝试提供reproducible code

标签: r plot bar-chart figure


【解决方案1】:

首先对您的数据进行排序。为了更好地控制图例位置,请将其分开。

示例

df1 <- mtcars[grep("^Merc", rownames(mtcars)), c(1, 2)]
df1 <- df1[order(df1$mpg), ]  # this orders your data by "mpg", look into `?order`

# plot
barplot(t(df1), col=c("blue", "green"), border="white", font.axis=2,
        beside=TRUE, xlab="group", font.lab=2)
legend("topleft", legend=c("mpg", "cyl"), pch=15, col=c("blue", "green"))

注意:

如文档所述,还有其他可能的字符串来指定图例位置:

The location may also be specified by setting x to a single keyword from 
the list "bottomright", "bottom", "bottomleft", "left",
"topleft", "top", "topright", "right" and "center".

您还可以指定精确坐标,请参阅?legend

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    相关资源
    最近更新 更多