With the code below, I have created a scatterplot matrix. The below code just creates a correlation matrix for all of the data, regardless of treatment. However, a column in my data is "Si". I would like to make two different matrices (one for each treatment type) by dividing the box into two for better comparison, just like I did with lower function (Si levels, 0mM, 4mM).
library(GGally)
leg <- grab_legend(ggplot(data=data1, aes(x=NA, y=NA, colour=Si)) +
geom_line(size=2))
my_fn <- function(data1, mapping, method="p", use="pairwise", ...){
# grab data
x <- eval_data_col(data1, mapping$x)
y <- eval_data_col(data1, mapping$y)
# calculate correlation
corr <- cor(x, y, method=method, use=use)
# calculate colour based on correlation value
# Here I have set a correlation of minus one to blue,
# zero to white, and one to red
# Change this to suit: possibly extend to add as an argument of `my_fn`
colFn <- colorRampPalette(c("blue", "white", "red"), interpolate ='spline')
fill <- colFn(100)[findInterval(corr, seq(-1, 1, length=100))]
ggally_cor(data=data1, size=5, digits=2, stars=TRUE, mapping=mapping, ...) +
theme_void() +
theme(panel.background=element_rect(fill=fill))
}
lowerFn <- function(data1, mapping, emap=NULL, method = "lm", ...) {
# mapping <- c(mapping, emap)
# class(mapping) = "uneval" # need this to combine the two aes
# Can use this instead
mapping <- ggplot2:::new_aes( c(mapping, emap))
p <- ggplot(data = data1, mapping = mapping) +
geom_point(data = data1, alpha = 0.8, size = 3, shape = 16) +
geom_smooth(method = method, ...) +
theme_gray() # to get the white background and prominent axis
p
}
ggpairs(
data1, columns=4:6, legend=leg,
upper = list(continuous=my_fn),
lower = list(continuous =
wrap(lowerFn,
method = "lm", # To make lm bold, use size = 1.3
emap=aes(color=Si),
fullrange=TRUE,
se=FALSE))) +
theme(legend.position='top')
here is the data link; https://docs.google.com/spreadsheets/d/1O5haLrVNsLx4_Sn-mr7lUaON4MnwLegpeg2OieODt8I/edit?usp=sharing