5

I want to create something like this

library(lattice)
splom(~iris[1:4], groups = Species, data = iris,
      panel=function(x,y,...) {
        panel.abline(0,1)
        panel.superpose(x,y,...)
      })

library(ggplot2)
library(gridExtra)
library(GGally)
ggpairs(iris,columns=1:4,colour="Species") +
  geom_abline(intercept=0,slope=1)

With the geom_abline element I get an error ("non-numeric argument to binary operator").

How can I make it work?

tonytonov
  • 25,060
  • 16
  • 82
  • 98
hatmatrix
  • 42,883
  • 45
  • 137
  • 231

2 Answers2

3

You can customize ggpairs plots by using a custom function. This is the data from this question where the issue was scales but you can do something similar for your question

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)))      # Note: lower = continuous
see24
  • 1,097
  • 10
  • 21
2

You can add the lines to each subplot.

library(ggplot2)
library(gridExtra)
library(GGally)
m=ggpairs(iris,columns=1:4,colour="Species") + geom_abline(intercept=0,slope=1)
for (i in 2:m$nrow) {
  for (j in 1:(i-1)) {
    m[i,j] = m[i,j] + geom_abline(intercept=0,slope=1)
  }
}
Marcus Ritt
  • 477
  • 5
  • 12