I'm using the crab
data set from MASS
library in R Studio. I want to create a scatterplot matrix of the five quantitative variables and an interaction variable of sp.sex
as the only categorical variable using ggpairs
. I have reordered the factor levels as B.M, B.F, O.M, and O.F.
I would like to use a diverging 4-class RdYlBu color scheme from RColorBrewer
, where blue represents blue species and red represents orange species. Additionally, I would like to have two darker colors from the palette for male crabs and two lighter colors for females.
I have this code so far, but continue to get an error and no matrix output:
crabs$sp.sex <- factor(paste(crabs$sp, crabs$sex, sep="."), levels=c("B.M", "B.F", "O.M", "O.F"))
ggpairs(crabs, columns = 4:9, aes(color = sp.sex),
mapping = aes_string(fill = "sp.sex"),
lower = list(continuous = wrap("density", alpha = 0.5)),
diag = list(continuous = wrap("density", alpha = 0.5)),
upper = list(continuous = wrap("cor", size = 2)),
title = "Scatterplot Matrix of Crab Data") +
scale_color_manual(values = c("blue", "blue4", "orange4", "orange"),
labels = c("B.M", "B.F", "O.M", "O.F")) +
scale_fill_manual(values = brewer.pal(4, "RdYlBu"),
labels = c("B.M", "B.F", "O.M", "O.F")) +
theme_bw()
The error I get is this:
Error in stop_if_params_exist(params) :
'params' is a deprecated argument. Please 'wrap' the function to supply arguments. help("wrap", package = "GGally")
There is no output coming up in the plot window.
I first tried to tinker with each part of your code and noticed this specific code has no issues running, as it removes the list arguments you have for upper
, lower
, and diagonal
. I just added the fill and color arguments to aes_string
because it was otherwise redundant:
ggpairs(crabs,
columns = 1:5,
mapping = aes_string(fill = "sp.sex",
color = "sp.sex"),
title = "Scatterplot Matrix of Crab Data")+
scale_color_manual(values = c("blue", "blue4",
"orange4", "orange"),
labels = c("B.M", "B.F",
"O.M", "O.F")) +
scale_fill_manual(values = brewer.pal(4, "RdYlBu"),
labels = c("B.M", "B.F", "O.M", "O.F")) +
theme_bw()
If you run all of the list arguments I mentioned previously, you get the following error:
Error in `stat_density2d()`:
! Problem while computing stat.
ℹ Error occurred in the 2nd layer.
Caused by error in `compute_layer()`:
! `stat_density2d()` requires the following missing aesthetics:
y
Run `rlang::last_error()` to see where the error occurred.
Warning messages:
1: In min(x, na.rm = na.rm) :
no non-missing arguments to min; returning Inf
2: In max(x, na.rm = na.rm) :
no non-missing arguments to max; returning -Inf
3: In min(x, na.rm = na.rm) :
no non-missing arguments to min; returning Inf
4: In max(x, na.rm = na.rm) :
no non-missing arguments to max; returning -Inf
5: In min(x, na.rm = na.rm) :
no non-missing arguments to min; returning Inf
6: In max(x, na.rm = na.rm) :
no non-missing arguments to max; returning -Inf
It appears there is some odd mapping issue going on here between the wrappers and what you are trying to achieve. One way of getting around this is to port in your own functions as shown in this ggpairs vignette. I show below:
#### Scatter Function ####
my_plot <- function(data,
mapping,
...) {
ggplot(data = data,
mapping = mapping) +
geom_point(...)
}
#### Density Function ####
my_dens <- function(data,
mapping,
...) {
ggplot(data = data,
mapping = mapping) +
geom_density(...)
}
#### Cor Text Function ####
my_text <- function(data,
mapping,
size,
...) {
ggally_cor(data = data,
mapping = mapping,
size = size)
}
Then just insert them into the wrappers of the plot:
ggpairs(crabs,
columns = 1:5,
mapping = aes_string(fill = "sp.sex",
color = "sp.sex"),
title = "Scatterplot Matrix of Crab Data",
lower = list(continuous = wrap(my_plot, alpha = 0.5)),
diag = list(continuous = wrap(my_dens, alpha = 0.5)),
upper = list(continuous = wrap(my_text, size = 2)))+
scale_color_manual(values = c("blue", "blue4",
"orange4", "orange"),
labels = c("B.M", "B.F",
"O.M", "O.F")) +
scale_fill_manual(values = brewer.pal(4, "RdYlBu"),
labels = c("B.M", "B.F", "O.M", "O.F")) +
theme_bw()