如何更改分组条形图的顺序?

问题描述 投票:0回答:1

我怎样才能将条形图从正常的顺序带到非常低和非常高的人为因素?我怎样才能改变传奇的位置?

enter image description here

这是我的R代码:

barplot(bartab, col=colors()[c(23, 89)], beside=TRUE, legend=rownames(bartab))
r plot bar-chart figure
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.