2017-02-26 23 views
0

我想首先道歉,因爲沒有足夠的努力來提問我的問題。我注意到他們並不清楚,並遵守社區要求幫助你更好地幫助你的規則。針對兩個分類變量創建一個明確的ggplot圖形

我一直有我的代碼的麻煩,我不知道如何製作一個明確的切割圖,因爲我想做它。

問題如下:製作一個條形圖,顯示每支球隊聯賽的勝率。圖必須儘可能清晰和可讀。

這是我的代碼:

library(Lahman) 
    library(ggplot2) 
    library(dplyr) 
    library(reshape2) 
    Teams %>% group_by(teamID) %>% 
    filter(!is.na(LgWin == "Y")) %>% 
    ggplot(Teams, mapping = aes(teamID, ..count..)) + 
    geom_bar(mapping = aes(fill = LgWin)) 

畫面沒有任何意義。請幫忙。

The picture does not make any sense. Please help.

回答

1
在要繪製的行數結束

每個teamId已經LgWin ==「Y」吧?如果是的話,你可以這樣做:

Teams %>% 
    filter(LgWin == "Y") %>% 
    group_by(teamID) %>% 
    summarise(number_of_wins = n()) %>% 
    transform(., teamID = reorder(teamID, number_of_wins)) %>% 
    ggplot(., mapping = aes(x = teamID, number_of_wins)) + 
    geom_bar(stat = "identity", mapping = aes(fill = number_of_wins)) + 
    coord_flip() 

這個結果:

enter image description here

希望這有助於...

+0

美麗。謝謝。 –

相關問題