Articles
Fplot Limit
How to control the limits of data values in R plots.
X Limits Y Limits Z Limits Adjust Axis Box Grid Grid Grid Background X Ticks Y Ticks Z Ticks X Tick Labels Y Tick Labels X Tick Format Y Tick Format Z Tick Format X Tick Angle Y Tick Angle Multiplot Multiplot Hold YY-axis Color Order Subplots. Fplotplots a function between specified limits. The function must be of the form y = f(x), where x is a vector whose range specifies the limits, and yis a vector the same size as xand contains the function's value at the points in x (see the first example).
R has multiple graphics engines. Here we will talk about the base graphics and the ggplot2
package.
We’ll create a bit of data to use in the examples:
ggplot2
demands that you have a data frame:
Seriously exciting data, yes?
Default behavior
The default is — not surprisingly — to create limits so that the data comfortably fit.
Base
Figure 1 shows the default limits. The command to do this is (almost):
The actual commands to create this figure and others are below in Appendix R.
Figure 1: Default base graphics plot.
ggplot2
The default here is basically the same, though the resulting picture looks rather different. Figure 2 is created with the commands:
The require
command makes sure that the package is loaded into the current R session. In this setting graphs are objects, and they are rendered only when they are printed — hence the call to print
.
Figure 2: Default ggplot2
plot.
Python 2d Plot Limit Cycle
Typical limit
If the default isn’t what you want, you can change it. Here we want — for some reason — more room on the left of the plot.
Base
The simplified version of the command for Figure 3 is:
Figure 3: Typical use of the xlim
graphics parameter.
The examples here are on the x-axis. To control the y-axis, just substitute “y” for “x” — ylim
rather than xlim
.
ggplot2
In ggplot2
modifications or additions to a plot object are usually done by adding new terms:
Figure 4: Typical ggplot2
specification of x limits.
Reverse direction
The first limit need not be the smallest — if not, then the axis is reversed.
Base
Figure 5: Base graph with the x-axis reversed.
ggplot2
The command for Figure 6 is:
Figure 6: ggplot2
with the x-axis reversed.
Expansion
The sharp-eyed will have noticed that the actual limits in the plots above are not what is specified. The specifications are strictly inside the plots. This makes it easy to make sure that no data is plotted on the boundary of the plot.
It is possible to change this behavior as well.
Base
We can see what the real range is by looking at the usr
graphics parameter:
You can set some graphics parameters, and some are read-only. The usr
parameter can be set, but you almost always want to just use it as is. The first two elements of usr
are the x-axis limits, the last two are the y-axis limits.
The x-axis was asked to have limits 10 apart, and we can see that there is an extra 0.4 on each side.
You can force the limits to be taken literally by specifying xaxs
(or yaxs
for the y-axis):
The 'i'
stands for “internal”, the other (default) choice is 'r'
as in “regular”.
ggplot2
Figure 7 is produced with:
Figure 7: ggplot2
with no x-axis expansion.
I wasn’t able to both modify the expansion and xlim
— I’m not sure if that is a bug or solely due to my ignorance of ggplot2
.
Appendix R
The actual commands that created the base graphics are:
Matlab Plot Limit Axis
Figure 1:
Figure 3:
Figure 5:
Plot a function fn within the range defined by limits.
fn is a function handle, inline function, or string containing thename of the function to evaluate.
The limits of the plot are of the form [xlo, xhi]
or[xlo, xhi, ylo, yhi]
.
The next three arguments are all optional and any number of them may begiven in any order.
tol is the relative tolerance to use for the plot and defaultsto 2e-3 (.2%).
n is the minimum number of points to use. When n is specified,the maximum stepsize will be xhi - xlo / n
. Morethan n points may still be used in order to meet the relativetolerance requirement.
The fmt argument specifies the linestyle to be used by the plotcommand.
Matlab Plot Limits
If the first argument hax is an axes handle, then plot into this axis,rather than the current axes returned by gca
.
With no output arguments the results are immediately plotted. With twooutput arguments the 2-D plot data is returned. The data can subsequentlybe plotted manually with plot (x, y)
.
Example:
Programming Notes:
Stem And Leaf Plot Limitations
fplot
works best with continuous functions. Functions withdiscontinuities are unlikely to plot well. This restriction may be removedin the future.
fplot
requires that the function accept and return a vector argument.Consider this when writing user-defined functions and use .*
,./
, etc. See the function vectorize
for potentiallyconverting inline or anonymous functions to vectorized versions.
How To Plot Limits On Mathematica
See also: ezplot, plot, vectorize.