首页 文章

R编程 - ggplot和geom_bar - 如何按降序对数据进行排序?

提问于
浏览
0

我是stackoverflow中的新手,也是R中的新手 . 我有一个问题 . 如何使用 descending 命令中的ggplot和geom_bar对数据进行排序?这是我的代码:

ggplot(actions)geom_bar(mapping = aes(x = reorder(Actions,Actions,length)))coord_flip()ylab(“Count”)xlab(“Actions”)

这是输出 . 非常感谢你的帮助 . 谢谢!

enter image description here

1 回答

  • 1

    您需要根据所选轴重新排序数据框 . 以下是汽车数据集的工作示例 .

    df <- mtcars
    
    df2 <- transform(df,
              cyl = reorder(cyl,order(mpg, decreasing = TRUE)))
    
    p <- ggplot(df2) + 
                    geom_bar(aes(x = cyl, y = mpg), stat = 'identity') +
                    coord_flip() + 
                    ylab("mpg") +
                    xlab("cyl")
    
    print(p)
    

    Sorted based on cyl, as that was on my x-axis originally

相关问题