【问题标题】:'group' in ggpairs(), where did it come from?ggpairs() 中的“组”,它是从哪里来的?
【发布时间】:2021-02-25 00:14:50
【问题描述】:

我在我的数据帧上运行了 ggpairs(),输出中出现了一个“组”变量(下图)。我的数据框有五列,数据框中绝对没有称为“组”的列。有谁知道这个“组”变量是什么以及它来自哪里?

【问题讨论】:

  • 没有可重现的小例子,不清楚。我用示例测试显示here 无法重现该问题

标签: r ggpairs


【解决方案1】:

ggpairs 当你传入一个分组的 tibble 时会发生这种情况:

library(GGally)
library(dplyr)

iris %>% 
  group_by(Species) %>%
  ggpairs()

要摆脱它,只需 ungroup 您的数据框,然后将其传递给 ggpairs

iris %>% 
  group_by(Species) %>%
  ungroup() %>%
  ggpairs()

这样做的原因是,当您将分组的 tibble 传递给 ggplot 时,它会将分组存储在其主数据表中作为名为 .group 的列:

p <- ggplot(iris %>% group_by(Species))
p$data
#> # A tibble: 150 x 6
#> # Groups:   Species [3]
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species .group
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>    <int>
#>  1          5.1         3.5          1.4         0.2 setosa       1
#>  2          4.9         3            1.4         0.2 setosa       1
#>  3          4.7         3.2          1.3         0.2 setosa       1
#>  4          4.6         3.1          1.5         0.2 setosa       1
#>  5          5           3.6          1.4         0.2 setosa       1
#>  6          5.4         3.9          1.7         0.4 setosa       1
#>  7          4.6         3.4          1.4         0.3 setosa       1
#>  8          5           3.4          1.5         0.2 setosa       1
#>  9          4.4         2.9          1.4         0.2 setosa       1
#> 10          4.9         3.1          1.5         0.1 setosa       1
#> # ... with 140 more rows

这是ggpairs 使用的数据,因此出现了.groups 变量。对于GGally 的作者,这可能会被标记为错误。请注意,如果给定一个普通的小标题或数据框,ggplot 将不会添加此列。

【讨论】:

  • @WithRegards 不客气。请see here 获取有关当有人回答您的问题时该怎么做的建议。谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 2018-02-26
  • 2014-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多