kmace
kmace

Reputation: 2044

achieving pairwise combinations using ggplot2

I'm trying to do all pairwise scatterplots of a single variable using ggplot2. Something similar to the default pairs(), but I'd like to manipulate the faceting and coloring with ggplot2. here is a failing example of my current attempt in ggplot2

iris_melt = melt(iris)
ggplot(iris_melt, aes(value,value)) + geom_point() + facet_wrap(variable~variable)

enter image description here

What I would like is a plot of:

I know ggpairs from GGally would do the trick in this situation, however I'd like to do custom faceting and I don't see why I'd need to 'unmelt' the data instead of keeping it tidy

Upvotes: 2

Views: 1110

Answers (3)

see24
see24

Reputation: 1210

You can supply a custom function to ggpairs which allows you to control all the usual ggplot parameters

df <- read.table("test.txt")

upperfun <- function(data,mapping){
  ggplot(data = data, mapping = mapping)+
    geom_density2d()+
    scale_x_continuous(limits = c(-1.5,1.5))+
    scale_y_continuous(limits = c(-1.5,1.5))
}   

lowerfun <- function(data,mapping){
  ggplot(data = data, mapping = mapping)+
    geom_point()+
    scale_x_continuous(limits = c(-1.5,1.5))+
    scale_y_continuous(limits = c(-1.5,1.5))
}  


ggpairs(df,upper = list(continuous = wrap(upperfun)),
        lower = list(continuous = wrap(lowerfun)))   

See this question for the data and context

Upvotes: 2

kmace
kmace

Reputation: 2044

Another post on stack overflow suggests that you enumerate the data, making an additional column for each comparison:

Faceting plots by combinations of columns in ggplot2

This gets what I want, but its definitely not elegant when doing pairwise combinations between multiple columns.

Upvotes: -1

adaien
adaien

Reputation: 1942

There is already a built-in function

 library(GGally)
 ggpairs(iris)

Upvotes: 2

Related Questions