Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8246383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:29:27+00:00 2026-06-07T22:29:27+00:00

I am trying to create a pairs plot of 6 data variables using ggplot2

  • 0

I am trying to create a pairs plot of 6 data variables using ggplot2 and colour the points according to the k-means cluster they belong to. I read the documentation of the highly impressive ‘GGally’ package as well as an informal fix by Adam Laiacano [http://adamlaiacano.tumblr.com/post/13501402316/colored-plotmatrix-in-ggplot2]. Unfortunately, I could not find any way to get the desired output in either.

Here is a sample code:-

#The Swiss fertility dataset has been used here

data_ <- read.csv("/home/tejaskale/Ubuntu\ One/IUCAA/Datasets/swiss.csv", header=TRUE)
data_ <- na.omit(data_)

u <- c(2, 3, 4, 5, 6, 7)
x <- data_[,u]
k <- 3
maxIterations <- 100
noOfStarts <- 100
filename <- 'swiss.csv'

library(ggplot2)
library(gridExtra)
library(GGally)

kmeansOutput <- kmeans(x, k, maxIterations, noOfStarts)

xNew <- cbind(x[,1:6], as.factor(kmeansOutput$cluster))
names(xNew)[7] <- 'cluster'
kmeansPlot <- ggpairs(xNew[,1:6], color=xNew$cluster)

OR

kmeansPlot <- plotmatrix(xNew[,1:6], mapping=aes(colour=xNew$cluster))

Both plots are created but aren’t coloured according to clusters.

Hope I haven’t missed an answer to this question on the forum and apologize if that is indeed the case. Any help would be highly appreciated.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:29:29+00:00Added an answer on June 7, 2026 at 10:29 pm

    The following slight modification of plotmatrix2 works fine for me:

    plotmatrix2 <- function (data, mapping = aes())
    {
        grid <- expand.grid(x = 1:ncol(data), y = 1:ncol(data))
        grid <- subset(grid, x != y)
        all <- do.call("rbind", lapply(1:nrow(grid), function(i) {
            xcol <- grid[i, "x"]
            ycol <- grid[i, "y"]
            data.frame(xvar = names(data)[ycol], yvar = names(data)[xcol], 
                x = data[, xcol], y = data[, ycol], data)
        }))
        all$xvar <- factor(all$xvar, levels = names(data))
        all$yvar <- factor(all$yvar, levels = names(data))
        densities <- do.call("rbind", lapply(1:ncol(data), function(i) {
            data.frame(xvar = names(data)[i], yvar = names(data)[i], 
                x = data[, i])
        }))
        densities$xvar <- factor(densities$xvar, levels = names(data))
        densities$yvar <- factor(densities$yvar, levels = names(data))
        mapping <- defaults(mapping, aes_string(x = "x", y = "y"))
        class(mapping) <- "uneval"
        ggplot(all) + facet_grid(xvar ~ yvar, scales = "free") + 
            geom_point(mapping, na.rm = TRUE) + stat_density(aes(x = x, 
            y = ..scaled.. * diff(range(x)) + min(x)), data = densities, 
            position = "identity", colour = "grey20", geom = "line")
    }
    
    
    plotmatrix2(mtcars[,1:3],aes(colour = factor(cyl)))
    

    enter image description here

    It may be a ggplot2 version issue, but I had to force the faceting variables in the densities data frame to be factors (that seems broken to me even in the GGally version). Also, generally don’t pass vectors to aes(), but simply column names.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
Using extension syntax I'm trying to create a left-join using LINQ on two lists
I'm trying to create a priorityqueue which keeps its elements(Pairs of integers) in the
I am trying to create a data structure called Disjoint Set. Looking at theory
I'm trying to create a form using Zend_Form (Zend_Framework component). The users should be
I am trying to create a key/value database with 300,000,000 key/value pairs of 8
Using the jQuery SelectBox plugin I'm trying to create a JSON object which looks
Hi I am trying to create a drop down menu using GWT. This is
I am trying create a delegate representation of constructor by emitting a Dynamic Method,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.