R语言plot绘制stepfun相关疑问:参数、plotout$t/y及补点方法
Great question—let’s unpack all these points clearly, since working with step functions in R can feel a bit counterintuitive at first!
Q1: What do t and y represent when plotting functions with plot(), and what parameters are available?
When you use plot() on a function object (like the output of stepfun()), R uses a specialized method called plot.stepfun for step functions. Here’s what the returned t and y mean:
plotout$t: These are the x-axis coordinates where the step function jumps to a new value, plus small extended endpoints to make the full staircase plot look clean. For your EDF example, this includes your sorted sample values, plus tiny ranges beyond the min and max of your data.plotout$y: These are the function values corresponding to each interval defined by thetpoints. For an EDF, this is the cumulative probability (0, 0.25, 0.5, 0.75, 1 for your 4 samples).
For parameters, you can use standard plot options like main, xlab, ylab, col, lwd, and pch. For step-function-specific controls, check ?plot.stepfun for these key parameters:
verticals: Logical, toggles vertical lines at jump points (defaultTRUEfor stepfun plots).do.points: Logical, controls whether points are drawn at jump locations (defaultTRUE).xval: Custom x-values to evaluate the step function at (great for finer control over plot resolution).- Note:
type='l'doesn’t work here—plot.stepfuninherently draws a staircase shape, so useverticalsto adjust the plot’s look instead.
Q2: Fixing the missing point in the quantile function plot, and clarifying plot.stepfun behavior
Why the first point is missing
Your quantile function uses stepfun((1:3)/4, sort(test)). The stepfun() tool creates a function that stays constant between its input jump points. Here, your jump points are 0.25, 0.5, 0.75, so the function returns sort(test)[1] for all probabilities p < 0.25—but plot.stepfun only draws within the range of the jump points by default, so the p=0 (minimum value) point isn’t included.
How to fix it
You have two simple options:
- Extend the stepfun to include
p=0:
Add0as a jump point to explicitly cover the full range of probabilities from 0 to 1:x <- c(-1.55, -0.67, -0.39, 0.60) test = sort(x) # Empirical quantile function with p=0 included plotout = plot(stepfun(c(0, (1:3)/4), c(min(test), test)), main="Empirical quantile function") # Keep your existing red segments segments(0.4, 1.04*plotout$y[1], 0.4, plotout$y[2], col="red") segments(-0.04, plotout$y[2], 0.4, plotout$y[2], col="red") - Manually add the missing point:
If you don’t want to adjust thestepfun()call, just add a point at(0, min(test))after plotting:plotout = plot(stepfun((1:3)/4, sort(test)), main="Empirical quantile function") points(0, min(test), pch=19) # Adds the p=0 point # Your existing segments code here
What plotout$t and plotout$y mean for the quantile function
For the quantile plot:
plotout$t: These are the probability values (the x-axis of your quantile plot) including the jump points (plus extended endpoints). If you used the first fix, this will include0, 0.25, 0.5, 0.75.plotout$y: These are the corresponding quantile values (your sorted sample points) for each interval of probabilities.
内容的提问来源于stack exchange,提问作者Lizzy

