Repository navigation
Closed
Description
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
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
Labels
No labels

Activity
smouksassi commentedon May 22, 2019
you need to manually set you r discrete x expansions
Created on 2019-05-22 by the reprex package (v0.3.0)
itsvenu commentedon May 22, 2019
Thanks for the reply. But it doesn't fix the problem. If you see in
mpgplot, last variablesuvis not aligned to the above panel. I also tried adjustingexpandvalues but not success yet.paleolimbot commentedon May 22, 2019
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 usescale_y_continuous(trans = "log2")rather thancoord_trans().If you really need
coord_trans(), you can fudge it by settingxlimandylimincoord_cartesian()(for the first plot) andlimxandlimyincoord_trans().Created on 2019-05-22 by the reprex package (v0.2.1)
itsvenu commentedon 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(replacingcoord_trans) in my code above, the alternate shading effects were gone . But they were preserved withcoord_trans. Do you see anything wrong in my code or is it something to do withscale_y_continuous transargs?Thanks a lot for the help.
paleolimbot commentedon May 22, 2019
I don't think it's the
transarg, I think it's something to do with the way that you've codedgeom_rect(). In particular, I think you're mapping some aesthetics when you need to set the aesthetics (outsideaes()). This is how I would code what you're trying to do:Created on 2019-05-22 by the reprex package (v0.2.1)
itsvenu commentedon May 24, 2019
If I plot
geom_rectafter the points, point colors were faded slightly because ofrectalpha. However, I will play around as per your suggestion. Thank you :)paleolimbot commentedon May 24, 2019
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().itsvenu commentedon May 24, 2019
Sure thing. Thanks for pointing it out.
lock commentedon 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/