3

我正在尝试使用GGally包绘制虹膜数据集

> library(GGally)
> ggpairs(iris, columns=c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"), colour='Species', lower=list(continuous='points'), axisLabels='none', upper=list(continuous='blank'))
Error in ggpairs(iris, columns = c("Sepal.Length", "Sepal.Width", "Petal.Length",  : 
  Make sure your 'columns' values are less than 5.
    columns = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)

为什么它抱怨列值的数量。它不能用于超过 5 列吗?

我还想运行 k-means,然后将结果与实际结果进行比较,但这也会产生类似的错误:

> set.seed(1234)
> iris$Cluster <- factor(kmeans(iris[,c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")], centers=length(levels(iris$Species)))$cluster)
> ggpairs(iris, columns=c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"), colour='Cluster', lower=list(continuous='points'), axisLabels='none', upper=list(continuous='blank'))
Error in ggpairs(iris, columns = c("Sepal.Length", "Sepal.Width", "Petal.Length",  : 
  Make sure your 'columns' values are less than 6.
    columns = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
4

1 回答 1

2

错误在于列参数需要是索引向量,即:

#notice the columns argument is a vector of indices
library(GGally)
ggpairs(iris, columns=1:4,
        colour='Species', lower=list(continuous='points'),
        axisLabels='none',
        upper=list(continuous='blank'))

输出:

在此处输入图像描述

kmeans情节完全一样:

set.seed(1234)
iris$Cluster <- factor(kmeans(iris[,c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")], centers=length(levels(iris$Species)))$cluster)
ggpairs(iris, columns=1:4, colour='Cluster', lower=list(continuous='points'), axisLabels='none', upper=list(continuous='blank'))
于 2015-04-02T15:51:09.403 回答