【问题标题】:Differentiating Lines in Lattice Using Parallelplot使用 Parallelplot 区分晶格中的线
【发布时间】:2023-03-21 10:29:01
【问题描述】:

这是我的数据:

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

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

【问题讨论】:

    标签: r lattice


    【解决方案1】:

    怎么样

    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 以使线条更粗。)

    【讨论】:

    • 是的,它有效!但是有什么方法可以修改颜色吗?
    【解决方案2】:

    basketball$Team &gt; 7 始终为真。

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

    因此,颜色总是相同的。

    也许使用颜色图更容易,例如rainbow

    【讨论】:

    • 谢谢!关于如何修复循环的任何建议?
    • 为什么需要循环?使用 DaveTurek 的方法或设置任何你想要的颜色(使用rainbow)?
    • 如果我想根据其他变量的中位数进行分类,我在想一个循环可能有用吗?将看看彩虹。有什么好的参考吗?
    • 只需输入`?rainbow`
    • 根据您想要的中位数定义一些新变量并将其用作groups 变量。
    猜你喜欢
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    相关资源
    最近更新 更多