r - 如何更改分组条形图的顺序?
问题描述
我怎样才能以正确的顺序将条形图从非常低到非常高和人为的?我怎样才能改变图例的位置?
这是我的 R 代码:
barplot(bartab, col=colors()[c(23, 89)], beside=TRUE, legend=rownames(bartab))
解决方案
只需首先订购您的数据。为了更好地控制图例位置,请将其分开。
例子
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
。
推荐阅读
- javascript - 使用 javascript 和 css 将任务列表添加到我的 HTML
- spring-boot - Spring Boot 2.3+ 不再打印有用的错误信息以进行验证(来自 MethodArgumentNotValidException)
- python - 无法导入名称“MultiRNNCell”
- typescript - Vue @Watch 未触发布尔值更改
- authentication - 使用 oauth 2.oc 从 azure 函数调用 web api#
- r - knitr 在 ggplots 中渲染缺少的标签
- python - python3从字符串中提取:)符号
- assembly - AVR 汇编中的“set”指令有什么作用?
- java - 无法将类型“java.lang.String”的属性值转换为所需类型“java.sql.Date”
- java - Android Studio:Java:使用 ID 填充带有按钮的 2D 数组