Search code examples
rggplot2logistic-regression

Add inverted density plot in a logistic regression - ggplot2


I have the following case:

### GENERATING DATA

set.seed(123)

x1 = rnorm(1000)            
x2 = rnorm(1000)
z = 1 + 2*x1 + 3*x2        
pr = 1/(1+exp(-z))         
y = pr > 0.5               
y1 = ifelse(y == "TRUE", 1, 0)

ex = data.frame(y = y, y1 = y1, x1 = x1, x2 = x2)


### PLOTING THE BINOMIAL REGRESSION

library(ggplot2)

ggplot(ex, aes(x=x1, y=y1)) + 
  geom_point(alpha = 0.2, cex = 3) +
  stat_smooth(method="glm", se=T, method.args = list(family=binomial)) +
  geom_density(data = ex[!ex$y, ], aes(x = x1, y = ..density..),
               alpha = 0.2, fill = "tomato") +
  theme_bw()

This code gives me:

enter image description here

I need to add an inverted density plot for upper points, something like this (I've tried to use the code of image bellow, available here, but without success):

enter image description here

Thank you!


Solution

  • One option would be to use geom_polygon with stat="density" where we could invert the density using after_stat(1 - density). Additionally I added a geom_path for the black colored outline (geom_polygon will connect the endpoints too):

    library(ggplot2)
    
    ggplot(ex, aes(x = x1, y = y1)) +
      geom_point(alpha = 0.2, cex = 3) +
      stat_smooth(method = "glm", se = T, method.args = list(family = binomial)) +
      geom_density(
        data = ex[!ex$y, ], aes(x = x1, y = after_stat(density)),
        alpha = 0.2, fill = "tomato"
      ) +
      geom_polygon(
        data = ex[ex$y, ], aes(x = x1, y = after_stat(1 - density)),
        alpha = 0.2, fill = "blue", stat = "density"
      ) +
      geom_path(
        data = ex[ex$y, ], aes(x = x1, y = after_stat(1 - density)),
        stat = "density", color = "black"
      ) +
      theme_bw()
    #> `geom_smooth()` using formula = 'y ~ x'