Search code examples
rggplot2plotlimit

How to set equal distance between breaks on y-axis in ggplot2 (even though the actual distance is different)?


I am trying to reproduce a plot from this paper:

https://www.nature.com/articles/s41467-019-10213-0

The plot I am trying to reproduce is the one on the far left:

https://www.nature.com/articles/s41467-019-10213-0/figures/2

I was able to prepare the data and plot it accordingly. The only problem I am stuck with is on how to set the breaks on the y-axis in a way that it would have an equal distance between each break (even though the actual distance between the two is different). More precisely: In the plot I want to reproduce, we have five breaks on the y-axis, ranging from 10^-1 to 10^-5: Even though the actual distance in range between 10^-1 and 10^-2 is much bigger than 10^-4 and 10^-5, the breaks have an equal distance to each other.

I read almost the whole book of Hadley Wickham on "ggplot2" to see how to do it. I tried setting the limits and breaks in the scaling layer of the plot. I tried changing the expand argument. I tried "zooming" into the graph using coord_cartesian and I also tried to change the ratio between the y- and x-axis. The data you will find here is just an arbitrary example, so everyone can somewhat reproduce the example. In the real case, the distribution drawn ranges form 0 to 100.

years <- 1:10
probs <- c(0.6788, 0.1232,0.0534534,0.0034235432,0.000452342341, 0.0000454234, 0.000002354222,0.000000987, 0.0000000384, 0.0000000042352452)

df <- as.data.frame(cbind(years, probs))


my_plot <- ggplot(data = df, mapping = aes(x = years, y = probs)) + xlab("L") + ylab("P(L)") 


my_plot + scale_x_continuous(breaks = seq(0,100,20)) + scale_y_continuous(expand =
                                                                            expand_scale(mult = c(0.0001, 0.02), add = c(0.0009, 0.001)), 
                                                                          trans= "sqrt", breaks = c(0.00001, 0.0001, 0.001, 0.01, 0.1), 
                                                                          limits= c(0, 0.2)) + geom_point(shape = 4) + geom_line() + theme(aspect.ratio=1.2)

What I expect to happen is that the breaks-range on the y-axis goes from 10^-1 to 10^-5, but in a way that the distance between each break is equal.

This is my first time asking a question on Stackoverflow, I apologize for any inconvenience. Also, I made sure that there is no question related to mine, such that it is not a duplicate.


Solution

  • This is giving me the kind of y axis you want:

    my_plot +
      scale_x_continuous(breaks = seq(0,100,20)) + 
      scale_y_log10(breaks = c(0.00001, 0.0001, 0.001, 0.01, 0.1),
                    limits= c(0.00001, 0.12),
                   expand = expand_scale(mult = c(0.0001, 0.02), 
                                         add = c(0.0009, 0.001)))+
      geom_point(shape = 4) + geom_line() + theme(aspect.ratio=1.2)