在绘图中断开 x 轴 [英] Break x-axis in plot

查看:85
本文介绍了在绘图中断开 x 轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打破我使用函数 plot.stepfun 的累积分布函数图的 x 轴,但似乎无法弄清楚如何.

I want to break the x-axis of a plot of a cumulative distribution function for which I use the function plot.stepfun, but don't seem to be able to figure out how.

以下是一些示例数据:

set.seed(1)
x <- sample(seq(1,20,0.01),300,replace=TRUE)

然后我用函数ecdf得到x的经验累积分布函数:

Then I use the function ecdf to get the empirical cumulative distribution function of x:

x.cdf <- ecdf(x)

并且我将 x.cdf 的类更改为 stepfun,因为我更喜欢使用 plot.ecdf 直接调用 plot.stepfun(也使用 plot.stepfun,但自定义绘图的可能性较少).

And I change the class of x.cdf to stepfun, because I prefer to call plot.stepfun directly over using plot.ecdf (which also uses plot.stepfun, but has fewer possibilities to customize the plot).

class(x.cdf) <- "stepfun"

然后我可以创建一个情节如下:

Then I am able to create a plot as follows:

 plot(x.cdf, do.point=FALSE)

但现在我想在 12 和 20 之间分解 x 轴,例如使用 axis.break [plotrix-library] 例如 在这里,但由于我没有用于绘图的普通 x 和 y 参数,我不知道该怎么做.

But now I want to break up the x-axis between 12 and 20, e.g. using axis.break [plotrix-library] such as here, but since I have no ordinary x and y-argument for plotting, I don't know how to do this.

非常感谢任何帮助!

推荐答案

打破 12 和 20 之间的轴"对我来说没有多大意义,因为 20 是 x 范围的结束,所以我会举例说明在 12 和 15 之间打破它. plotrix.axis.break 函数实际上并没有做太多事情(如果您逐步完成该示例就可以看到.)它所做的只是在特定位置放置几个斜杠,断点".所有其余的工作都需要使用常规绘图函数完成,而 plot.stepfun 并没有真正设置为这样做,所以我使用带有 type="s" 参数的常规 plot.default.您需要对 x 值、ecdf 函数的参数和轴参数中的标签进行偏移.

"Breaking the axis between 12 and 20" doesn't make a lot of sense to me since 20 is the end of the x range, so I will exemplify breaking it between 12 and 15. The plotrix.axis.break function doesn't actually do very much (as can be seen if you step through that example.) All it does is put a couple of slashes at a particular location, the "breakpos". All the rest of the work needs to be done with regular plotting functions and plot.stepfun isn't really set up to do it, so I'm using regular plot.default with the type="s" argument. You need to do the offsetting of the x values, the arguments to the ecdf function and the labels in the axis arguments.

png()
 plot( c(seq(1,12,0.1), seq(15,20,0.1)-3),  # Supply the range, shifted
         x.cdf(c(seq(1,12,0.1), seq(15,20,0.1))),  # calc domain values, not shifted
         type="s",  xaxt="n", xlab="X", ylab="Quantile")
 axis(1, at=c( 1:12, (16:20)-3), labels=c(1:12, (16:20)) ) #shift x's, labels unshifted
 axis.break(breakpos=12)
dev.off()

这篇关于在绘图中断开 x 轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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