0

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

ggpairs() 绘图

4

1 回答 1

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 将不会添加此列。

于 2020-11-13T00:25:44.680 回答