Using different scales as fill based on factor

Viewed 2237
cnt = 100
df <- data.frame(x = c(rnorm(cnt, mean = 3), rnorm(cnt, mean = 0)),
                 y = rnorm(2 * cnt), g = rep(0:1, each = cnt))

ggplot(df, aes(x, y, color = as.factor(g))) + 
  stat_density2d(aes(fill = ..level..), alpha = 0.3, geom = "polygon")

This creates a filled contour plot based on factor: enter image description here

I'd like to use a different fill scale for each contour, such that g = 0 has a red fill while g = 1 has a blue fill. Is this possible and if so, how?

1 Answers
Related