#' Chart weights by group or category1#'2#' @param object object of class \code{optimize.portfolio}.3#' @param \dots passthrough parameters to \code{\link{plot}}.4#' @param grouping5#' \describe{6#' \item{groups: }{group the weights by group constraints.}7#' \item{category_labels: }{group the weights by category_labels in the \code{portfolio} object.}8#' }9#' @param plot.type "line" or "barplot".10#' @param main an overall title for the plot: see \code{\link{title}}.11#' @param las numeric in \{0,1,2,3\}; the style of axis labels12#' \describe{13#' \item{0:}{always parallel to the axis,}14#' \item{1:}{always horizontal,}15#' \item{2:}{always perpendicular to the axis,}16#' \item{3:}{always vertical[\emph{default}].}17#' }18#' @param xlab a title for the x axis: see \code{\link{title}}.19#' @param cex.lab the magnification to be used for x and y labels relative to the current setting of \code{cex}.20#' @param element.color color for the default border and axis.21#' @param cex.axis the magnification to be used for x and y axis relative to the current setting of \code{cex}.22#' @author Ross Bennett23#' @export24chart.GroupWeights <- function(object, ..., grouping=c("groups", "category"), plot.type="line", main="Group Weights", las=3, xlab=NULL, cex.lab=0.8, element.color="darkgray", cex.axis=0.8){25if(!inherits(object, "optimize.portfolio")) stop("object must be of class 'optimize.portfolio'")2627if(plot.type %in% c("bar", "barplot")){28barplotGroupWeights(object=object, ...=..., grouping=grouping, main=main,29las=las, xlab=xlab, cex.lab=cex.lab,30element.color=element.color, cex.axis=cex.axis)31} else if(plot.type == "line"){32constraints <- get_constraints(object$portfolio)33tmp <- extractGroups(object)34grouping <- grouping[1]3536if(grouping == "groups"){37weights <- tmp$group_weights38if(is.null(weights)) stop("No weights detected for groups")39if(any(is.infinite(constraints$cUP)) | any(is.infinite(constraints$cLO))){40# set ylim based on weights if box constraints contain Inf or -Inf41ylim <- range(weights)42} else {43# set ylim based on the range of box constraints min and max44ylim <- range(c(constraints$cLO, constraints$cUP))45}46}4748if(grouping == "category"){49weights <- tmp$category_weights50if(is.null(weights)) stop("No weights detected for category")51ylim <- range(weights)52}5354columnnames = names(weights)55numgroups = length(columnnames)5657if(is.null(xlab))58minmargin = 359else60minmargin = 561if(main=="") topmargin=1 else topmargin=462if(las > 1) {# set the bottom border to accommodate labels63bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab64if(bottommargin > 10 ) {65bottommargin<-1066columnnames<-substr(columnnames,1,19)67}68}69else {70bottommargin = minmargin71}72par(mar = c(bottommargin, 4, topmargin, 2) +.1)7374plot(weights, axes=FALSE, xlab='', ylim=ylim, ylab="Weights", main=main, ...)75if(grouping == "groups"){76if(!any(is.infinite(constraints$cLO))){77points(constraints$cLO, type="b", col="darkgray", lty="solid", lwd=2, pch=24)78}79if(!any(is.infinite(constraints$cUP))){80points(constraints$cUP, type="b", col="darkgray", lty="solid", lwd=2, pch=25)81}82}83axis(2, cex.axis = cex.axis, col = element.color)84axis(1, labels=columnnames, at=1:numgroups, las=las, cex.axis = cex.axis, col = element.color)85box(col = element.color)86}87}8889#' barplot of group weights by group or category90#'91#' This function is called by chart.GroupWeights function if chart.type="barplot"92#'93#' @param object object of class \code{optimize.portfolio}94#' @param ... passthrough parameters to \code{\link{plot}}95#' @param grouping96#' \describe{97#' \item{groups: }{group the weights by group constraints}98#' \item{category_labels: }{group the weights by category_labels in portfolio object}99#' }100#' @param main an overall title for the plot: see \code{\link{title}}101#' @param las numeric in \{0,1,2,3\}; the style of axis labels102#' \describe{103#' \item{0:}{always parallel to the axis [\emph{default}],}104#' \item{1:}{always horizontal,}105#' \item{2:}{always perpendicular to the axis,}106#' \item{3:}{always vertical.}107#' }108#' @param xlab a title for the x axis: see \code{\link{title}}109#' @param cex.lab The magnification to be used for x and y labels relative to the current setting of \code{cex}110#' @param element.color color for the default border and axis111#' @param cex.axis The magnification to be used for x and y axis relative to the current setting of \code{cex}112#' @author Ross Bennett113barplotGroupWeights <- function(object, ..., grouping=c("groups", "category"), main="Group Weights", las=3, xlab=NULL, cex.lab=0.8, element.color="darkgray", cex.axis=0.8){114if(!inherits(object, "optimize.portfolio")) stop("object must be of class 'optimize.portfolio'")115116constraints <- get_constraints(object$portfolio)117tmp <- extractGroups(object)118119if(grouping == "groups"){120weights <- tmp$group_weights121if(is.null(weights)) stop("No weights detected for groups")122}123124if(grouping == "category"){125weights <- tmp$category_weights126if(is.null(weights)) stop("No weights detected for category")127}128129columnnames = names(weights)130numgroups = length(columnnames)131132barplot(weights, ylab = "", names.arg=columnnames,133border=element.color, cex.axis=cex.axis, main=main, las=las,134cex.names=cex.lab, xlab=xlab, ...)135box(col=element.color)136}137138###############################################################################139# R (https://r-project.org/) Numeric Methods for Optimization of Portfolios140#141# Copyright (c) 2004-2021 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt142#143# This library is distributed under the terms of the GNU Public License (GPL)144# for full details see the file COPYING145#146# $Id$147#148###############################################################################149150151