gpt4 book ai didi

r - ggplot2:对图进行排序

转载 作者:行者123 更新时间:2023-12-03 05:50:54 28 4
gpt4 key购买 nike

我有一个 data.frame,它是从最高到最低排序的。例如:

x <- structure(list(variable = structure(c(10L, 6L, 3L, 4L, 2L, 8L, 
9L, 5L, 1L, 7L), .Label = c("a", "b", "c", "d", "e", "f", "g",
"h", "i", "j"), class = c("ordered", "factor")), value = c(0.990683229813665,
0.975155279503106, 0.928571428571429, 0.807453416149068, 0.717391304347826,
0.388198757763975, 0.357142857142857, 0.201863354037267, 0.173913043478261,
0.0496894409937888)), .Names = c("variable", "value"), row.names = c(10L,
6L, 3L, 4L, 2L, 8L, 9L, 5L, 1L, 7L), class = "data.frame")

ggplot(x, aes(x=variable,y=value)) + geom_bar(stat="identity") +
scale_y_continuous("",label=scales::percent) + coord_flip()

现在,数据很好并且已排序,但是当我绘图时,它是按因子排序的。这很烦人,我该如何解决它?

最佳答案

这似乎就是您正在寻找的:

g <- ggplot(x, aes(reorder(variable, value), value))
g + geom_bar() + scale_y_continuous(formatter="percent") + coord_flip()

reorder() 函数将根据变量 对 x 轴项目重新排序。

关于r - ggplot2:对图进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3744178/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com