bodkan / slendr

Population genetic simulations in R 🌍
https://bodkan.net/slendr
Other
55 stars 6 forks source link

`plot_model()` does not adjust migration arrow ends when `log=TRUE` #131

Closed IsabelMarleen closed 1 year ago

IsabelMarleen commented 1 year ago

When using plot_model() with log=TRUE, the migration arrows are not adjusted to the new scale, so arrows end in the wrong place.

Small reproducible example:

library(slendr)
init_env()

p1 <- population("p1", time=10, N=100)
p2 <- population("p2", time=10, N=100)
gf <- gene_flow(from=p1, to=p2, start= 5, end=0, rate=0.05)
model <- compile_model(list(p1, p2), gene_flow = gf, generation_time = 1, direction = "backward")

plot_model(model)
plot_model(model, log=TRUE)

reprex_plotting_log_false reprex_plotting_log_true

bodkan commented 1 year ago

Thanks for the bug report, @IsabelMarleen!

plot_model() is one piece of ggplot2 voodoo, but I suspect this has something to do with adjusting an 0-valued coordinates of the "bottom-most" geoms when log = TRUE is set. I.e. what's being dealt with here w.r.t. to the population polygons themselves.

I wonder if an analogous ifelse trick can be done with the terminal points of the gene-flow arrows? Makes sense, I think?

Although I do wonder why this doesn't give a warning, because it shouldn't be possible to plot y = 0 coordinates anyway?

This is just a quick analysis post-lunch -- I'll have to think about it more once do food coma passes. :)

IsabelMarleen commented 1 year ago

I think you are right. It does give a warning: Transformation introduced infinite values in continuous y-axis. I should have included it, I think I subconsciously assumed any issue like that would affect the arrow and the block equally and ignored it. I just tested what happens when both populations and the migration end at 1 instead of 0 and then the arrow works correctly. So there is probably an extra check included for populations ending at 0 on log scale but not for the arrows.

bodkan commented 1 year ago

This should be fixed now.

@IsabelMarleen: I'm not sure if you discovered this issue by accident or if this prevented some of your demes-r models from plotting properly. If the former, feel free to ignore this message. If it's the later, maybe check if everything works as expected now?