[R] Viewport help

Glenn Schultz glennmschultz at me.com
Mon May 25 03:27:53 CEST 2015


Hello All,

I have a function which outputs graphically the results of a pass-through OAS analysis.  The viewport is 2x2.  The idea is to leave a small margin at the top and enter a title with something like the following. 

Bond Lab Pass Through OAS 
Bond Id: foo at bond.id
OAS : foo at OAS

I am a little lost on the viewport and can't quite get to the last step.  The function is below and the code I have question about is in red.

Glenn

The function is below - The code I have a question about is in red
#' OAS Analaysis of Pass Though MBS
#' 
#' Function calls BondLab PassThroughOAS and plots the results
#' copyright Bond Lab Technologies, Inc 2015
#' @importFrom BondLab PassThroughOAS
#' @param bond.id a character string the bond id
#' @param trade.date a character string the trade date
#' @param settlement.date a character string the settlement date
#' @param original.balance a numeric value the original balance
#' @param price a numeric value the price
#' @param sigma a numeric the **annualized** volatility.  The volatilty assumption assumes
#' a trading year of 252 days
#' @export
PassThrough.OAS <- function(bond.id = character,
                            trade.date = character,
                            settlement.date = character,
                            original.balance = numeric(),
                            price = numeric(),
                            sigma = numeric(),
                            paths = numeric()){
  set.seed = 100
  sigma = sigma/sqrt(trading.days)
  OAS.Analysis <- PassThroughOAS(bond.id = bond.id, 
                                 trade.date = trade.date, 
                                 settlement.date = settlement.date, 
                                 original.bal = original.balance,
                                 price = price,  
                                 sigma = sigma, 
                                 paths = paths, 
                                 PrepaymentAssumption = "MODEL")
  
  # price distribution
  OAS.Price <- data.frame(OAS.Analysis at PriceDist)
  OAS.Price <- data.frame(cbind(OAS.Price, seq(1: length(OAS.Price))))
  colnames(OAS.Price) <- c("value", "count")

  
  price.dist <- ggplot(OAS.Price, aes(x = value )) +
    geom_density(fill = "#56B4E9", colour = "#56B4E9", alpha = .6) +
    geom_histogram(aes(y =..density..), color = "lightgrey", fill = "#0072B2", bindwidth = .01) +
    theme_minimal() +
    #scale_x_continuous(breaks = seq(80,120, 5)) +
    labs(title = "Price Distribution") +
    ylab("Density")+
    xlab("Path Price") +
    theme(panel.grid.major = element_line(size = .25, color = "grey")) +
    theme(axis.text = element_text(size = 15)) +
    theme(axis.title = element_text(size = 20)) + 
    theme(legend.position = "none")
  
  # modified duration distribution
  OAS.Mdur <- data.frame(OAS.Analysis at PathModDur)
  OAS.Mdur <- data.frame(cbind(OAS.Mdur, seq(1:length(OAS.Mdur))))
  colnames(OAS.Mdur) <- c("value", "count")

  
  Mdur.dist <- ggplot(OAS.Mdur, aes(x = value )) +
    geom_density(fill = "#56B4E9", colour = "#56B4E9", alpha = .6) +
    geom_histogram(aes(y =..density..), color = "lightgrey", fill = "#0072B2", bindwidth = .01) +
    theme_minimal() +
    #scale_x_continuous(breaks = seq(80,120, 5)) +
    labs(title = "Mod. Duration Distribution") +
    ylab("Density")+
    xlab("Path Mod. Duration") +
    theme(panel.grid.major = element_line(size = .25, color = "grey")) +
    theme(axis.text = element_text(size = 15)) +
    theme(axis.title = element_text(size = 20)) + 
    theme(legend.position = "none")
  
    layout <- grid.layout(nrow = 2, ncol = 2,
                          widths = unit(c(2,2), c("null", "null")),
                          heights = unit(c(.3, 2), c("null", "null")))
  
    vplayout <- function(...){
    grid.newpage()
    pushViewport(viewport(layout = layout))}
  
    subplot <- function(x, y) {viewport(layout.pos.row = x,
                                      layout.pos.col = y)}
    mmplot <- function(a, b) {
    vplayout()
    print(a, vp = subplot(2, 1))
    print(b, vp = subplot(2, 2))
  }
  
  OAS <- mmplot(price.dist, Mdur.dist)
  plot(OAS)
  
} 


More information about the R-help mailing list