daattali / ggExtra

📊 Add marginal histograms to ggplot2, and more ggplot2 enhancements
http://daattali.com/shiny/ggExtra-ggMarginal-demo/
Other
380 stars 48 forks source link

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

Open crew102 opened 6 years ago

crew102 commented 6 years ago

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 commented 5 years ago

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.

DoaneAS commented 5 years ago

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

crew102 commented 5 years ago

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

DoaneAS commented 5 years ago

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.

crew102 commented 5 years ago

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).

piaebe commented 4 years ago

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?

crew102 commented 4 years ago

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.

piaebe commented 4 years ago

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

crew102 commented 4 years ago

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.

piaebe commented 4 years ago

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?

crew102 commented 4 years ago

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