Closed Sim19 closed 7 years ago
Can you provide a reproducible example of the bug you are seeing? In other words, can you provide some example code that results in the unwanted graph that you are getting?
Here is a minimal reproducible case
library(tidyverse)
library(ggExtra)
df = tibble(x = rnorm(1000), y = rnorm(1000))
ggplot(df, aes(x = x, y = y)) + geom_point() %>% ggMarginal(type = 'histogram')
p1 = ggplot(df, aes(x = x, y = y)) + geom_point()
m1 = ggMarginal(p1, type = 'histogram')
print(m1)
rx = range(df$x)
ry = range(df$y)
r = c(min(rx, ry), max(rx,ry))
p2 = ggplot(df, aes(x = x, y = y)) + geom_point() + xlim(r) + ylim(r)
m2 = ggMarginal(p2, type = 'histogram')
print(m2)
P.S., If I do
install_github('daattali/ggExtra', ref="v0.6")
Then the second plot comes out properly.
Thanks, this does look like a regression bug. Although it looks to me like only the y axis is affected, and not the x axis, do you agree?
Here is a more minimal example showing the problem (using your code but more minimal for debugging):
library(ggExtra)
library(ggplot2)
df <- data.frame(x = rnorm(1000), y = rnorm(1000))
p1 <- ggplot(df, aes(x = x, y = y)) + geom_point()
m1 <- ggMarginal(p1, type = 'histogram')
print(m1)
p2 <- p1 + xlim(-5, 5) + ylim(-5, 5)
m2 <- ggMarginal(p2, type = 'histogram')
print(m2)
Hmm, I wonder what is going on here. I'll take a look by week's end.
I agree that it only seems to affect the y-axis.
I like your more minimalist reproducible example. Nice chance for me learn about being more concise.
Eliminate any package that is not 100% necessary, and use as few variables as possible - use hardcoded values instead to make it clear exactly what's going on :) Makes it much easier to debug
I'm going to add a unit test to the the upcoming pr later today...forgot to do that before i pushed.
@Sim19 and @jonathan-g please make sure the fix works for you. You can thank @crew102
Hi, this issue isn't solved, right? At least for me it seems to be open? In my case ggMarginal does not consider the limitation of the y-axis:
The code line: diffplot <- ggplot(df, aes(APD$Bias_voltage_irr, diff, col=APD$Lot)) + geom_point() + theme_bw() + theme(legend.position = "bottom", legend.direction = "horizontal") + geom_hline(yintercept = 0, linetype = 3) + geom_hline(yintercept = mean(differences.frame$diff)) + geom_hline(yintercept = mean(differences.frame$diff) + 1sd.diff, linetype = 2) + geom_hline(yintercept = mean(differences.frame$diff) - 1sd.diff, linetype = 2) + ylab("Difference pre and post Irradiation / V") + xlab("Voltage / V") + coord_cartesian(ylim = c(-10, 10)) + labs(colour="Lots") + guides(colour = guide_legend(nrow = 1)) ggMarginal(diffplot, type="histogram", bins = 100, colour = "darkblue")
Based on the color of the histograms in your example, it looks like you may be using an old version of ggExtra. Try updating it with devtools::install_github("daattali/ggExtra")
.
I have the same issue DeLaRiva reported, even after updating using devtools::install_github("daattali/ggExtra")
.
It appears that ggMarginal()
doesn't scale with coord_cartesian()
, though it scales properly with xlim()
and ylim()
. Is this intentional?
Here's an addendum to the earlier example:
df <- data.frame(x = rnorm(1000), y = rnorm(1000))
p1 <- ggplot(df, aes(x = x, y = y)) + geom_point() + ggtitle("xlim / ylim") m1 <- ggMarginal(p1, type = 'histogram') print(m1)
p2 <- p1 + xlim(-10, 10) + ylim(-10,10) + ggtitle("xlim / ylim") m2 <- ggMarginal(p2, type = 'histogram') print(m2)
p3 <- p1 + coord_cartesian(xlim = c(-10, 10), ylim = c(-10, 10)) + ggtitle("Coord_Cartesian") m3 <- ggMarginal(p3, type = 'histogram') print(m3)
It wasn't intentional, and is relatively easily fixed. The only question from my point of view is, whenever coord_cartesian()
is used in the scatter plot, should the marginal plots be created using all of the data, or only the data that is shown in the scatter plot?
From ?coord_cartesian
:
# There are two ways of zooming the plot display: with scales or
# with coordinate systems. They work in two rather different ways.
p <- ggplot(mtcars, aes(disp, wt)) +
geom_point() +
geom_smooth()
p
# Setting the limits on a scale converts all values outside the range to NA.
p + scale_x_continuous(limits = c(325, 500))
# Setting the limits on the coordinate system performs a visual zoom.
# The data is unchanged, and we just view a small portion of the original
# plot. Note how smooth continues past the points visible on this plot.
p + coord_cartesian(xlim = c(325, 500))
For example, if the user uses coord_cartesian(xlim = c(5, 20))
in their scatter plot, should the marginal density plot look like this:
library(ggplot2)
p <- ggplot(mtcars) + geom_density(aes(mpg))
p + coord_cartesian(xlim = c(5, 20))
...Or like this:
p + xlim(c(5, 20))
#> Warning: Removed 14 rows containing non-finite values (stat_density).
I think the second choice will provide more intuitive output. Thoughts, @daattali ?
Hi,
I guess due to a recent update of the package, there are a few bugs now, which weren't there before. I'd like to make the axes of my scatterplot symmetric. But when adding the histograms on the y and x axes with ggExtra:ggMarginal($GGPLOT, 'histogram'), the histogram of the axis that is enlarged the most, is not shown. Instead, two vertical bars are drawn (seemingly random). When the limits of the axes in $GGPLOT are not changed, the plot is fine.
I last ran that exact code on the same data on June 23rd, 2017. Then, the histograms on the axes were just fine.