Graphics Programs Reference
In-Depth Information
As is, the column names match the CSV file's header. That's what you
want. But you also want to name the rows by player name instead of row
number, so shift the first column over to row names.
row.names(bball) <- bball$Name
bball <- bball[,2:20]
The first line changes row names to the first column in the data frame.
The second line selects columns 2 through 20 and sets the subset of data
back to bball .
A lot of visual-
ization involves
gathering and
preparing data.
Rarely, do you get
data exactly how
you need it, so you
should expect to
do some data
munging before
the visuals.
The data also has to be in matrix format rather than a data frame. You'd
get an error if you tried to use a data frame with the heatmap() function.
Generally speaking, a data frame is like a collection of vectors where each
column represents a different metric. each column can have different for-
mats such as numeric or a string. A matrix on the other hand is typically
used to represent a two-dimensional space and the data type has to be
uniform across all cells.
bball_matrix <- data.matrix(bball)
The data is ordered how you want it and formatted how you need it to be,
so you can plug it into heatmap() to reap the rewards. By setting the scale
argument to “column,” you tell R to use the minimum and maximum of
each column to determine color gradients instead of the minimum and
maximum of the entire matrix.
bball_heatmap <- heatmap(bball_matrix, Rowv=NA,
Colv=NA, col = cm.colors(256), scale=”column”, margins=c(5,10))
Your result should resemble Figure 7-3. Using cm.colors() , you specified a
color range from cyan to magenta. The function creates a vector of hexa-
decimal colors with a cyan-to-magenta range by default with n shades in
between (in this case, 256). So notice the third column, which is for points
per game, starts at magenta, indicating the highest values for Dwyane
Wade and Lebron James, and then shifts toward a darker cyan hue to the
bottom for Allen Iverson and Nate Robinson. You can also quickly find
other magenta spots representing leading rebounder Dwight Howard or
assist leader Chris Paul.
Search WWH ::




Custom Search