如何在同一个X轴的同一面板上绘制具有两个不同y轴范围的点? [英] How do I plot points with two different y-axis ranges on the same panel in the same X axis?

查看:286
本文介绍了如何在同一个X轴的同一面板上绘制具有两个不同y轴范围的点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ggplot来创建一个散点图,该ggplot共享一个X轴但具有两个不同比例的Y轴。

Y轴的底部有三个从0%到0.1%的比例,然后是0.1%到1%,最后是10%的规则间隔。



来自。 Hadley Wickham(ggplot2的创建者)在这里解释 ,介绍在这个轴上简单的休息。



请注意,这段代码很复杂,可能需要定制。更重要的是,它仍然有些误导性:一般来说,你应该把你的数据分成不同的地块,或者把所有的东西都放在日志范围内。尽管如此,值得一提的是,ggplot2让你可以访问这些工具,即使你用自己的脚砸自己也是如此!


I am trying to create a scatterplot using ggplot that shares an X axis but has a Y axis with two different scales.

The bottom of the Y axis has three scales from 0% to 0.1%, then 0.1% to 1%, and then finally regular intervals by 10%.

An example from here:

Is there a way to produce something like this in R using ggplot? Would I be modifying the axes? Overlaying several plots on the same panel? Or something else?

解决方案

Generally discontinuous axes are not recommended within ggplot2, for reasons explored in this question. Hadley Wickham (creator of ggplot2) explains here:

I'm not a big fan of this type of display because I think it is visually distorting. I think it's much more appropriate to show two plots - one of all the data, and one of just the small values. That way you can see how much the large values dominate the smaller ones.

However, it is indeed possible! You have to create a custom axis transformation, which is not for the faint of heart.

Here's an example. Suppose we have data where y is on a log-normal scale.

set.seed(20)
dat <- data.frame(x = c(0, rnorm(50)), y = c(0, exp(rnorm(50, -2, 1.5))))
ggplot(dat, aes(x, y)) + geom_point()

Many points are crowded near the bottom: let's suppose I wanted to put all the values below 1 on a log scale, and the values above 1 on a linear scale. I thus create a custom transformation, called combine_trans, that combines a log scale with a linear scale (that's not exactly what your example plot above does, since it appears to go to 0, but it's probably close enough).

combine_trans <- function(breakpoint, tr1, tr2,
                          br1 = tr1$breaks,
                          br2 = tr2$breaks) {
    # combine two transformations.
    # can also be given functions to determine tick marks
    trans_breakpoint <- tr1$transform(breakpoint)
    trans <- function(x) {
        # choose which transformation to apply
        ifelse(x < breakpoint,
               tr1$transform(x),
               tr2$transform(x - breakpoint) - trans_breakpoint)
    }
    inv <- function(x) {
        # inverse of both transformations
        ifelse(x < trans_breakpoint,
               tr1$inverse(x),
               breakpoint + tr2$inverse(x + trans_breakpoint))
    }
    br <- function(x) {
        # combine break choices from both scales
        br1 <- br1(c(x[x < breakpoint], breakpoint))
        br2 <- br2(c(breakpoint, max(x[x > breakpoint])))
        br <- c(br1, br2)
        br[br > 0]
    }
    # combine domains
    dom <- c(max(tr1$domain[1], tr2$domain[1]), min(tr1$domain[2], tr2$domain[2]))
    trans_new("combined", trans, inv, breaks = br, domain = dom)
}

# combine log10 transformation and identity transformation
combined <- combine_trans(1, log10_trans(), identity_trans())

ggplot(dat, aes(x, y)) +
    geom_point() +
    scale_y_continuous(trans = combined) +
    geom_hline(yintercept = 1, lty = 2)

Notice I manually added a horizontal dashed line with geom_hline, which at least helps draw attention to the discontinuity.

You can see another example of a discontinuous transformation here, which introduces a simple break in the axis.

Note that this code is complicated and may require customization. What's more, it is still somewhat misleading: in general, you should probably just divide your data into separate plots, or put everything on the log scale. Still, it is worth knowing that ggplot2 gives you access to these tools, even if you "shoot yourself in the foot" with them!

这篇关于如何在同一个X轴的同一面板上绘制具有两个不同y轴范围的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆