Closed jarauh closed 6 years ago
Hi @jarauh ,
We probably don't want to try and support other plot types (i.e., non-scatter plots) for the main plot (see https://github.com/daattali/ggExtra/issues/131#issuecomment-429573557 for further details). It would also be difficult to support 2-d density plots b/c of the issue shown below:
library(ggplot2)
p <- ggplot(faithful, aes(eruptions, waiting))
# ggMarginal pulls the scatter plot data out of the built ggplot2 object and
# creates marginal plots from it.
pb <- ggplot_build(p + geom_point())
head(pb$data[[1]])
#> x y PANEL group shape colour size fill alpha stroke
#> 1 3.600 79 1 -1 19 black 1.5 NA NA 0.5
#> 2 1.800 54 1 -1 19 black 1.5 NA NA 0.5
#> 3 3.333 74 1 -1 19 black 1.5 NA NA 0.5
#> 4 2.283 62 1 -1 19 black 1.5 NA NA 0.5
#> 5 4.533 85 1 -1 19 black 1.5 NA NA 0.5
#> 6 2.883 55 1 -1 19 black 1.5 NA NA 0.5
# however, the data in the built 2-d density plot is not the original data, but
# rather the data that is describing the density curves.
p_density <- p + geom_density2d()
p_density
pb <- ggplot_build(p_density)
head(pb$data[[1]])
#> level x y piece group PANEL colour size linetype alpha
#> 1 0.002 1.600000 67.31097 1 -1-001 1 #3366FF 0.5 1 NA
#> 2 0.002 1.635354 67.60062 1 -1-001 1 #3366FF 0.5 1 NA
#> 3 0.002 1.638725 67.62626 1 -1-001 1 #3366FF 0.5 1 NA
#> 4 0.002 1.670707 67.87765 1 -1-001 1 #3366FF 0.5 1 NA
#> 5 0.002 1.706061 68.12348 1 -1-001 1 #3366FF 0.5 1 NA
#> 6 0.002 1.712013 68.16162 1 -1-001 1 #3366FF 0.5 1 NA
Created on 2018-10-16 by the reprex package (v0.2.0.9000).
This may not always have been the case for ggplot2 (i'm using ggplot2 v 3.0.0), and I assume it wasn't if you were indeed getting marginal plots from ggMarginal()
that made sense with your 2-d density plots. Regardless, I don't think it makes sense to support this kind of feature to begin with. I'd suggest installing whatever version of ggExtra was working with 2d density plots for you in the past and going from there.
Ah, I understand. Thank you for the explanation.
So the problem seems to be: The original data disappears when using ggplot_build
. However, before ggplot_build
, it is difficult to anticipate all transformations that are hidden in the ggplot object, and so ggMarginal
has to use ggplot_build
. Is this more or less correct?
By the way, is there a good place to learn about the ggplot2
-internals? Apart from reading the sources...
So the problem seems to be: The original data disappears when using ggplot_build.
Kinda...You can still get the raw data using ggplot_built
, but it's just not located where it normally is (i.e., when the plot is a scatter plot). The bigger issue really has to do with the fact that supporting marginal plots for plots that aren't scatter plots would be extremely messy and is beyond the scope of the package.
However, before ggplot_build, it is difficult to anticipate all transformations that are hidden in the ggplot object, and so ggMarginal has to use ggplot_build. Is this more or less correct?
Yep.
By the way, is there a good place to learn about the ggplot2-internals? Apart from reading the sources...
You can try the ggplot2 book: https://github.com/hadley/ggplot2-book
Thanks for your answers. I have the book, but it does not go into the details. For example, ggplot_build
is not mentioned, not even ggproto
. The ggplot homepage has some more information (on ggproto
), but again, not too much detail about ggplot_build
and other internals. But ok, maybe the documentation is not fast enough to cover recent changes to internals (given the recent jump in major version).
In any case, further pointers to ggplot insight would be appreciated, if available. I will close the issue now.
I would look to the source if you can't find details in the book.
In earlier versions of
ggExtra
, it was possible to add aggMarginal
to ageom_line
. The current code explicitly looks for ageom_?point
in the plot and fails if no scatter plot is present.When there are lots of data points, I find that scatter points are not very helpful, and I prefer to simply plot the 2d-density. A marginal plots can still be helpful in such a situation.
Example (adapted from the
geom_density_2d
-documentation):Suggestion:
p
contains a scatter plot.x
andy
) as a fallback.