Learn R Programming

mosaic (version 0.4-1)

plotFun: Plotting mathematical expressions

Description

Plots mathematical expressions in one and two variables.

Usage

plotFun(object, ..., add = FALSE, xlim = NULL,
    ylim = NULL, npts = NULL, ylab = NULL, xlab = NULL,
    zlab = NULL, filled = TRUE, levels = NULL,
    nlevels = 10, surface = FALSE, groups = NULL,
    col.regions = topo.colors, type = "l", alpha = NULL)

Arguments

object
a mathematical expression (see examples)
add
if TRUE, then overlay an existing plot
xlim
limits for x axis (or use variable names, see examples)
ylim
limits for y axis (or use variable names, see examples)
npts
number of points for plotting.
xlab
label for x axis
ylab
label for y axis
zlab
label for z axis (when in surface-plot mode)
col
color for line graphs and contours
filled
fill with color between the contours (TRUE by default)
levels
levels at which to draw contours
nlevels
number of contours to draw (if levels not specified)
surface
draw a surface plot rather than a contour plot
col.regions
a vector of colors or a function (topo.colors by default) for generating such
type
type of plot ("l" by default)
alpha
number from 0 (transparent) to 1 (opaque) for the fill colors
groups
grouping argument ala lattice graphics
...
additional parameters, typically processed by lattice functions such as xyplot, levelplot or their panel functions. Frequ

Value

  • a trellis object

Details

makes plots of mathematical expressions using the formula syntax. Will draw both line plots and contour/surface plots (for functions of two variables). In RStudio, the surface plot comes with sliders to set orientation. If the colors in filled surface plots are too blocky, increase npts beyond the default of 50, though npts=300 is as much as you're likely to ever need. See examples for overplotting a constraint function on an objective function.

Examples

Run this code
plotFun( a*sin(x^2)~x, xlim=range(-5,5), a=2 )  # setting parameter value
plotFun( u^2 ~ u, ulim=c(-4,4) )                # limits in terms of u
# Note roles of ylim and y.lim in this example
plotFun( y^2 ~ y, ylim=c(-2,20), y.lim=c(-4,4) )
# Combining plot elements to show the solution to an inequality
plotFun( x^2 -3 ~ x, xlim=c(-4,4), grid=TRUE )
ladd( panel.abline(h=0,v=0,col='gray50') )
plotFun( (x^2 -3) * (x^2 > 3) ~ x, type='h', alpha=.1, lwd=4, col='lightblue', add=TRUE )
plotFun( sin(x) ~ x,
   groups=cut(x, findZeros(sin(x) ~ x, within=10)),
   col=c('blue','green'), lty=2, lwd=3, xlim=c(-10,10) )
f <- rfun( ~ u & v )
plotFun( f(u=u,v=v) ~ u & v, u.lim=range(-3,3), v.lim=range(-3,3) )
plotFun( u^2 + v < 3 ~ u & v, add=TRUE, npts=200 )
# display a linear model using a formula interface
model <- lm(wage ~ poly(exper,degree=2), data=CPS)
fit <- makeFun(model)
xyplot(wage ~ exper, data=CPS)
plotFun(fit(exper) ~ exper, add=TRUE)

Run the code above in your browser using DataLab