r - 使用 ggplot 的条形图重新排序无法正常工作

这是数据集: https://www.dropbox.com/s/mrlfnh6e2ww1xwd/home.csv?dl=0

这是我的代码:

hom <- read.csv(file.choose(),header=TRUE)
home.melt <- melt(hom, id.vars='home')

ggplot(home.melt, 
aes(x = reorder(home, value), y = value, 
fill=forcats::fct_rev(variable))) + 
geom_bar(stat = "identity",width = 0.8) + coord_flip() +
theme_minimal(base_size=10) +
labs(title="Home time",
   subtitle="By matches",
   x="Home",
   y="time (minutes)",
   fill=" ")

这是输出:

如您所见,它不是按降序排列的。

最佳答案

关键是在重新排序的调用中指定函数:

reorder(home, value, FUN = sum)

默认是“均值”

 ggplot(home.melt, 
               aes(x = reorder(home, value, FUN = sum), y = value, 
                   fill=forcats::fct_rev(variable))) + 
          geom_bar(stat = "identity",width = 0.8) + coord_flip() +
          theme_minimal(base_size=10) +
          labs(title="Home time",
               subtitle="By matches",
               x="Home",
               y="time (minutes)",
               fill=" ")

https://stackoverflow.com/questions/46103521/

相关文章:

python - 无法将字节连接到 str(转换为 Python3)

python - Python 中的 str.join() 和 str().join() 有什么区别

javascript - 如何在 Gatsby.js 中将时间戳附加到 markdown 前面的内容

angularjs - ngrx 参数选择函数

scala - 为什么我不能在 Scala 中增加?

css - 通过添加元素 React.js 的数量来更改 div 元素的高度

php - 为什么 Google_Service_Drive 文件的属性只返回 null?

node.js - 如何在 Node.js 中设置文件编码

apache-spark-sql - pySpark groupby 中的条件聚合

amazon-web-services - aws s3 ls 递归 grep 扩展 '.mov'