1#' boxplot of the weights of the optimal portfolios2#'3#' This function charts the optimal weights of a portfolio run via4#' \code{\link{optimize.portfolio}} or \code{\link{optimize.portfolio.rebalancing}}.5#' The upper and lower bounds on weights can be plotted for single period optimizations.6#' The optimal weights will be charted through time for \code{optimize.portfolio.rebalancing}7#' objects. For \code{optimize.portfolio.rebalancing} objects, the weights are8#' plotted with \code{\link[PerformanceAnalytics]{chart.StackedBar}}.9#'10#' @param object optimal portfolio object created by \code{\link{optimize.portfolio}}.11#' @param neighbors set of 'neighbor' portfolios to overplot. See Details.12#' @param \dots any other passthru parameters .13#' @param main an overall title for the plot: see \code{\link{title}}14#' @param las numeric in \{0,1,2,3\}; the style of axis labels15#' \describe{16#' \item{0:}{always parallel to the axis,}17#' \item{1:}{always horizontal,}18#' \item{2:}{always perpendicular to the axis,}19#' \item{3:}{always vertical [\emph{default}].}20#' }21#' @param xlab a title for the x axis: see \code{\link{title}}22#' @param cex.lab The magnification to be used for x and y labels relative to the current setting of \code{cex}23#' @param element.color provides the color for drawing less-important chart elements, such as the box lines, axis lines, etc.24#' @param cex.axis The magnification to be used for axis annotation relative to the current setting of \code{cex}.25#' @param colorset color palette or vector of colors to use.26#' @param legend.loc location of the legend. If NULL, the legend will not be plotted.27#' @param cex.legend The magnification to be used for legend annotation relative to the current setting of \code{cex}.28#' @param plot.type "line" or "barplot" to plot.29#' @seealso \code{\link{optimize.portfolio}} \code{\link{optimize.portfolio.rebalancing}} \code{\link[PerformanceAnalytics]{chart.StackedBar}}30#' @name chart.Weights31#' @export32chart.Weights <- function(object, ...){33UseMethod("chart.Weights")34}3536barplotWeights <- function(object, ..., main="Weights", las=3, xlab=NULL, cex.lab=1, element.color="darkgray", cex.axis=0.8, legend.loc="topright", cex.legend=0.8, colorset=NULL){37weights <- object$weights38columnnames <- names(weights)3940if(is.null(xlab))41minmargin = 342else43minmargin = 544if(main=="") topmargin=1 else topmargin=445if(las > 1) {# set the bottom border to accommodate labels46bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab47if(bottommargin > 10 ) {48bottommargin<-1049columnnames<-substr(columnnames,1,19)50# par(srt=45) #TODO figure out how to use text() and srt to rotate long labels51}52}53else {54bottommargin = minmargin55}56par(mar = c(bottommargin, 4, topmargin, 2) +.1)5758if(is.null(colorset)) colorset <- 1:length(weights)59barplot(height=weights, las=las, main=main, xlab=xlab, ylab="Weights", cex.axis=cex.axis, cex.names=cex.lab, col=colorset, ...)60if(!is.null(legend.loc)){61legend(legend.loc, legend=names(weights), cex=cex.legend, fill=colorset, bty="n")62}63box(col=element.color)64}656667barplotWeights <- function(object, ..., main="Weights", las=3, xlab=NULL, cex.lab=1, element.color="darkgray", cex.axis=0.8, legend.loc="topright", cex.legend=0.8, colorset=NULL){68weights <- object$weights69columnnames <- names(weights)7071if(is.null(xlab))72minmargin = 373else74minmargin = 575if(main=="") topmargin=1 else topmargin=476if(las > 1) {# set the bottom border to accommodate labels77bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab78if(bottommargin > 10 ) {79bottommargin<-1080columnnames<-substr(columnnames,1,19)81# par(srt=45) #TODO figure out how to use text() and srt to rotate long labels82}83}84else {85bottommargin = minmargin86}87par(mar = c(bottommargin, 4, topmargin, 2) +.1)8889if(is.null(colorset)) colorset <- 1:length(weights)90barplot(height=weights, las=las, main=main, xlab=xlab, ylab="Weights", cex.axis=cex.axis, cex.names=cex.lab, col=colorset, ...)91if(!is.null(legend.loc)){92legend(legend.loc, legend=names(weights), cex=cex.legend, fill=colorset, bty="n")93}94box(col=element.color)95}9697#' @rdname chart.Weights98#' @method chart.Weights optimize.portfolio.rebalancing99#' @export100chart.Weights.optimize.portfolio.rebalancing <- function(object, ..., main="Weights"){101rebal.weights <- extractWeights(object)102chart.StackedBar(w=rebal.weights, main=main, ...)103}104105106###############################################################################107# R (https://r-project.org/) Numeric Methods for Optimization of Portfolios108#109# Copyright (c) 2004-2021 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt110#111# This library is distributed under the terms of the GNU Public License (GPL)112# for full details see the file COPYING113#114# $Id$115#116###############################################################################117118119