I\'m trying to use ggvis to create a NFL strength-of-schedule chart for the 2014 season. The data comes from FootballOutsiders.com, and later I\'ll make a Shiny app that aut
I set the colors for each cell by creating a new variable def.color that maps each value of defense to a specific color. In ggplot2 you can set the colors directly within the call to ggplot using one line of code, such as scale_fill_manual(), rather than adding a color variable to the data frame. I'm hoping there's a way to do that in ggvis, but I haven't found it yet. So, for now, here we go:
# Create a new variable df2$def.color for mapping df2$defense values to colors
# Functions to create color ramps for the blue and orange color ranges
Blue = colorRampPalette(c("darkblue","lightblue"))
Orange = colorRampPalette(c("orange","darkorange3"))
# Negative values of defense get a blue color scale with 10 colors
df2$def.color[!is.na(df2$defense) & df2$defense<0] =
as.character(cut(df2$defense[!is.na(df2$defense) & df2$defense<0],
seq(min(df2$defense - 0.1, na.rm=TRUE), 0, length.out=11),
labels=Blue(10)))
# Positive values of defense get an orange color scale with 10 colors
df2$def.color[!is.na(df2$defense) & df2$defense>=0] =
as.character(cut(df2$defense[!is.na(df2$defense) & df2$defense>=0],
seq(0, max(df2$defense, na.rm=TRUE)+0.1, length.out=11),
labels=Orange(10)))
# Set NA values in df2$def.color to light gray in df2$def.color
df2$def.color[is.na(df2$defense)] = "#E5E5E5"
# Set NA values in df2$defense to blanks so that we won't get "NaN" in cells with
# missing data
df2$defense[is.na(df2$defense)] = ""
Now we create the plot. To get the colors, map def.color to fill using := to override the default colors. To add the values of defense use layer_text. I'm not happy with the text placement within each cell, but this is the best I've been able to come up with for now.
df2 %>%
ggvis(~week, ~team, fill:=~def.color) %>%
layer_rects(width = band(), height = band()) %>%
scale_nominal("x", padding = 0, points = FALSE) %>%
scale_nominal("y", padding = 0, points = FALSE) %>%
layer_text(text:=~defense, stroke:="white", align:="left", baseline:="top")
