我試圖在變量中對變量進行分組並按降序排序。在ggplot2中訂購多個變量
是myDF
region airport value
MIA FLL 0.244587909
MIA PBI 0.824144687
MIA MIA 0.484907626
NYC EWR 0.731075565
NYC LGA 0.708648915
NYC HPN 0.523991258
LAX LGB 0.651847818
LAX LAX 0.423607479
LAX SNA 0.433837044
LAX ONT 0.723144957
Other MCO 0.657586674
Other SJC 0.084138321
Other OAK 0.698794154
Other BOS 0.85765002
Other BNA 0.018953126
Other WAS 0.234897245
https://i.stack.imgur.com/G1E2k.jpg
我想重現上圖。
這是第一個嘗試:
ggplot(mydf, aes(x=airport,y=value, fill = region)) +
geom_bar(stat = "identity")
這裏是第2次嘗試:
ggplot(mydf, aes(x=reorder(airport,-value,sum),y=value, fill = region)) +
geom_bar(stat = "identity")
我卡在這裏。我可以嵌套重新訂購嗎? reorder(reorder(x, y), y)
我不想讓這是一個手動過程來調出每個分組。
mydf$order <- c('ONT','LGB','SNA','LAX','PBI','MIA','FLL','EWR','LGA','HPN','BOS','OAK','MCO','WAS','SJC','BNA')
ggplot(mydf, aes(x=airport,y=value, fill = region, order = order)) +
geom_bar(stat = "identity")
這仍然無法正常工作。我會很感激任何幫助!
哇!感謝您的幫助! (甜蜜格式的獎勵積分) –