2017-02-21 63 views
3

我試圖分析足球數據,其中特定的傳球和進球是通過3場比賽或術語進行追蹤。也跟蹤一個團隊所採用的防禦結構或模式。我的數據集的下面是一個例子:如何繪製水平條形圖中的各個組件?

# Example data 
Time <- c(1, 1, 2, 3, 4, 5, 6, 9, 1, 2, 3, 5, 5, 7, 9, 1, 2, 4, 7, 9) 
Event <- c("Pass", "Pass", "Pass", "Goal", "Pass", "Pass", "Goal", "Pass", "Pass", 
      "Pass", "Pass", "Pass", "Pass", "Pass", "Pass", "Goal", "Pass", "Pass", "Pass", "Goal") 
Term <- c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3) 
Symbol <- c("P", "P", "P", "G", "P", "P", "G", "P", "P", 
      "P", "P", "P", "P", "P", "P", "G", "P", "P", "P", "G") 
By <- c("Home", "Away", "Home", "Home", "Home", "Away", "Away", "Away", "Home", 
      "Home", "Home", "Away", "Away", "Away", "Home", "Home", "Home", "Away", "Away", "Away") 
Mode <- c("Press", "Press", "Press", "Forward", "Forward", "Forward", "Forward", "Press", "Press", 
      "Press", "Press", "Press", "Press", "Forward", "Forward", "Forward", "Forward", "Press", "Press", "Forward") 
# Make data.frame 
GameData <- data.frame(Time, Event, Term, Symbol, By, Mode) 
# Make factors 
GameData$Event <- as.factor(GameData$Event) 
GameData$Symbol <- as.factor(GameData$Symbol) 
GameData$Mode <- as.factor(GameData$Mode) 
GameData$Term <- as.factor(GameData$Term) 
GameData$By <- as.factor(GameData$By) 

我想,當這些通行證和目標隨時間流逝進行可視化,根據在模式改變爲。但是,當我將此作爲一個水平條形圖在ggplot2中繪製時,時間反而是總結而不是在適當的時候改變顏色。例如,每個學期的最長時間是9分鐘,但x軸上升到30?我對劇情代碼如下:

# Load package 
require(ggplot2) 
# Plot 
ggplot(GameData, aes(x = Term, y = Time, fill = Mode)) + 
    geom_bar(stat = "identity") + 
    geom_text(data = GameData, aes(label = Symbol, colour = By), size = 9) + 
    scale_color_manual(values =c("black", "red")) + 
    coord_flip() + 
    scale_x_discrete(limits = rev(levels(GameData$Term))) 

我覺得這是一個愚蠢的錯誤,但我要去哪裏錯了,所以在相應時間的顏色/模式的變化?

總之,我想要下面的陰謀的背景每個Mode有不同的顏色Time每個Term

# Plot 
ggplot(GameData, aes(x = Term, y = Time, fill = Mode)) + 
    geom_text(data = GameData, aes(label = Symbol, colour = By), size = 9) + 
    scale_color_manual(values =c("black", "red")) + 
    coord_flip() + 
    scale_x_discrete(limits = rev(levels(GameData$Term))) + 
    theme_classic() 

謝謝。

+0

我很難想象如何讓您的結果看起來。你能畫出你期望的樣子嗎,僅僅是第一學期?我很困惑,你有2行的時間= 1,沒有行的時間= 7或8,但你想要一個沒有添加的條形圖...似乎有洞。你可能想要'geom_tile'來代替嗎? – Gregor

+0

你也可以做'position ='identity'',但是那個去9的酒吧會覆蓋下面的所有東西。 – Gregor

+0

是的,我也嘗試過'position =「identity」'然而,當'Mode'改變時我無法想象。時間對應於事件發生的時間,但是「期限」最多持續9分鐘。 – user2716568

回答

1

以下是使用geom_tile()繪製彩條的部分解決方案(感謝@Gregor的想法)。

# Work around to make sure Time=8 appears on x-axis. 
GameData$discrete_time = factor(GameData$Time, levels=paste(1:9)) 

plot1 = ggplot(GameData, aes(y=Term, x=discrete_time)) + 
     geom_tile(aes(fill=Mode)) + 
     geom_text(aes(label=Symbol, colour=By), size=9) + 
     scale_color_manual(values=c("black", "white")) + 
     scale_fill_brewer(palette="Set1") + 
     scale_x_discrete(drop=FALSE) 

ggsave("plot.png", plot=plot1, width=9, height=3, dpi=150) 

enter image description here


評論:

  • 你可以通過添加適當的行數據(組符號來NA到彩色的瓷磚,但留在空的瓷磚填補通過/目標空白)。
  • 如果有兩個完全重疊的「通過」,有些位置(例如,Term = 1,Time = 1)。我嘗試過各種躲避,抖動,分組策略,但沒有什麼看起來不錯,並揭示了重疊的事件。如果你擴大到更大的數據集,解決這個問題將變得更加重要。