From d619711839afb6776f003c990e1a77c8fedf1a6a Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Sat, 26 Apr 2014 03:04:12 +0200 Subject: Enable selection of observed variables to plot --- R/plot.mkinfit.R | 25 ++++++++++++++----------- man/plot.mkinfit.Rd | 28 +++++++++++++++++----------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/R/plot.mkinfit.R b/R/plot.mkinfit.R index d4ad6c4..2b85977 100644 --- a/R/plot.mkinfit.R +++ b/R/plot.mkinfit.R @@ -1,10 +1,5 @@ -# $Id: $ - -# Copyright (C) 2010-2013 Johannes Ranke +# Copyright (C) 2010-2014 Johannes Ranke # Contact: jranke@uni-bremen.de -# The summary function is an adapted and extended version of summary.modFit -# from the FME package, v 1.1 by Soetart and Petzoldt, which was in turn -# inspired by summary.nls.lm # This file is part of the R package mkin @@ -23,8 +18,10 @@ if(getRversion() >= '2.15.1') utils::globalVariables(c("type", "variable", "observed")) plot.mkinfit <- function(x, fit = x, + obs_vars = vector(), xlab = "Time", ylab = "Observed", - xlim = range(fit$data$time), ylim = c(0, max(fit$data$observed, na.rm = TRUE)), + xlim = range(fit$data$time), + ylim = c(0, max(subset(fit$data, variable %in% obs_vars)$observed, na.rm = TRUE)), col_obs = 1:length(fit$mkinmod$map), pch_obs = col_obs, lty_obs = rep(1, length(fit$mkinmod$map)), @@ -34,6 +31,12 @@ plot.mkinfit <- function(x, fit = x, solution_type = fit$solution_type parms.all <- c(fit$bparms.optim, fit$bparms.fixed) + obs_vars_all <- names(fit$mkinmod$map) + + if (length(obs_vars) > 0){ + vars <- intersect(obs_vars_all, obs_vars) + } else vars <- obs_vars_all + ininames <- c( rownames(subset(fit$start, type == "state")), rownames(subset(fit$fixed, type == "state"))) @@ -61,13 +64,13 @@ plot.mkinfit <- function(x, fit = x, } # Plot the data and model output names(col_obs) <- names(pch_obs) <- names(lty_obs) <- names(fit$mkinmod$map) - for (obs_var in names(fit$mkinmod$map)) { + for (obs_var in vars) { points(subset(fit$data, variable == obs_var, c(time, observed)), pch = pch_obs[obs_var], col = col_obs[obs_var]) } - matlines(out$time, out[-1], col = col_obs, lty = lty_obs) + matlines(out$time, out[vars], col = col_obs[vars], lty = lty_obs[vars]) if (legend == TRUE) { - legend(lpos, inset= inset, legend=names(fit$mkinmod$map), - col=col_obs, pch=pch_obs, lty=lty_obs) + legend(lpos, inset= inset, legend = vars, + col = col_obs[vars], pch = pch_obs[vars], lty = lty_obs[vars]) } } diff --git a/man/plot.mkinfit.Rd b/man/plot.mkinfit.Rd index ccf2960..41682ad 100644 --- a/man/plot.mkinfit.Rd +++ b/man/plot.mkinfit.Rd @@ -9,7 +9,9 @@ the observed data together with the solution of the fitted model. } \usage{ -\method{plot}{mkinfit}(x, fit = x, xlab = "Time", ylab = "Observed", +\method{plot}{mkinfit}(x, fit = x, + obs_vars = vector(), + xlab = "Time", ylab = "Observed", xlim = range(fit$data$time), ylim = c(0, max(fit$data$observed, na.rm = TRUE)), col_obs = 1:length(fit$mkinmod$map), pch_obs = col_obs, lty_obs = rep(1, length(fit$mkinmod$map)), @@ -18,10 +20,14 @@ \arguments{ \item{x}{ Alias for fit introduced for compatibility with the generic S3 method. -} + } \item{fit}{ an object of class \code{\link{mkinfit}}. -} + } + \item{obs_vars}{ + A character vector of names of the observed variables for which the + data and the model should be plotted. + } \item{xlab}{ label for the x axis. } @@ -36,28 +42,28 @@ } \item{col_obs}{ colors used for plotting the observed data and the corresponding model prediction lines. -} + } \item{pch_obs}{ symbols to be used for plotting the data. -} + } \item{lty_obs}{ line types to be used for the model predictions. -} + } \item{add}{ should the plot be added to an existing plot? -} + } \item{legend}{ should a legend be added to the plot? -} + } \item{lpos}{ position of the legend. Passed to \code{\link{legend}} as the first argument. -} + } \item{inset}{ Passed to \code{\link{legend}} if applicable. -} + } \item{\dots}{ further arguments passed to \code{\link{plot}}. -} + } } \value{ The function is called for its side effect. -- cgit v1.2.1