我要做一个柱状图,其中最大的柱状图离y轴最近,最短的柱状图离y轴最远。这有点像我的表格
Name Position
1 James Goalkeeper
2 Frank Goalkeeper
3 Jean Defense
4 Steve Defense
5 John Defense
6 Tim Striker
所以我试图建立一个条形图,根据位置显示球员的数量
p <- ggplot(theTable, aes(x = Position)) + geom_bar(binwidth = 1)
但是图表显示的是门将栏,然后是防守栏,最后是前锋栏。我希望图表的顺序是,防守条最靠近y轴,守门员条,最后是前锋条。
谢谢
除了forcats::fct_infreq之外,由
@HolgerBrandl,有forcats::fct_rev,它颠倒了因子的顺序。
theTable <- data.frame(
Position=
c("Zoalkeeper", "Zoalkeeper", "Defense",
"Defense", "Defense", "Striker"),
Name=c("James", "Frank","Jean",
"Steve","John", "Tim"))
p1 <- ggplot(theTable, aes(x = Position)) + geom_bar()
p2 <- ggplot(theTable, aes(x = fct_infreq(Position))) + geom_bar()
p3 <- ggplot(theTable, aes(x = fct_rev(fct_infreq(Position)))) + geom_bar()
gridExtra::grid.arrange(p1, p2, p3, nrow=3)
排序的关键是按照您想要的顺序设置因子的级别。不需要有序因子;一个有序因子中的额外信息是不必要的,如果这些数据被用于任何统计模型中,可能会导致错误的参数化——多项式对比不适用于这样的名义数据。
## set the levels in order we want
theTable <- within(theTable,
Position <- factor(Position,
levels=names(sort(table(Position),
decreasing=TRUE))))
## plot
ggplot(theTable,aes(x=Position))+geom_bar(binwidth=1)
在最一般的意义上,我们只需要将因子级别设置为所需的顺序。如果不指定,因子的级别将按字母顺序排序。您还可以如上所述在因子调用中指定级别顺序,也可以采用其他方法。
theTable$Position <- factor(theTable$Position, levels = c(...))
我认为已经提供的解决方案过于冗长。使用ggplot进行频率排序barplot的一种更简洁的方法是
ggplot(theTable, aes(x=reorder(Position, -table(Position)[Position]))) + geom_bar()
它类似于Alex Brown的建议,但更简短,并且不需要任何函数定义。
更新
我认为我的旧解决方案在当时是好的,但现在我宁愿使用forcats::fct_infreq,它是按频率排序因子级别:
require(forcats)
ggplot(theTable, aes(fct_infreq(Position))) + geom_bar()