masher
masher

Reputation: 4096

Scale density plots in ggpairs based on total datapoints?

I'm plotting correlations in ggpairs and am splitting the data based on a filter.

The density plots are normalising themselves on the number of data points in each filtered group. I would like them to normalise on the total number of data points in the entire data set. Essentially, I would like to be able to have the sum of the individual density plots be equal to the density plot of the entire dataset.

I know this probably breaks the definition of "density plot", but this is a presentation style I'd like to explore.

In plain ggplot, I can do this by adding y=..count.. to the aesthetic, but ggpairs doesn't accept x or y aesthetics.

Some sample code and plots:

set.seed(1234)
group = as.numeric(cut(runif(100),c(0,1/2,1),c(1,2)))
x = rnorm(100,group,1)
x[group == 1] = (x[group == 1])^2
y = (2 * x) + rnorm(100,0,0.1)
data = data.frame(group = as.factor(group), x = x, y = y)

#plot  of everything
data %>% 
  ggplot(aes(x)) + 
  geom_density(color = "black", alpha = 0.7)

Density plot of the whole data set

#the scaling I want
data %>% 
  ggplot(aes(x,y=..count..,  fill=group)) + 
  geom_density(color = "black", alpha = 0.7)

Density plot of dataset scaled to the total count

#the scaling I get
data %>% 
  ggplot(aes(x,  fill=group)) + 
  geom_density(color = "black", alpha = 0.7)

Density plot of dataset scaled to the count of each group

data %>% ggpairs(., columns = 2:3,
             mapping = ggplot2::aes(colour=group), 
             lower = list(continuous = wrap("smooth", alpha = 0.5, size=1.0)),
             diag = list(continuous = wrap("densityDiag", alpha=0.5 ))
)

The actual ggpairs plot

Are there any suggestions that don't involve reformatting the entire dataset?

Upvotes: 0

Views: 839

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76402

I am not sure I understand the question but if the densities of both groups plus the density of the entire data is to be plotted, it can easily be done by

  1. Getting rid of the grouping aesthetics, in this case, fill.
  2. Placing another call to geom_density but this time with inherit.aes = FALSE so that the previous aesthetics are not inherited.

And then plot the densities.

library(tidyverse)

data %>% 
  ggplot(aes(x, y=..count.., fill = group)) + 
  geom_density(color = "black", alpha = 0.7) +
  geom_density(mapping = aes(x, y = ..count..),
               inherit.aes = FALSE)

enter image description here

Upvotes: 0

Related Questions