0

I tried to align to plot one on top of the other with ggplot2. Data in two plots show each category of x property.

Here you can download the sample data: https://gist.github.com/itsvenu/97605e07b626d04f290fb39af7ccbc3a

Following is the code I wrote to produce the figure

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)

That produced

fig

I would like to arrange the top and bottom panel shadings on the same lines. For some reason, there coming a space between y axis and first beeswarm dots. I tried expand argument, but it didn't change anything.

Any help would be greatly appreciated.

Thank you.

cat
  • 109
  • 8
  • 3
    Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung May 10 '19 at 08:21
  • Hi, thanks for the reply. It's highly protected patient data (I'm not allowed to share). That's why I didn't share it in the first place. I will try to make some data to reproduce the problem. – cat May 10 '19 at 08:53
  • @Tung, I added provided example data and code to reproduce the problem. – cat May 10 '19 at 13:17
  • 1
    try p1 without coord_trans and with coord_trans. you will see with coord_trans the geoms change with extra space around the plots.. trying to see why. But the doc `?coord_trans` clearly mentions the geoms will be changed.... – infominer May 10 '19 at 13:57
  • Have you tried setting the limits on the x axis to be the same for both plots? `scale_x_discrete(limits = c(0,4))` – TheSciGuy May 10 '19 at 18:16

0 Answers0