我要做一个柱状图,其中最大的柱状图离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轴,守门员条,最后是前锋条。 谢谢


当前回答

由于我们只关注单个变量(“位置”)的分布,而不是两个变量之间的关系,那么直方图可能是更合适的图形。Ggplot有geom_histogram(),这使得它很容易:

ggplot(theTable, aes(x = Position)) + geom_histogram(stat="count")

使用geom_histogram ():

我认为geom_histogram()有点古怪,因为它对待连续数据和离散数据是不同的。

对于连续数据,可以只使用不带参数的geom_histogram()。 例如,如果我们添加一个数字向量“Score”……

    Name   Position   Score  
1   James  Goalkeeper 10
2   Frank  Goalkeeper 20
3   Jean   Defense    10
4   Steve  Defense    10
5   John   Defense    20
6   Tim    Striker    50

然后在“Score”变量上使用geom_histogram()…

ggplot(theTable, aes(x = Score)) + geom_histogram()

对于像“Position”这样的离散数据,我们必须指定一个由美学计算出来的统计数据,使用stat = "count"来给出条形高度的y值:

 ggplot(theTable, aes(x = Position)) + geom_histogram(stat = "count")

注意:奇怪且令人困惑的是,你也可以使用stat = "count"来表示连续的数据,我认为它提供了一个更美观的图形。

ggplot(theTable, aes(x = Score)) + geom_histogram(stat = "count")

编辑:对DebanjanB的有用建议的扩展回答。

其他回答

排序的关键是按照您想要的顺序设置因子的级别。不需要有序因子;一个有序因子中的额外信息是不必要的,如果这些数据被用于任何统计模型中,可能会导致错误的参数化——多项式对比不适用于这样的名义数据。

## 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(...))

我发现ggplot2没有为此提供“自动”解决方案,这非常令人恼火。这就是为什么我在ggcharts中创建了bar_chart()函数。

ggcharts::bar_chart(theTable, Position)

默认情况下,bar_chart()对条形图进行排序并显示水平图。要更改设置水平= FALSE。此外,bar_chart()删除了柱和轴之间难看的“间隙”。

我同意zach的观点,在dplyr内计数是最好的解决方案。我发现这是最短的版本:

dplyr::count(theTable, Position) %>%
          arrange(-n) %>%
          mutate(Position = factor(Position, Position)) %>%
          ggplot(aes(x=Position, y=n)) + geom_bar(stat="identity")

这也将比事先重新排序因子级别快得多,因为计数是在dplyr中完成的,而不是在ggplot或使用table中完成的。

一个简单的基于dplyr的因子重排序可以解决这个问题:

library(dplyr)

#reorder the table and reset the factor to that ordering
theTable %>%
  group_by(Position) %>%                              # calculate the counts
  summarize(counts = n()) %>%
  arrange(-counts) %>%                                # sort by counts
  mutate(Position = factor(Position, Position)) %>%   # reset factor
  ggplot(aes(x=Position, y=counts)) +                 # plot 
    geom_bar(stat="identity")                         # plot histogram

除了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)