Rsgf: Example Reports for playerCard() Function

https://bcable.net/x/Rsgf

library(Rsgf)
library(ggplot2)
library(grid)
library(pander)
library(png)
library(rmarkdown)
player_card <- Rsgf::playerCard(player_names, sgf_path)

Output Files

Player Card: PDF

Some Stats

Prep Work

resultParse <- function(df){
    df$Win.Player <- substr(df$Result, 1, 1)
    df$Win.By <- substr(df$Result, 3, length(df$Result))
    df
}
results <- aggregate(
    Game.Number ~ Result + White.Player.Name + Black.Player.Name,
    data=player_card, FUN=function(x){ x[[1]] }
)
results <- resultParse(results)
names(results) <- c(
    "Result", "White.Player.Name", "Black.Player.Name",
    "Game.Number", "Win.Player", "Win.By"
)

results$White.Player.Name <- as.character(results$White.Player.Name)
results$Black.Player.Name <- as.character(results$Black.Player.Name)

results$Win.Player.Name <- results$White.Player.Name
results$Win.Player.Name[results$Win.Player == "B"] <-
    results$Black.Player.Name[results$Win.Player == "B"]
player_card <- resultParse(player_card)
player_card$User.Color <- rep("B", nrow(player_card))
player_card$User.Color[player_card$White.Player.Name %in% player_names] <- "W"

Stats

game_total <- length(results$Game.Number)
game_wins <- sum(results$Win.Player.Name %in% player_names)
game_losses <- sum(!(results$Win.Player.Name %in% player_names))
resigns_total <- sum(results$Win.By == "R")
resigns_wins <- sum(
    results$Win.Player.Name %in% player_names &
    results$Win.By == "R"
)
resigns_losses <- sum(
    !(results$Win.Player.Name %in% player_names) &
    results$Win.By == "R"
)
display_frame <- data.frame(
    Games=c(game_wins, game_losses, game_total),
    Resignations=c(resigns_wins, resigns_losses, resigns_total),
    Percentage=c(
        round(resigns_wins/game_wins, 2),
        round(resigns_losses/game_losses, 2),
        round(resigns_total/game_total, 2)
    ),
    row.names=c("Wins", "Losses", "Total")
)
display_frame
##        Games Resignations Percentage
## Wins      63           16       0.25
## Losses    67           30       0.45
## Total    130           46       0.35

Result Count

scored_results <- results[!(results$Win.By %in% c("R", "T", "F")),]
scored_results$Win.Score <- as.numeric(as.character(scored_results$Win.By))
scored_results$Win.Score[
    !(scored_results$Win.Player.Name %in% player_names)
] <- -scored_results$Win.Score[
    !(scored_results$Win.Player.Name %in% player_names)
]
scored_results$Win.Score <- as.factor(scored_results$Win.Score)
agg_score <- aggregate(Game.Number ~ Win.Score, data=scored_results, FUN=length)
names(agg_score) <- c("Win.Score", "Count")
scored_results$Win.Score <- as.numeric(as.character(scored_results$Win.Score))
g <- ggplot(scored_results, aes(x=Win.Score))
g <- g + geom_freqpoly(binwidth=3, center=0)
g <- g + xlab("") + ylab("")
g <- g + scale_x_continuous(
    breaks=seq(
        round(min(scored_results$Win.Score)/10)*10,
        max(scored_results$Win.Score)+10, by=10
    )
)
g <- g + theme_bw() %+replace% theme(
    line=element_line(
        colour="#000000", size=1, linetype="solid", lineend="round"
    ),
    axis.text.x=element_text(hjust=0.5, vjust=0.5, angle=90, size=20),
    axis.text.y=element_text(size=20)
)
g

plot of chunk score_distribution

ggsave("plot_score_distribution.png", g, width=20, height=4, dpi=96)

All Games Heatmap

agg_heatmap <- aggregate(Game.Number ~ x + y,
    data=player_card[player_card$User.Color == player_card$Color,],
    FUN=length
)
names(agg_heatmap) <- c("x", "y", "Count")
agg_heatmap$Count <- as.factor(agg_heatmap$Count)
g <- Rsgf::boardHeatMap(agg_heatmap)
g

plot of chunk agg_heatmap_render

ggsave("plot_agg_heatmap.png", g, width=20, height=20, dpi=96)

Opening Heatmaps

Handicap vs. Color

agg_heatmap_first10_hcolor <- aggregate(
    Game.Number ~ x + y + Color + Handicap,
    data=player_card[
        player_card$User.Color == player_card$Color &
        player_card$Move.Number < 11,
    ],
    FUN=length
)
names(agg_heatmap_first10_hcolor) <- c("x", "y", "Color", "Handicap", "Count")
handicap_crosstab <- function(){
    grid.newpage()

    # black/white header
    pushViewport(viewport(
        name="blackwhite", x=1, y=1, width=0.98, height=0.02,
        just=c("right", "top"), layout=grid.layout(1, 2)
    ))
    grid.text("Black", gp=gpar(fontface="bold", fontsize=60),
        vp=viewport(layout.pos.row=1, layout.pos.col=1)
    )
    grid.text("White", gp=gpar(fontface="bold", fontsize=60),
        vp=viewport(layout.pos.row=1, layout.pos.col=2)
    )
    popViewport()

    # handicap header
    pushViewport(viewport(
        name="handicap", x=0, y=0, width=0.02, height=0.98,
        just=c("left", "bottom"), layout=grid.layout(3, 1)
    ))
    grid.text("Handicap: 0", rot=90, gp=gpar(fontface="bold", fontsize=60),
        vp=viewport(layout.pos.row=1, layout.pos.col=1)
    )
    grid.text("Handicap: 2", rot=90, gp=gpar(fontface="bold", fontsize=60),
        vp=viewport(layout.pos.row=2, layout.pos.col=1)
    )
    grid.text("Handicap: 3", rot=90, gp=gpar(fontface="bold", fontsize=60),
        vp=viewport(layout.pos.row=3, layout.pos.col=1)
    )
    popViewport()

    # body viewport
    pushViewport(viewport(
        name="body", x=1, y=0, width=0.98, height=0.98,
        just=c("right", "bottom"), layout=grid.layout(3, 2)
    ))

    # loop plots
    for(handicap in c(0, 2, 3)){
        agg_black <- aggregate(
            Game.Number ~ x + y,
            data=player_card[
                player_card$Color == "B" &
                player_card$Handicap == handicap &
                player_card$User.Color == player_card$Color &
                player_card$Move.Number < 11,
            ], FUN=length
        )
        names(agg_black) <- c("x", "y", "Count")

        agg_white <- aggregate(
            Game.Number ~ x + y,
            data=player_card[
                player_card$Color == "W" &
                player_card$Handicap == handicap &
                player_card$User.Color == player_card$Color &
                player_card$Move.Number < 11,
            ], FUN=length
        )
        names(agg_white) <- c("x", "y", "Count")

        g_b <- go_boardmap(agg_black)
        g_w <- go_boardmap(agg_white)

        row <- handicap
        if(row == 0){ row <- 1 }
        plot(g_b, vp=viewport(layout.pos.row=row, layout.pos.col=1))
        plot(g_w, vp=viewport(layout.pos.row=row, layout.pos.col=2))
    }
}
# save to screen
handicap_crosstab()

plot of chunk agg_heatmap_first10_hcolor_render

# open PNG
png("plot_crosstab_handicap.png", width=40, height=60, units="in", res=96)
handicap_crosstab()
dev.off()
## png 
##   2

Winner vs. Color

winloss_crosstab <- function(){
    grid.newpage()

    # black/white header
    pushViewport(viewport(
        name="blackwhite", x=1, y=1, width=0.98, height=0.02,
        just=c("right", "top"), layout=grid.layout(1, 2)
    ))
    grid.text("Black", gp=gpar(fontface="bold", fontsize=60),
        vp=viewport(layout.pos.row=1, layout.pos.col=1)
    )
    grid.text("White", gp=gpar(fontface="bold", fontsize=60),
        vp=viewport(layout.pos.row=1, layout.pos.col=2)
    )
    popViewport()

    pushViewport(viewport(
        name="winloss", x=0, y=0, width=0.02, height=0.98,
        just=c("left", "bottom"), layout=grid.layout(2, 1)
    ))
    grid.text("Win", rot=90, gp=gpar(fontface="bold", fontsize=60),
        vp=viewport(layout.pos.row=1, layout.pos.col=1)
    )
    grid.text("Loss", rot=90, gp=gpar(fontface="bold", fontsize=60),
        vp=viewport(layout.pos.row=2, layout.pos.col=1)
    )
    popViewport()

    pushViewport(viewport(
        name="body", x=1, y=0, width=0.98, height=0.98,
        just=c("right", "bottom"), layout=grid.layout(2, 2)
    ))

    # loop graphs
    for(color in c("B", "W")){
        agg_win <- aggregate(
            Game.Number ~ x + y,
            data=player_card[
                player_card$User.Color == player_card$Win.Player &
                player_card$User.Color == color &
                player_card$Color == color &
                player_card$Move.Number < 11,
            ], FUN=length
        )
        names(agg_win) <- c("x", "y", "Count")

        agg_lose <- aggregate(
            Game.Number ~ x + y,
            data=player_card[
                player_card$User.Color != player_card$Win.Player &
                player_card$User.Color == color &
                player_card$Color == color &
                player_card$Move.Number < 11,
            ], FUN=length
        )
        names(agg_lose) <- c("x", "y", "Count")

        g_w <- go_boardmap(agg_win)
        g_l <- go_boardmap(agg_lose)

        col <- 1
        if(color == "W"){ col <- 2 }
        plot(g_w, vp=viewport(layout.pos.row=1, layout.pos.col=col))
        plot(g_l, vp=viewport(layout.pos.row=2, layout.pos.col=col))
    }
}
# save to screen
winloss_crosstab()

plot of chunk agg_heatmap_first10_wcolor_render

# open PNG
png("plot_crosstab_winloss.png", width=40, height=40, units="in", res=96)
winloss_crosstab()
dev.off()
## png 
##   2