ONLY DO WHAT ONLY YOU CAN DO

こけたら立ちなはれ 立ったら歩きなはれ

R で ヒストグラム ~少年サッカー データ分析~

グラフを並べられる facet_wrap というのを使ってみた

setwd("e:/data")
d <- read.table("stats_all.txt", header=T)
# ggplot2 パッケージを使用
library(ggplot2)

g <- ggplot(d)
g <- g + geom_histogram(
             aes(
                 x=d$"自チーム.10分あたりのTOUCH数",
                 colour=d$"勝敗", 
                 fill=d$"勝敗"
             ), 
             binwidth=5, 
             alpha=0.3,
         )
g <- g + xlim(40, 180)
g <- g + facet_wrap(~d$"勝敗",ncol=2)
g <- g + labs(title="ボールタッチ数")
g <- g + xlab("10分あたりのボールタッチ回数")
g <- g + ylab("試合数")
g <- g + labs(colour="凡例", fill="凡例")
g <- g + theme_bw()
g <- g + scale_color_manual(values=c("#94d82d", "#f06595", "#5c7cfa"))
g <- g + scale_fill_manual( values=c("#94d82d", "#f06595", "#5c7cfa"))
print(g)

f:id:fornext1119:20180424123943p:plain
全然ダメ。"勝"の色は"勝"のグラフにしか表示されないはずじゃないの?
手間はかかるけど、愚直にやってみた。

d_w <- subset(d, 勝敗=="勝")
d_l <- subset(d, 勝敗=="負")
d_d <- subset(d, 勝敗=="引き分け")

g <- ggplot(d_w)
g <- g + geom_histogram(
             aes(
                 x=d_w$"自チーム.10分あたりのTOUCH数",
                 colour="#f06595", 
                 fill="#f06595"
             ), 
             binwidth=5, 
             alpha=0.3
         )
g <- g + xlim(40, 180)
g <- g + labs(title="勝った試合のボールタッチ数")
g <- g + xlab("10分あたりのボールタッチ回数")
g <- g + ylab("試合数")
g <- g + scale_color_manual(values=c("#f06595"))
g <- g + scale_fill_manual( values=c("#f06595"))
g <- g + theme_bw()
g <- g + theme(legend.position="none")
print(g)
g_w <- g

g <- ggplot(d_d)
g <- g + geom_histogram(
             aes(
                 x=d_d$"自チーム.10分あたりのTOUCH数",
                 colour="#94d82d", 
                 fill="#94d82d"
             ), 
             binwidth=5, 
             alpha=0.3
         )
g <- g + xlim(40, 180)
g <- g + labs(title="引き分けた試合のボールタッチ数")
g <- g + xlab("10分あたりのボールタッチ回数")
g <- g + ylab("試合数")
g <- g + scale_color_manual(values=c("#94d82d"))
g <- g + scale_fill_manual( values=c("#94d82d"))
g <- g + theme_bw()
g <- g + theme(legend.position="none")
print(g)
g_d <- g

g <- ggplot(d_l)
g <- g + geom_histogram(
             aes(
                 x=d_l$"自チーム.10分あたりのTOUCH数",
                 colour="#5c7cfa", 
                 fill="#5c7cfa"
             ), 
             binwidth=5, 
             alpha=0.3
         )
g <- g + xlim(40, 180)
g <- g + labs(title="負けた試合のボールタッチ数")
g <- g + xlab("10分あたりのボールタッチ回数")
g <- g + ylab("試合数")
g <- g + scale_color_manual(values=c("#5c7cfa"))
g <- g + scale_fill_manual( values=c("#5c7cfa"))
g <- g + theme_bw()
g <- g + theme(legend.position="none")
print(g)
g_l <- g

library("gridExtra")
grid.arrange(g_w, g_d, g_l, ncol=2)

f:id:fornext1119:20180424124230p:plain
なんで、このように区分け・描画されないんだろう