Break x-axis in plot

匿名 (未验证) 提交于 2019-12-03 08:30:34

问题:

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.

Here's some example data:

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

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

x.cdf <- ecdf(x) 

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) 

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.

Any help would be very much appreciated!

回答1:

"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() 



文章来源: Break x-axis in plot
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!