how to define facet axis limits in ggpairs function

1.5k views Asked by At

Having a ggpairs function, how would one limit range of lower facets to e.g. 0.5 for x and y?

library(GGally)

xy <- data.frame(matrix(runif(4 * 1000), ncol = 4))

ggpairs(xy)

enter image description here

1

There are 1 answers

0
Roman Luštrik On BEST ANSWER

You need to define a function which plots (one facet). You can go wild with ggplot in here. See this similar question.

limitRange <- function(data, mapping, ...) { 
  ggplot(data = data, mapping = mapping, ...) + 
    geom_point(...) + 
    geom_smooth(method = "lm", se = FALSE) +
    scale_y_continuous(limits = c(0, 0.5)) +
    scale_x_continuous(limits = c(0, 0.5)) 
}

# This is how you specify which part of the image will be
# plotted using your function.
ggpairs(xy, lower = list(continuous = limitRange))

enter image description here