简体   繁体   English

如何在ggplot2中的y轴上的断点之间设置相等的距离(即使实际距离不同)?

[英]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 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 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). 我唯一遇到的问题是如何在y轴上设置断点,使得每个断点之间的距离相等(即使两者之间的实际距离不同)。 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. 更准确地说:在我要复制的情节中,我们在y轴上有五个中断,范围从10 ^ -1到10 ^ -5:即使实际距离在10 ^ -1和10 ^ -2之间比10 ^ -4和10 ^ -5大得多,这些中断之间的距离相等。

I read almost the whole book of Hadley Wickham on "ggplot2" to see how to do it. 我在“ ggplot2”上阅读了几乎所有的Hadley Wickham的书,以了解如何做。 I tried setting the limits and breaks in the scaling layer of the plot. 我尝试在图的缩放层中设置限制和中断。 I tried changing the expand argument. 我尝试更改expand参数。 I tried "zooming" into the graph using coord_cartesian and I also tried to change the ratio between the y- and x-axis. 我尝试使用coord_cartesian将其“缩放”到图形中,并且还尝试更改y轴和x轴之间的比率。 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. 在实际情况下,绘制的分布范围为0到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. 我期望发生的是y轴上的中断范围从10 ^ -1到10 ^ -5,但是每次中断之间的距离是相等的。

This is my first time asking a question on Stackoverflow, I apologize for any inconvenience. 这是我第一次问有关Stackoverflow的问题,对于给您带来的任何不便,我深表歉意。 Also, I made sure that there is no question related to mine, such that it is not a duplicate. 另外,我确保没有与我相关的问题,因此它不是重复的。

This is giving me the kind of y axis you want: 这给了我想要的y轴:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM