Edit Individual Ggplots in Ggally::Ggpairs: How to Have the Density Plot Not Filled in Ggpairs

Edit individual ggplots in GGally::ggpairs: How do I have the density plot not filled in ggpairs?

The answer to the question can be found on https://cran.r-project.org/web/packages/GGally/vignettes/ggpairs.html (archived here)

ggally_mysmooth <- function(data, mapping, ...){
ggplot(data = data, mapping=mapping) +
geom_density(mapping = aes_string(color="cut"), fill=NA)
}
ggpairs(
diamonds.samp[,1:5],
mapping = aes(color = cut),
upper = list(continuous = wrap("density", alpha = 0.5), combo = "box"),
lower = list(continuous = wrap("points", alpha = 0.3), combo = wrap("dot", alpha = 0.4)),
diag = list(continuous = ggally_mysmooth),
title = "Diamonds"
)

Sample Image

Set alpha and remove black outline of density plots in ggpairs

thanks to @Henrik this is the solution using a custom function

my_dens <- function(data, mapping, ...) {
ggplot(data = data, mapping=mapping) +
geom_density(..., alpha = 0.7, color = NA)
}

pm <- ggpairs(tips, mapping = aes(color = sex), columns = c("total_bill", "time", "tip"),
diag = list(continuous = my_dens))
pm

Sample Image

Examples on how to customize ggpairs plots can be found in the vignette. See the "Matrix Sections" and "Plot Matrix Subsetting".

GGally - unexpected behavior with ggpairs(..., diag = list( continuous = 'density'))

So I finally figured this out, by studying a different question here. It turns out that unless axisLabels is set to "show", density plots on the diagonal are suppressed, without warning.

xx <- mtcars[,c(1,3,4,6)]   ## extract mpg, disp, hp, and wt from mtcars
library(GGally)
ggpairs(xx, diag=list(continuous="density"), axisLabels='show')

Produces this, as expected:
Sample Image

Accumulating tailored ggpairs() plot objects into a list object

A possible solution, if I correctly understood your question :

library(GGally)
data = mtcars
data$ID = rownames(mtcars)
data = data[, c(12,1:11)]

# Load tidyverse
library(tidyverse)

# Create a vector list for each plot you want
var_list <- data.frame(var = names(data)[3:5],
color = colorVec)

# Function for sampling orange points
my_color_fn <- function(data, color_nb) {
sample(1:nrow(data), color_nb)
}

# Create a list with a data for each variable with colors
data_list <- apply(var_list, 1,
function(x)
data %>%
select(ID, mpg, as.character(x[["var"]])) %>%
mutate(color = "black") %>%
mutate(color = replace(color, my_color_fn(., x[["color"]]), "orange")))

# Update my_fn function
my_fn <- function(data, mapping, ...){
xChar = as.character(mapping$x)
yChar = as.character(mapping$y)
x = data[, c(xChar)]
y = data[, c(yChar)]
p <- ggplot(data, aes_string(x=x, y=y)) +
geom_point(aes(color = color)) +
scale_color_manual("", values = c("black" = "black",
"orange" = "orange"))
p
}

# Create a function to get ggpairs for each subset
my_fn2 <- function(data)
{
p <- ggpairs(data %>% select(- ID), 1:2,
lower = list(continuous = my_fn),
upper = list(continuous = wrap("cor", size = 4)))
return(p)
}

# Get plot for each list element
ret <- lapply(data_list, function(x) my_fn2(x))

ret[[1]]
ret[[2]]
ret[[3]]

plot_1
plot_2
plot_3



Related Topics



Leave a reply



Submit