Ggplot2: can a non-aesthetic parameter change depending on a factor?

I want to create a ggplot in which the statistic is changed according to the aesthetically displayed factor. Specifically, I would like to create a contour plot using stat_density2d (), I would like to map the discrete factor to color, and I would like to specify a different gap value for each factor level.

Here's a minimal working example:

d <- data.frame(x=c(rnorm(500), rnorm(500, 2)), y=rnorm(1000), z=c(rep("a", 500), rep("b", 500)))

ggplot(d, aes(x, y, fill=z)) + 
      stat_density2d(breaks=.05, geom="polygon", alpha=.5)

      

enter image description here

This is what I'm going for, except for the gaps that are the same between factors. I am looking for a way to specify different gap values โ€‹โ€‹for each factor. One way to do this is to create a separate layer for each factor:

ggplot() + 
      stat_density2d(data=d[d$z=="a",], aes(x, y), breaks=.05, geom="polygon", alpha=.5, fill="red") +
      stat_density2d(data=d[d$z=="b",], aes(x, y), breaks=.1, geom="polygon", alpha=.5, fill="blue")

      

enter image description here

But it doesn't work because I am losing the legend and it is too cumbersome for cases with more than two factors.

I would like to combine the benefits of these two cases. I'm pretty sure I've seen a way to do what I'm trying to do, but now can't seem to find it as it is relevant. Anyone have any thoughts on a possible solution?

+3


source to share


1 answer


Commented on this a couple of years ago - so taking the decision directly from Kohske answer

# Data
set.seed(1)    
d <- data.frame(x=c(rnorm(500), rnorm(500, 2)), y=rnorm(1000), 
                                   z=c(rep("a", 500), rep("b", 500)))

library(plyr)
library(ggplot2)

hls <- mapply(function(x, b) stat_density2d(data = x, breaks = b, 
                    geom="polygon", alpha=.5), dlply(d, .(z)), c(0.05, 0.1))

ggplot(d, aes(x, y, fill=z)) + hls + scale_fill_manual(values=c("red", "blue"))

      



enter image description here

# Compare
ggplot() + 
  stat_density2d(data=d[d$z=="a",], aes(x, y), breaks=.05, geom="polygon", 
                                                      alpha=.5, fill="red") +
  stat_density2d(data=d[d$z=="b",], aes(x, y), breaks=.1, geom="polygon",
                                                     alpha=.5, fill="blue")

      

+5


source







All Articles