2017-04-03 23 views
0

在下面的df,我要重新排序,從最高到最低酒吧在每個方面GGPLOT2:重新排序,從最高到最低杆在每個方面

我試圖

df <- df %>% tidyr::gather("var", "value", 2:4) 
ggplot(df, aes (x = reorder(id, -value), y = value, fill = id))+ 
    geom_bar(stat="identity")+facet_wrap(~var, ncol =3) 

它給了我 enter image description here

它並沒有排列在每個方面從最高到最低的酒吧。

我想出了另一種獲得我想要的方式。我曾在一次繪製每個變量,然後使用grid.arrange()

#I got this function from @eipi10's answer 
#http://stackoverflow.com/questions/38637261/perfectly-align-several-plots/38640937#38640937 
#Function to extract legend 
# https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs 
g_legend<-function(a.gplot) { 
    tmp <- ggplot_gtable(ggplot_build(a.gplot)) 
    leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") 
    legend <- tmp$grobs[[leg]] 
    return(legend) 
} 

p1 <- ggplot(df[df$var== "A", ], aes (x = reorder(id, -value), y = value, fill = id))+ 
    geom_bar(stat="identity") + facet_wrap(~var, ncol =3) 

fin_legend <- g_legend(p1) 
p1 <- p1 + guides(fill= F) 

p2 <- ggplot(df[df$var== "B", ], aes (x = reorder(id, -value), y = value, fill = id))+ 
    geom_bar(stat="identity") + facet_wrap(~var, ncol =3)+guides(fill=FALSE) 

p3 <- ggplot(df[df$var== "C", ], aes (x = reorder(id, -value), y = value, fill = id))+ 
    geom_bar(stat="identity") + facet_wrap(~var, ncol =3)+guides(fill=FALSE) 


grid.arrange(p1, p2, p3, fin_legend, ncol =4, widths = c(1.5, 1.5, 1.5, 0.5)) 

結果將所有情節是什麼,我想 enter image description here

我不知道是否有一個簡單的方法,可以幫助我訂購吧從所有方面從最高到最低,而不必將每個變量分開繪製,然後將它們合併。任何建議將不勝感激。

DATA

df <- read.table(text = c(" 
id A B C 
site1 10 15 20 
site2 20 10 30 
site3 30 20 25 
site4 40 35 40 
site5 50 30 35"), header = T) 

回答

2

下面的方法使用的x軸與facet_wrap()專門準備的變量,但使用labels參數scale_x_discrete()以顯示正確的x軸標籤:

準備數據

我更流暢在data.table,所以這裏使用這裏。隨意使用你喜歡的數據處理軟件包。

編輯:刪除第二虛擬變量,僅需要ord

library(data.table) 
# reshape from wide to long 
molten <- melt(setDT(df), id.vars = "id") 
# create dummy var which reflects order when sorted alphabetically 
molten[, ord := sprintf("%02i", frank(molten, variable, -value, ties.method = "first"))] 

molten 
#  id variable value ord 
# 1: site1  A 10 05 
# 2: site2  A 20 04 
# 3: site3  A 30 03 
# 4: site4  A 40 02 
# 5: site5  A 50 01 
# 6: site1  B 15 09 
# 7: site2  B 10 10 
# 8: site3  B 20 08 
# 9: site4  B 35 06 
#10: site5  B 30 07 
#11: site1  C 20 15 
#12: site2  C 30 13 
#13: site3  C 25 14 
#14: site4  C 40 11 
#15: site5  C 35 12 

創建情節

library(ggplot2) 
# `ord` is plotted on x-axis instead of `id` 
ggplot(molten, aes(x = ord, y = value, fill = id)) + 
    # geom_col() is replacement for geom_bar(stat = "identity") 
    geom_col() + 
    # independent x-axis scale in each facet, 
    # drop absent factor levels (not the case here) 
    facet_wrap(~ variable, scales = "free_x", drop = TRUE) + 
    # use named character vector to replace x-axis labels 
    scale_x_discrete(labels = molten[, setNames(as.character(id), ord)]) + 
    # replace x-axis title 
    xlab("id") 

enter image description here

數據
df <- read.table(text = " 
id A B C 
site1 10 15 20 
site2 20 10 30 
site3 30 20 25 
site4 40 35 40 
site5 50 30 35", header = T) 
+0

感謝您的時間和幫助。 – aelwan

+0

我真的很感謝你對代碼每一行的詳細解釋。非常感謝Uwe。 – aelwan

1

如果你願意失去X軸標籤,您可以用實際的y值做到這一點作爲x審美,然後在每個小滴未使用的因子水平:

ggplot(df, aes (x = factor(-value), y = value, fill = id))+ 
    geom_bar(stat="identity", na.rm = TRUE)+ 
    facet_wrap(~var, ncol =3, scales = "free_x", drop = TRUE) + 
    theme(
     axis.text.x = element_blank(), 
     axis.ticks.x = element_blank() 
    ) 

結果:

enter image description here

x軸標籤的丟失在這裏可能不會太糟糕,因爲您仍然可以繼續使用顏色(並且由於它在各個面之間並不一致,所以x軸仍然令人困惑)。

+0

感謝您的時間和幫助 – aelwan

相關問題