利用Parallelplot在格子中区分线

时间:2016-09-02 11:36:39

标签: r lattice

这是我的数据:

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"))

上述数据中有两支球队(克利夫兰和金州勇士队)。我的平行情节现在看起来像这样:

parallelplot

上图的代码是:

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

我试图为这两支队伍添加不同的颜色:

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)

但是,当我访问reading_colors时,这是值(循环不起作用):

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

感谢任何关于为什么这不起作用的反馈,有人可以请我帮助我区分我的界限吗?

2 个答案:

答案 0 :(得分:0)

怎么样

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

每个组都有不同的默认颜色。

编辑以控制颜色 -

使用trellis.par.set,您可以定义组颜色:

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

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

或者您可以直接在parallelplot电话中执行此操作:

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

(注意灰色很浅,所以我添加了lwd=2以使线条变粗。)

答案 1 :(得分:0)

basketball$Team > 7永远是真的。

> basketball$Team > 7
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

因此,颜色总是一样的。

使用色彩图可能更容易,例如rainbow