如何更改成组的板凳的顺序?

时间:2019-02-22 08:30:39

标签: r plot bar-chart figure

我如何才能将条形图从低到高并按人为顺序排列正确? 以及如何更改图例的位置?

enter image description here

这是我的R代码:

setTimeout

1 个答案:

答案 0 :(得分: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"))

enter image description here

注意:

文档中指出,还有其他可能的字符串可以指定图例位置:

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