3

I'm trying to plot the iris dataset using the GGally package

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

Why is it complaining about number of column values. Can it not be used on more than 5 columns?

I also want to run k-means and then compare the results to the actual but this also gives a similar error:

> 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)
Anthony
  • 33,838
  • 42
  • 169
  • 278

1 Answers1

2

The error lies in the columns argument which needs to be a vector of indices i.e.:

#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'))

Output:

enter image description here

And it is exactly the same thing for the kmeans plot:

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'))
LyzandeR
  • 37,047
  • 12
  • 77
  • 87
  • Thanks! this works but I'm now having trouble applying this to my dataset. I asked a separate question for it http://stackoverflow.com/questions/29418515/error-in-ggally-error-in-unittic-pos-c-mm-x-and-units-must-have-leng Thanks! – Anthony Apr 02 '15 at 17:05
  • Happy I could be of help :). I replied to your other post as well to give you a solution :) – LyzandeR Apr 02 '15 at 17:31
  • How can I specify the columns , for example among 16 columns I want 5,8,10? – Reza Dec 31 '17 at 00:27
  • `columns=c(5,8,10)` should work. If not try asking a new question. – LyzandeR Dec 31 '17 at 02:18