簡體   English   中英

如何在R中的直方圖ggplot2上保持分類x的順序

[英]How keep the order of categorical x on a histogram ggplot2 in R

我想繪制一個直方圖,類別在x上,分數在y上。 表中的順序應保留在繪圖中,但現在繪圖已重新排序,我在SO上找到的關於此的幾篇文章對我的情況沒有幫助。 例如我試過了: 在ggplot2條形圖中訂購條形

require(data.table)
require(ggplot2)

table <- structure(list(a = c(1, 2, 3, 4, 5, 6), b = c("grease", "surf", 
"lift", "zen", "ufo", "nothing"), c = c("3976.65457028497", "3700.27298336394", 
"3691.44157683915", "3687.89781035758", "3685.83200999925", "3685.44486138222"
)), .Names = c("a", "b", "c"), row.names = c(NA, -6L), class = c("data.table", 
"data.frame"))

ggplot(data=table) + geom_histogram(aes(x=b,y=c),stat='identity')

因此,這使它們按字母順序排序,而我希望它們按降序c排序。 我該怎么做?

據我了解,這就是您想要做的(我將對象表重命名為table.dt):

ggplot(data=table.dt,aes(x=reorder(b,-as.numeric(c)),y=c)) +
  geom_histogram(stat='identity')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM