改变离散x尺度的顺序我正在用具有离散x比例尺的ggmap制作一个隐藏的条形图,x轴现在是按字母顺序排列的,但是我需要重新排列它,使它按y轴的值排序(也就是说,最高的条形线将位于左边)。我试过排序或排序,但是结果是对x轴进行排序,而不是分别对条形进行排序。我做错了什么?
3 回答
阿波罗的战车
TA贡献1862条经验 获得超6个赞
library(ggplot2)# Automatic levelsggplot(mtcars, aes(factor(cyl))) + geom_bar()
# Manual levelscyl_table <- table(mtcars$cyl)cyl_levels <- names(cyl_table)[order(cyl_table)]mtcars$cyl2 <- factor(mtcars$cyl, levels = cyl_levels)# Just to be clear, the above line is no different than:# mtcars$cyl2 <- factor(mtcars$cyl, levels = c("6","4","8"))# You can manually set the levels in whatever order you please. ggplot(mtcars, aes(cyl2)) + geom_bar()
reorder
mtcars$cyl3 <- with(mtcars, reorder(cyl, cyl, function(x) -length(x)))ggplot(mtcars, aes(cyl3)) + geom_bar()
守着一只汪
TA贡献1872条经验 获得超3个赞
limits
scale_x_discrete
ggplot(mtcars, aes(factor(cyl))) + geom_bar() + scale_x_discrete(limits=c(8,4,6))
墨色风雨
TA贡献1853条经验 获得超6个赞
reorder
:
qplot(reorder(factor(cyl),factor(cyl),length),data=mtcars,geom="bar")
编辑:
qplot(reorder(factor(cyl),factor(cyl),function(x) length(x)*-1), data=mtcars,geom="bar")
- 3 回答
- 0 关注
- 804 浏览
添加回答
举报
0/150
提交
取消