Skip to content

Remove extra space created by coord_trans #3338

Closed
@itsvenu

Description

@itsvenu

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions