0

I first make a plot

   df <- data.frame(x = c(1:40, rep(1:20, 3), 15:40))
    p <- ggplot(df, aes(x=x, y = x)) +
      stat_density2d(aes(fill='red',alpha=..level..),geom='polygon', show.legend = F)

Then I want to change the geom_density values and use these in another plot.

# build plot 
q <- ggplot_build(p)

# Change density
dens <- q$data[[1]]     
dens$y <- dens$y - dens$x 

Build the other plot using the changed densities, something like this:

  # Built another plot
    ggplot(df, aes(x=x, y =1)) + 
      geom_point(alpha = 0.3) +
      geom_density2d(dens)

This does not work however is there a way of doing this?


EDIT: doing it when there are multiple groups:

df <- data.frame(x = c(1:40, rep(1:20, 3), 15:40), group = c(rep('A',40), rep('B',60), rep('C',26)))
p <- ggplot(df, aes(x=x, y = x)) +
  stat_density2d(aes(fill=group,alpha=..level..),geom='polygon', show.legend = F)

q <- ggplot_build(p)
dens <- q$data[[1]]     
dens$y <- dens$y - dens$x

ggplot(df, aes(x=x, y =1)) + 
  geom_point(aes(col = group), alpha = 0.3) +
  geom_polygon(data = dens, aes(x, y, fill = fill, group = piece, alpha = alpha)) +
  scale_alpha_identity() +
  guides(fill = F, alpha = F)

Results when applied to my own dataset Although this is exactly what I'm looking for the fill colors seem not to correspond to the initial colors (linked to A, B and C): enter image description here

CodeNoob
  • 1,988
  • 1
  • 11
  • 33

1 Answers1

1

Like this? It is possible to plot a transformation of the shapes plotted by geom_density. But that's not quite the same as manipulating the underlying density...

ggplot(df, aes(x=x, y =1)) + 
  geom_point(alpha = 0.3) +
  geom_polygon(data = dens, aes(x, y, fill = fill, group = piece, alpha = alpha)) +
  scale_alpha_identity() +
  guides(fill = F, alpha = F)

enter image description here


Edit - OP now has multiple groups. We can plot those with the code below, which produces an artistic plot of questionably utility. It does what you propose, but I would suggest it would be more fruitful to transform the underlying data and summarize that, if you are looking for representative output.

ggplot(df, aes(x=x, y =1)) + 
  geom_point(aes(col = group), alpha = 0.3) +
  geom_polygon(data = dens, aes(x, y, fill = group, group = piece, alpha = alpha)) +
  scale_alpha_identity() +
  guides(fill = F, alpha = F) +
  theme_minimal()

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Wow thankyou Jon that's muchhhhh better than what I did now! I now built the object and edited the original object, rebuild it and changed the axis etc. took my like 15 lines of code. – CodeNoob Jun 08 '19 at 09:29
  • Is this also possible with multiple groups (see edit)? – CodeNoob Jun 08 '19 at 09:54
  • Didn't get notified with the edit, thankyou for the suggestion for the multi group plot however the fill seem not to correspond to the original as can be seen when `guides(fill = T, alpha = T)` there are multiple fill colours instead of the three colours for A,B and C (see my edit to see how it looks on my dataset) – CodeNoob Jun 09 '19 at 13:23
  • Nearly got is using `geom_polygon(data = dens, aes(x, y, fill = fill, group = group, alpha = alpha))` however the order of the colors seems to change – CodeNoob Jun 09 '19 at 13:30
  • What about `geom_polygon(data = dens, aes(x, y, fill = group, group = piece, alpha = alpha)) +`? – Jon Spring Jun 09 '19 at 17:23
  • Doesn't work either, maybe it isn't possible at all – CodeNoob Jun 10 '19 at 11:22