Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/R/chart.Weights.R
1433 views
1
2
#' boxplot of the weights of the optimal portfolios
3
#'
4
#' This function charts the optimal weights of a portfolio run via
5
#' \code{\link{optimize.portfolio}} or \code{\link{optimize.portfolio.rebalancing}}.
6
#' The upper and lower bounds on weights can be plotted for single period optimizations.
7
#' The optimal weights will be charted through time for \code{optimize.portfolio.rebalancing}
8
#' objects. For \code{optimize.portfolio.rebalancing} objects, the weights are
9
#' plotted with \code{\link[PerformanceAnalytics]{chart.StackedBar}}.
10
#'
11
#' @param object optimal portfolio object created by \code{\link{optimize.portfolio}}.
12
#' @param neighbors set of 'neighbor' portfolios to overplot. See Details.
13
#' @param \dots any other passthru parameters .
14
#' @param main an overall title for the plot: see \code{\link{title}}
15
#' @param las numeric in \{0,1,2,3\}; the style of axis labels
16
#' \describe{
17
#' \item{0:}{always parallel to the axis,}
18
#' \item{1:}{always horizontal,}
19
#' \item{2:}{always perpendicular to the axis,}
20
#' \item{3:}{always vertical [\emph{default}].}
21
#' }
22
#' @param xlab a title for the x axis: see \code{\link{title}}
23
#' @param cex.lab The magnification to be used for x and y labels relative to the current setting of \code{cex}
24
#' @param element.color provides the color for drawing less-important chart elements, such as the box lines, axis lines, etc.
25
#' @param cex.axis The magnification to be used for axis annotation relative to the current setting of \code{cex}.
26
#' @param colorset color palette or vector of colors to use.
27
#' @param legend.loc location of the legend. If NULL, the legend will not be plotted.
28
#' @param cex.legend The magnification to be used for legend annotation relative to the current setting of \code{cex}.
29
#' @param plot.type "line" or "barplot" to plot.
30
#' @seealso \code{\link{optimize.portfolio}} \code{\link{optimize.portfolio.rebalancing}} \code{\link[PerformanceAnalytics]{chart.StackedBar}}
31
#' @name chart.Weights
32
#' @export
33
chart.Weights <- function(object, ...){
34
UseMethod("chart.Weights")
35
}
36
37
barplotWeights <- 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){
38
weights <- object$weights
39
columnnames <- names(weights)
40
41
if(is.null(xlab))
42
minmargin = 3
43
else
44
minmargin = 5
45
if(main=="") topmargin=1 else topmargin=4
46
if(las > 1) {# set the bottom border to accommodate labels
47
bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab
48
if(bottommargin > 10 ) {
49
bottommargin<-10
50
columnnames<-substr(columnnames,1,19)
51
# par(srt=45) #TODO figure out how to use text() and srt to rotate long labels
52
}
53
}
54
else {
55
bottommargin = minmargin
56
}
57
par(mar = c(bottommargin, 4, topmargin, 2) +.1)
58
59
if(is.null(colorset)) colorset <- 1:length(weights)
60
barplot(height=weights, las=las, main=main, xlab=xlab, ylab="Weights", cex.axis=cex.axis, cex.names=cex.lab, col=colorset, ...)
61
if(!is.null(legend.loc)){
62
legend(legend.loc, legend=names(weights), cex=cex.legend, fill=colorset, bty="n")
63
}
64
box(col=element.color)
65
}
66
67
68
barplotWeights <- 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){
69
weights <- object$weights
70
columnnames <- names(weights)
71
72
if(is.null(xlab))
73
minmargin = 3
74
else
75
minmargin = 5
76
if(main=="") topmargin=1 else topmargin=4
77
if(las > 1) {# set the bottom border to accommodate labels
78
bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab
79
if(bottommargin > 10 ) {
80
bottommargin<-10
81
columnnames<-substr(columnnames,1,19)
82
# par(srt=45) #TODO figure out how to use text() and srt to rotate long labels
83
}
84
}
85
else {
86
bottommargin = minmargin
87
}
88
par(mar = c(bottommargin, 4, topmargin, 2) +.1)
89
90
if(is.null(colorset)) colorset <- 1:length(weights)
91
barplot(height=weights, las=las, main=main, xlab=xlab, ylab="Weights", cex.axis=cex.axis, cex.names=cex.lab, col=colorset, ...)
92
if(!is.null(legend.loc)){
93
legend(legend.loc, legend=names(weights), cex=cex.legend, fill=colorset, bty="n")
94
}
95
box(col=element.color)
96
}
97
98
#' @rdname chart.Weights
99
#' @method chart.Weights optimize.portfolio.rebalancing
100
#' @export
101
chart.Weights.optimize.portfolio.rebalancing <- function(object, ..., main="Weights"){
102
rebal.weights <- extractWeights(object)
103
chart.StackedBar(w=rebal.weights, main=main, ...)
104
}
105
106
107
###############################################################################
108
# R (https://r-project.org/) Numeric Methods for Optimization of Portfolios
109
#
110
# Copyright (c) 2004-2021 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt
111
#
112
# This library is distributed under the terms of the GNU Public License (GPL)
113
# for full details see the file COPYING
114
#
115
# $Id$
116
#
117
###############################################################################
118
119