ggExtra icon indicating copy to clipboard operation
ggExtra copied to clipboard

Marginal plots don't reflect axis limits made using coordinate system (e.g., coord_cartesian())

Open crew102 opened this issue 6 years ago ā€¢ 11 comments

As mentioned by @Futats in #75

library(ggplot2)
library(ggExtra)

p <- ggplot(mtcars) +
  geom_point(aes(wt, drat)) +
  coord_cartesian(xlim = c(4, 5.5))

ggMarginal(p)

Created on 2018-08-15 by the reprex package (v0.2.0).

crew102 avatar Aug 15 '18 16:08 crew102

Turns out this is a really hairy issue. I think it can be fixed, but it leads to some very confusing code, so much so that I'm not sure it's worth it to fix.

crew102 avatar Sep 29 '18 02:09 crew102

Axis limits are basic plot attributes. Is there any way to specify axis limits for the marginal plots?

DoaneAS avatar Jun 03 '19 18:06 DoaneAS

Do you mean you want to set the limits of the marginal plots to be different from the limits used in the scatter plot?

crew102 avatar Jun 03 '19 20:06 crew102

I want the limits to be the same in the scatter and box plots. Iā€™m setting scatter plot limits with coord_cartesian(xlim = xlim, ylim = ylim), but the box plots are not picking this up.

DoaneAS avatar Jun 04 '19 02:06 DoaneAS

Try using xlim/ylim or scale_x_continuous/scale_y_continuous:

library(ggplot2)
library(ggExtra)

p <- ggplot(mtcars) +
  geom_point(aes(wt, drat)) +
  xlim(c(4, 5.5))

ggMarginal(p)
#> Warning: Removed 28 rows containing missing values (geom_point).

Created on 2019-06-03 by the reprex package (v0.2.0.9000).

crew102 avatar Jun 04 '19 03:06 crew102

Using xlim/ylim or scale_x_continuous work so that the marginal plots adapt to the same axis, which works welll for scatterplots but not for additional line plots as they are cut at the limits even if the shown axis extends a bit further. Therefore, I would like to use coord_cartesian or alternatively scale_x_continuous(limits=limits, expand=c(0,0)). I want to plot a scatterplot together with a geom_line() without cutting any data AND with marginal boxplots using the same axis range. Is there any solution for this issue already?

piaebe avatar Oct 29 '19 19:10 piaebe

Hmm, my first thought is probably not, there won't be a way to do that b/c it will require coord_cartesian. If you can provide an example of the type of behavior that you're seeing and what you would prefer to see, I can take a closer look at it.

crew102 avatar Oct 30 '19 16:10 crew102

Thank you for considering my request! Aim: a scatterplot together with boxplot margins, a line plot and defined axis limits, because I have several subplots and want to use the same limits. Problem: either no limits, cut line plot or boxplots not matching data points Example code:

line <- data.frame("wt"=0:10,"drat"=0:10)

p <- ggplot(data=mtcars, aes(wt, drat)) +
  geom_point() +
  geom_line(data=line)

limits <- c(1, 6)

ggMarginal(p, type="boxplot") # boxplots match, but no limits defined

ggMarginal_p

p1 <- p + coord_cartesian(limits)
ggMarginal(p1, type="boxplot") # boxplot does not adapt to coord_cartesian

ggMarginal_p1

p2 <- p + xlim(limits)
ggMarginal(p2, type="boxplot") # line stops before end of plot window, boxplot good

ggMarginal_p2

p3 <- p + scale_x_continuous(limits=limits, expand=c(0,0))
ggMarginal(p3, type="boxplot") # boxplot does not adapt to expand, line ok (but actually only if line data points matches with limits)

ggMarginal_p3

piaebe avatar Nov 01 '19 20:11 piaebe

Hi @pricklpitty , thanks for clarifying/for the good examples. Unfortunately what you want to do would require a fix to how ggMarginal deals with the the "cartesian" coordinate system (i.e., a fix to the issue originally reported here), which isn't easy. We've decided not to attempt to fix this issue for now, as it would introduce quite a bit of complexity into the code.

crew102 avatar Nov 02 '19 00:11 crew102

Hi @crew102, thank you for your response. Yes, it would require to fix the original issue posted here. Unfortunately, I am still struggling with this issue, e.g. trying to use dummy data to fix the limits, but I do not succeed in matching boxplots AND continue the line to figure limits, I can only produce one of the targets at once... Do you have any workaround suggestion for me?

piaebe avatar Nov 08 '19 10:11 piaebe

Hmm, yeah, sorry, I can't think of one.

crew102 avatar Nov 14 '19 03:11 crew102