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

Remove extra space created by coord_trans #3338

Closed
itsvenu opened this issue May 22, 2019 · 9 comments
Closed

Remove extra space created by coord_trans #3338

itsvenu opened this issue May 22, 2019 · 9 comments

Comments

@itsvenu
Copy link

itsvenu commented May 22, 2019

I'm trying to produce a two panel figure for my data. At one point I had to use coord_trans in my code which created an extra space between y-axis and first beeswarm dots.

Here is the figure

fig

I would like to remove the space between y-axis and first set of dots so that gray-white shading align in 2 panels. If I don't use coord_trans, shadings are aligned perfectly. Although, coord_trans documentation says that it will change visual appearance of geoms, I'm hoping there might be a solution to produce a figure I'm thinking of.

Following is the sample data and to reproduce the figure

data: Link to data

all_sb <- unique(mp_mb_sampledata$SB) 

cl_min <- seq(from = 0.5, to = max(as.numeric(as.factor(all_sb))), by = 1)[1:length(all_sb)]
cl_max <- seq(from = 1.5, to = max(as.numeric(as.factor(all_sb))) + 0.5, by = 1)[1:length(all_sb)]

shading_cols <- data.frame(min = cl_min,
                           max = cl_max,
                           col = rep(c(0, 1), length(cl_min)))


## top panel
p1 <- ggplot()+
  ggbeeswarm::geom_quasirandom(data = mp_mb_sampledata, aes(x = SB, y = mut_per_mb))+
  geom_rect(data = shading_cols,
            aes(xmin = min, xmax = max, ymin = -Inf, ymax = Inf,
                fill = factor(col), alpha = 0.01))+
  scale_fill_manual(values = c("white", "gray"), guide = FALSE)+
  ggbeeswarm::geom_quasirandom(data = mp_mb_sampledata, aes(x = SB, y = mut_per_mb))+
  coord_trans(y="log2")+
  theme_classic(base_size = 18)+
  xlab("")+ylab("")+
  theme(legend.position = "none",
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.line.x = element_blank(),
        axis.text = element_text(color = "black"))

## bottom panel
p2 <- ggplot()+
  geom_bar(data = req_bc, aes(x = SB, y = Freq, fill = MutationType), stat = "identity")+
  geom_rect(data = shading_cols,
            aes(xmin = min, xmax = max, ymin = -Inf, ymax = Inf,
                fill = factor(col), alpha = 0.01))+
  scale_fill_manual(values = color_pal)+
  geom_bar(data = req_bc, aes(x = SB, y = Freq, fill = MutationType), stat = "identity")+
  theme_classic(base_size = 18)+
  theme(axis.text = element_text(color = "black"),
        plot.margin = unit(c(0.01, 5.5, 5.5, 5.5), "pt"))+
  ylab("Fraction")+
  scale_y_continuous(breaks = c(0, 0.50, 1), labels = c(0, 0.50, 1))+
  ggpubr::rotate_x_text(angle = 90, hjust = 1)

## both in one

p1 + p2 + patchwork::plot_layout(ncol = 1)

Thanks a lot for the great package and support.

@smouksassi
Copy link

smouksassi commented May 22, 2019

you need to manually set you r discrete x expansions

library(ggplot2)
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang
library(ggbeeswarm)
library(patchwork)

## top panel
p1 <- ggplot(mpg, aes(class,cty) )+
  geom_quasirandom()+
  coord_trans(y="log2")+
  scale_x_discrete(expand=c(0,0.6))
## bottom panel
p2 <- ggplot(mpg, aes(class,fill=drv))+
     geom_bar(position = "fill")+
  scale_x_discrete(expand=c(0,0.6))

p1 + p2 + plot_layout(ncol = 1)

Created on 2019-05-22 by the reprex package (v0.3.0)

@itsvenu
Copy link
Author

itsvenu commented May 22, 2019

Thanks for the reply. But it doesn't fix the problem. If you see in mpg plot, last variable suv is not aligned to the above panel. I also tried adjusting expand values but not success yet.

@paleolimbot
Copy link
Member

It looks like coord_trans() doesn't expand its scales, nor is there an option to do so. The best way to do this would be to use scale_y_continuous(trans = "log2") rather than coord_trans().

library(ggplot2)
library(patchwork)

p <- ggplot(mpg, aes(class, cty))+
  geom_boxplot() 

wrap_plots(
  p,
  p + scale_y_continuous(trans = "log2", breaks = seq(10, 35, by = 5)),
  ncol = 1
)

If you really need coord_trans(), you can fudge it by setting xlim and ylim in coord_cartesian() (for the first plot) and limx and limy in coord_trans().

library(ggplot2)
library(patchwork)

p <- ggplot(mpg, aes(class, cty))+
  geom_boxplot() 

wrap_plots(
  p + coord_cartesian(xlim = c(0.4, 7.6), expand = FALSE),
  p + coord_trans(y = "log2", limx = c(0.4, 7.6)),
  ncol = 1
)

Created on 2019-05-22 by the reprex package (v0.2.1)

@itsvenu
Copy link
Author

itsvenu commented May 22, 2019

Hi @paleolimbot thanks a lot for the suggestion. The second approach seems to be working as expected.

When I used the first approach, after scale_y_continuous (replacing coord_trans) in my code above, the alternate shading effects were gone . But they were preserved with coord_trans. Do you see anything wrong in my code or is it something to do with scale_y_continuous trans args?

Thanks a lot for the help.

@paleolimbot
Copy link
Member

I don't think it's the trans arg, I think it's something to do with the way that you've coded geom_rect(). In particular, I think you're mapping some aesthetics when you need to set the aesthetics (outside aes()). This is how I would code what you're trying to do:

library(ggplot2)
library(patchwork)

p <- ggplot(mpg, aes(class, cty)) +
  geom_boxplot(aes(class, cty)) +
  geom_rect(
    aes(xmin = xmin, xmax = xmax),
    data = data.frame(xmin = c(0.5, 3.5), xmax = c(1.5, 4.5)),
    ymax = Inf,
    ymin = -Inf,
    alpha = 0.1,
    inherit.aes = FALSE
  )

wrap_plots(
  p,
  p + scale_y_continuous(trans = "log2", breaks = seq(10, 35, by = 5)),
  ncol = 1
)

Created on 2019-05-22 by the reprex package (v0.2.1)

@itsvenu
Copy link
Author

itsvenu commented May 24, 2019

If I plot geom_rect after the points, point colors were faded slightly because of rect alpha. However, I will play around as per your suggestion. Thank you :)

@paleolimbot
Copy link
Member

I would encourage you to reach out at https://community.rstudio.com/ for questions like these! Here we are concerned with unexpected behaviour or features that you feel should be added. In this case, we're concerned with the irregular behaviour of scale expansion in coord_trans().

@paleolimbot paleolimbot added bug an unexpected problem or unintended behavior coord 🗺️ and removed bug an unexpected problem or unintended behavior coord 🗺️ labels May 24, 2019
@itsvenu
Copy link
Author

itsvenu commented May 24, 2019

Sure thing. Thanks for pointing it out.

@lock
Copy link

lock bot commented Nov 20, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Nov 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants