autoplot.funData | R Documentation |
This function allows to plot funData
objects based on the
ggplot2 package. The function provides a wrapper that rearranges the
data in a funData
object on a one- or two-dimensional domain and
provides a basic ggplot
object, which can be
customized using all functionalities of the ggplot2 package.
autoplot.funData(
object,
obs = seq_len(nObs(object)),
geom = "line",
plotNA = FALSE,
...
)
autolayer.funData(
object,
obs = seq_len(nObs(object)),
geom = "line",
plotNA = FALSE,
...
)
object |
A |
obs |
A vector of numerics giving the observations to plot. Defaults to
all observations in |
geom |
A character string describing the geometric object to use.
Defaults to |
plotNA |
Logical. If |
... |
Further parameters passed to |
If some observations contain missing values (coded via NA
), the
functions can be interpolated using the option plotNA = TRUE
. This
option relies on the na.approx
function in package
zoo
and is currently implemented for one-dimensional
functions only in the function approxNA
.
A ggplot
object that can be customized using
all functionalities of the ggplot2 package.
funData
, ggplot
,
plot.funData
# Install / load package ggplot2 before running the examples
library("ggplot2")
# One-dimensional
argvals <- seq(0,2*pi,0.01)
object <- funData(argvals,
outer(seq(0.75, 1.25, length.out = 11), sin(argvals)))
g <- autoplot(object) # returns ggplot object
g # plot the object
# add the mean function in red
g + autolayer(meanFunction(object), col = 2)
# Two-dimensional
X <- array(0, dim = c(2, length(argvals), length(argvals)))
X[1,,] <- outer(argvals, argvals, function(x,y){sin((x-pi)^2 + (y-pi)^2)})
X[2,,] <- outer(argvals, argvals, function(x,y){sin(2*x*pi) * cos(2*y*pi)})
object2D <- funData(list(argvals, argvals), X)
autoplot(object2D, obs = 1)
autoplot(object2D, obs = 2)
## Not run: autoplot(object2D) # must specify obs!
### More examples ###
par(mfrow = c(1,1))
# using plotNA (needs packages zoo and gridExtra)
objectMissing <- funData(1:5, rbind(c(1, NA, 5, 4, 3), c(10, 9, NA, NA, 6)))
g1 <- autoplot(objectMissing) # the default
g2 <- autoplot(objectMissing, plotNA = TRUE) # requires zoo
gridExtra::grid.arrange(g1 + ggtitle("plotNA = FALSE (default)"),
g2 + ggtitle("plotNA = TRUE")) # requires gridExtra
# Customizing plots (see ggplot2 documentation for more details)
# parameters passed to geom_line are passed via the ... argument
gFancy <- autoplot(object, color = "red", linetype = 2)
gFancy
# new layers can be added directly to the ggplot object
gFancy + theme_bw() # add new layers to the ggplot object
gFancy + ggtitle("Fancy Plot with Title and Axis Legends") +
xlab("The x-Axis") + ylab("The y-Axis")
autoplot(object2D, obs = 1) + ggtitle("Customized 2D plot") + theme_minimal() +
scale_fill_gradient(high = "green", low = "blue", name = "Legend here")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.