我試圖通過'rank'變數以降序顯示躲避的列,因為它們以升序顯示,但我似乎無法弄清楚。
我試圖調整一些已經發布的建議,但無濟于事。當我嘗試 y=factor(rank, levels = rev(levels(rank))) 時,它給了我一個錯誤,并且在 position_dodge2 函式中包含 reverse=TRUE 沒有任何效果。
這是我的代碼:
str1 <- data.frame(
mo = c('jan', 'feb', 'mar', 'apr', 'may', 'jan', 'feb', 'jan', 'apr', 'apr',
'feb', 'may', 'feb', 'may', 'apr', 'jun', 'apr', 'jun', 'apr', 'jun'),
rank = c(9.5, 9.2, 9.1, 9.1, 9.0, 8.8, 8.7, 8.7, 8.6, 8.6, 8.6, 8.6, 8.5,
8.5, 8.5, 8.4, 8.4, 8.4, 8.4, 8.8),
value= c(2226, 139, 18428, 227899, 10000, 558, 1, 700, 10, 1313, 2, 1530, 1,
700, 1530, 168, NA, 50, NA, 3022))
str1 <- with(str1, str1[order(mo, -as.numeric(rank)), ]) # I have tried this from another post to no avail
ggplot(str1, aes(x=reorder(mo, -value), y=rank, fill=value))
geom_col(position=position_dodge2(preserve = "single", reverse=T))
coord_flip()
uj5u.com熱心網友回復:
首先,您需要修復排序:
str1 <- with(str1, str1[order(mo, rank), ])
然后我會mo通過使其成為一個因素來設定訂單的順序
str1$mo <- factor(str1$mo, levels = c("jan", "feb", "mar", "apr", "may", "jun"))
然后你就不需要為了審美而使用這個reorder()功能了。x十個反向引數position_dodge2()應該可以按您的預期作業:
ggplot(str1, aes(x=mo, y=rank, fill=value))
geom_col(position=position_dodge2(preserve = "single", reverse=T))
coord_flip()
這應該給你這個(我希望這是你所追求的):

如果你想顛倒月份的順序,你也可以這樣做scale_x_discrete(limits=rev)(在這種情況下,它的 x 是因為你使用了coord_flip().
ggplot(str1, aes(x=mo, y=rank, fill=value))
geom_col(position=position_dodge2(preserve = "single"))
coord_flip()
scale_x_discrete(limits=rev)

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/533308.html
標籤:rggplot2位置闪避
上一篇:分面換行功能的問題
