umm umm - 3 months ago 10
R Question

Differentiating Lines in Lattice Using Parallelplot

here is my data:

basketball <- structure(list(Team = c("Golden State Warriors", "Golden State Warriors",
"Golden State Warriors", "Golden State Warriors", "Golden State Warriors",
"Golden State Warriors", "Golden State Warriors", "Cleveland Cavaliers",
"Cleveland Cavaliers", "Cleveland Cavaliers", "Cleveland Cavaliers",
"Cleveland Cavaliers", "Cleveland Cavaliers", "Cleveland Cavaliers"
), Players = c("Stephen Curry", "Kevin Durant", "Draymond Green",
"Klay Thompson", "Zaza Pachulia", "Andre Iguodala", "David West",
"Lebron James", "Kyrie Irving", "Kevin Love", "Richard Jefferson",
"Chris Andersen", "J.r. Smith", "Iman Shumpert"), `3 points` = c(286,
64, 111, 239, 0, 74, 4, 120, 157, 144, 66, 4, 163, 67), `3 points Attempts` = c(646,
159, 329, 545, 3, 212, 20, 339, 378, 392, 155, 13, 426, 196),
`2 points` = c(367, 174, 228, 363, 240, 157, 319, 504, 421,
269, 78, 116, 152, 126), `2 points Attempt` = c(695, 308,
436, 754, 526, 284, 666, 940, 857, 560, 169, 194, 329, 275
), `Free Throws` = c(308, 146, 132, 225, 126, 68, 119, 375,
315, 258, 78, 76, 54, 43), `Free Throws Attempts` = c(337,
171, 200, 256, 160, 114, 161, 528, 365, 321, 114, 114, 72,
64), `Total Rebounds` = c(341, 178, 647, 247, 500, 257, 449,
416, 237, 731, 183, 299, 218, 224), Steals = c(163, 24, 123,
87, 80, 89, 48, 109, 114, 51, 32, 26, 82, 81), `Blocks ` = c(16,
25, 99, 60, 21, 25, 48, 49, 20, 39, 11, 61, 22, 16), `Personal Fouls` = c(158,
40, 253, 122, 170, 100, 160, 135, 146, 140, 115, 88, 163,
138), `Total points` = c(1900, 686, 921, 1668, 606, 604,
769, 1743, 1628, 1228, 432, 320, 847, 496)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -14L), .Names = c("Team",
"Players", "3 points", "3 points Attempts", "2 points", "2 points Attempt",
"Free Throws", "Free Throws Attempts", "Total Rebounds", "Steals",
"Blocks ", "Personal Fouls", "Total points"))


There are two teams in the data above (Cleveland and Golden State Warriors). My parallel plot right now looks like this:

parallelplot

The code for the graph above is:

library(lattice)
parallelplot(basketball, horizontal.axis = F, col = '#000000')


I've tried to put different colors for the two teams with:

library(lattice)
reading_colors <- c()
for (i in 1:length(basketball$Team)){
if (basketball$Team[i] > 7) {
col <- "#000000"
} else {
col <- "#cccccc"
}
reading_colors <- c(reading_colors,col)
}
parallelplot(basketball, horizontal.axis = F, col = reading_colors)


However, when I access reading_colors, this is the values (the loop is not working):

c("#000000", "#000000", "#000000", "#000000", "#000000", "#000000",
"#000000", "#000000", "#000000", "#000000", "#000000", "#000000",
"#000000", "#000000")


Would appreciate any feedback on why this is not working and can someone please help me differentiate my lines?

Answer

How about

parallelplot(basketball, horizontal.axis = F, groups=basketball$Team)

Each group will have a different default color.

EDIT to control colors -

With trellis.par.set you can define the group colors:

trellis.par.set(list(superpose.line=list(col=c("#000000","#cccccc"))))

parallelplot(basketball, horizontal.axis = F,groups=basketball$Team,lwd=2)

Or you can do it directly in the parallelplot call:

parallelplot(basketball, horizontal.axis = F,groups=basketball$Team,lwd=2,
  par.settings=list(superpose.line=list(col=c("#000000","#cccccc"))))

(Note the gray color is pretty light so I addedlwd=2 to make the lines thicker.)