Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feature) Specify layer(s) that ggMarginal uses #144

Closed
wpetry opened this issue Jun 21, 2019 · 6 comments
Closed

(feature) Specify layer(s) that ggMarginal uses #144

wpetry opened this issue Jun 21, 2019 · 6 comments

Comments

@wpetry
Copy link

wpetry commented Jun 21, 2019

This feature would allow the user to specify which layer of the ggplot is used to make the marginal plots. It would be a more controlled middle ground between the original behavior addressed in issue #67 and the current behavior (using all layers).

@daattali
Copy link
Owner

Could you please post some usecases/examples of how you see this working?

@wpetry
Copy link
Author

wpetry commented Jun 21, 2019

I'm making a phase plot using a ribbon annotation to separate areas with qualitatively different outcomes. I want to plot the marginal density of the scatter points, but the data for the annotation ribbon appear to be driving the behavior of the marginal density plots.

Load packages, make data:

library(tidyverse)
library(ggExtra)

theme_set(theme_bw() + theme(panel.grid = element_blank()))

# make scatterplot data
set.seed(503234)
dat <- tibble(x = rbeta(5000, shape1 = 5, shape2 = 1),
              y = exp(rnorm(5000, mean = 0, sd = 2)))
# make data for annotation ribbon (i.e., the phase space boundaries)
rib <- tibble(x = seq(0, 1, 0.001)) %>%
  mutate(min = 0.99 * x + 0.01,
         max = -99 * x + 100)
              
# Without annotation layer, WORKS
p <- ggplot(dat, aes(x = x, y = y))+
  geom_point(alpha = 0.15)+
  scale_y_log10()+
  coord_cartesian(xlim = c(0, 1))
ggMarginal(p, type = "density")

Rplot02

# With phase boundary annotation layer, fails
q <- ggplot(dat, aes(x = x, y = y))+
  annotate("ribbon", x = rib$x, ymin = rib$min, ymax = rib$max,
           fill = "lightblue")+
  geom_point(alpha = 0.15)+
  scale_y_log10()+
  coord_cartesian(xlim = c(0, 1))
ggMarginal(q, type = "density")

Rplot03

Perhaps one could add an argument layers to the function that accepts a numeric vector of plot layers to use for the marginal plots. This would be difficult for users (they'd have to manually count which layer would be the focus) and fragile (adding another layer could silently return marginal plots on undesired layers).

@daattali
Copy link
Owner

This code does work for me. I tried both the CRAN and the latest GitHub versions.

library(tidyverse)
library(ggExtra)

theme_set(theme_bw() + theme(panel.grid = element_blank()))

# make scatterplot data
set.seed(503234)
dat <- tibble(x = rbeta(5000, shape1 = 5, shape2 = 1),
              y = exp(rnorm(5000, mean = 0, sd = 2)))
# make data for annotation ribbon (i.e., the phase space boundaries)
rib <- tibble(x = seq(0, 1, 0.001)) %>%
    mutate(min = 0.99 * x + 0.01,
           max = -99 * x + 100)

q <- ggplot(dat, aes(x = x, y = y))+
    annotate("ribbon", x = rib$x, ymin = rib$min, ymax = rib$max,
             fill = "lightblue")+
    geom_point(alpha = 0.15)+
    scale_y_log10()+
    coord_cartesian(xlim = c(0, 1))
ggMarginal(q, type = "density")

I get
image

@davidizquierdogomez
Copy link

Hello, is it possible to add axis to marginal plots created with ggMarginal () ?

@daattali
Copy link
Owner

I think you're talking about the same thing as #64

@davidizquierdogomez
Copy link

I think you're talking about the same thing as #64

Exactly, @daattali. I am a reviewer on a paper and it's a pity that the proportions of the density plots are not display to get, in a quick glance, all the info needed to interpret the figure.

On the other hand, I'm building up a figure using a bubble plot (latitude and longitude points) over a map (shapefile). I'm using several geom_line(), geom_path(), geom_polygon() layers in a ggplot2 mode. Then the ggMarginal() takes the info of all latitude and longitud points showed in the figure, including the borders of the shapefiles which are build up based on points with a latitude and longitude values...is there any way to make the ggMarginal() function take the latitude and longitud points of the elements georeferenciated in the map but not those belonging to the borders of the countries?

thanks for a great package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants