Search code examples
rggplot2ggpairs

ggpairs formatting for points only


I'm looking to increase the size of the points AND outline them in black while keeping the line weight the same across the remaining plots.

library(ggplot2)
library(GGally)

pp <- ggpairs(pp.sed, columns = c(1,2), aes(color=pond.id, alpha = 0.5)) +
  theme_bw()
print(pp)

Which gives me the following figure: enter image description here Data for reproducibility, and TIA!

> dput(pp.sed)
structure(list(Fe.259.941 = c(905.2628883, 825.7883359, 6846.128702, 
1032.932924, 997.8037721, 588.9599882, 6107.641947, 798.4493611, 
1046.38376, 685.2485692, 6452.273486, 730.8656684, 902.8585447, 
1039.886406, 7408.801001, 2512.089991, 911.2101809, 941.3712067, 
659.1069185, 1070.090445, 1017.666402, 925.3221586, 645.0500668, 
954.0009756, 1022.594904, 803.5865352, 7653.184537, 1082.714082, 
1048.51115, 773.9070604, 6889.060748, 973.0971769, 1002.091143, 
798.9670583, 5089.035978, 2361.713222, 970.8258109, 748.3574529, 
3942.04816, 889.1760124), Mn.257.611 = c(17.24667962, 14.90488024, 
14.39265671, 20.51133433, 19.92596564, 11.76690074, 19.76386229, 
14.29779164, 20.23646264, 13.55374658, 16.8847698, 13.11784439, 
15.91777975, 20.64068844, 16.78681661, 28.61732162, 15.88328987, 
19.59750367, 13.09735943, 21.59458118, 17.680152, 19.87127449, 
12.8082581, 20.12050221, 17.57143193, 18.72196029, 16.21525793, 
22.0518966, 18.39642397, 18.32238508, 16.17696923, 20.69668404, 
17.96018218, 18.71945309, 16.50162126, 30.60719123, 17.69058768, 
14.99048753, 16.28302375, 18.32277507), pond.id = structure(c(6L, 
5L, 2L, 1L, 3L, 5L, 2L, 1L, 3L, 5L, 2L, 1L, 6L, 3L, 2L, 4L, 6L, 
3L, 4L, 4L, 6L, 3L, 4L, 1L, 6L, 3L, 2L, 1L, 6L, 3L, 2L, 1L, 6L, 
3L, 2L, 1L, 6L, 5L, 2L, 1L), .Label = c("LIL", "RHM", "SCS", 
"STN", "STS", "TS"), class = "factor")), class = "data.frame", row.names = c(11L, 
12L, 13L, 15L, 26L, 27L, 28L, 30L, 36L, 37L, 38L, 40L, 101L, 
102L, 103L, 105L, 127L, 128L, 129L, 131L, 142L, 143L, 144L, 146L, 
157L, 158L, 159L, 161L, 172L, 173L, 174L, 176L, 184L, 185L, 186L, 
188L, 199L, 200L, 201L, 203L))

Solution

  • The GGally package already offers a family of wrap_xxx functions which could be used to set parameters to override default behaviour, e.g. using wrap you could override the default size of points using wrap(ggally_points, size = 5).

    To use the wrapped function instead of the default you have to call

    ggpairs(..., lower = list(continuous = wrap(ggally_points, size = 5))).

    Switching the outline is a bit more tricky. Using wrap we could switch the shape of the points to 21 and set the outline color to "black". However, doing so the points are no longer colored. Unfortunately I have found no way to override the mapping. While it is possible to add a global fill aes, a drawback of doing so is that we lose the black outline for the densities.

    One option to fix that is to write a wrapper for ggally_points which adjusts the mapping so that the fill aes is used instead of color.

    library(ggplot2)
    library(GGally)
    
    ggally_points_filled <- function(data, mapping, ...) {
      names(mapping)[grepl("^colour", names(mapping))] <- "fill"
      ggally_points(data, mapping, ..., shape = 21)
    }
    w_ggally_points_filled <- wrap(ggally_points_filled, size = 5, color = "black")
    
    ggpairs(pp.sed, columns = c(1, 2), aes(color = pond.id, alpha = 0.5),
            lower = list(continuous = w_ggally_points_filled)) +
      theme_bw()