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()
謝謝。
我很難想象如何讓您的結果看起來。你能畫出你期望的樣子嗎,僅僅是第一學期?我很困惑,你有2行的時間= 1,沒有行的時間= 7或8,但你想要一個沒有添加的條形圖...似乎有洞。你可能想要'geom_tile'來代替嗎? – Gregor
你也可以做'position ='identity'',但是那個去9的酒吧會覆蓋下面的所有東西。 – Gregor
是的,我也嘗試過'position =「identity」'然而,當'Mode'改變時我無法想象。時間對應於事件發生的時間,但是「期限」最多持續9分鐘。 – user2716568