Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/R/charts.groups.R
1433 views
1
#' Chart weights by group or category
2
#'
3
#' @param object object of class \code{optimize.portfolio}.
4
#' @param \dots passthrough parameters to \code{\link{plot}}.
5
#' @param grouping
6
#' \describe{
7
#' \item{groups: }{group the weights by group constraints.}
8
#' \item{category_labels: }{group the weights by category_labels in the \code{portfolio} object.}
9
#' }
10
#' @param plot.type "line" or "barplot".
11
#' @param main an overall title for the plot: see \code{\link{title}}.
12
#' @param las numeric in \{0,1,2,3\}; the style of axis labels
13
#' \describe{
14
#' \item{0:}{always parallel to the axis,}
15
#' \item{1:}{always horizontal,}
16
#' \item{2:}{always perpendicular to the axis,}
17
#' \item{3:}{always vertical[\emph{default}].}
18
#' }
19
#' @param xlab a title for the x axis: see \code{\link{title}}.
20
#' @param cex.lab the magnification to be used for x and y labels relative to the current setting of \code{cex}.
21
#' @param element.color color for the default border and axis.
22
#' @param cex.axis the magnification to be used for x and y axis relative to the current setting of \code{cex}.
23
#' @author Ross Bennett
24
#' @export
25
chart.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){
26
if(!inherits(object, "optimize.portfolio")) stop("object must be of class 'optimize.portfolio'")
27
28
if(plot.type %in% c("bar", "barplot")){
29
barplotGroupWeights(object=object, ...=..., grouping=grouping, main=main,
30
las=las, xlab=xlab, cex.lab=cex.lab,
31
element.color=element.color, cex.axis=cex.axis)
32
} else if(plot.type == "line"){
33
constraints <- get_constraints(object$portfolio)
34
tmp <- extractGroups(object)
35
grouping <- grouping[1]
36
37
if(grouping == "groups"){
38
weights <- tmp$group_weights
39
if(is.null(weights)) stop("No weights detected for groups")
40
if(any(is.infinite(constraints$cUP)) | any(is.infinite(constraints$cLO))){
41
# set ylim based on weights if box constraints contain Inf or -Inf
42
ylim <- range(weights)
43
} else {
44
# set ylim based on the range of box constraints min and max
45
ylim <- range(c(constraints$cLO, constraints$cUP))
46
}
47
}
48
49
if(grouping == "category"){
50
weights <- tmp$category_weights
51
if(is.null(weights)) stop("No weights detected for category")
52
ylim <- range(weights)
53
}
54
55
columnnames = names(weights)
56
numgroups = length(columnnames)
57
58
if(is.null(xlab))
59
minmargin = 3
60
else
61
minmargin = 5
62
if(main=="") topmargin=1 else topmargin=4
63
if(las > 1) {# set the bottom border to accommodate labels
64
bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab
65
if(bottommargin > 10 ) {
66
bottommargin<-10
67
columnnames<-substr(columnnames,1,19)
68
}
69
}
70
else {
71
bottommargin = minmargin
72
}
73
par(mar = c(bottommargin, 4, topmargin, 2) +.1)
74
75
plot(weights, axes=FALSE, xlab='', ylim=ylim, ylab="Weights", main=main, ...)
76
if(grouping == "groups"){
77
if(!any(is.infinite(constraints$cLO))){
78
points(constraints$cLO, type="b", col="darkgray", lty="solid", lwd=2, pch=24)
79
}
80
if(!any(is.infinite(constraints$cUP))){
81
points(constraints$cUP, type="b", col="darkgray", lty="solid", lwd=2, pch=25)
82
}
83
}
84
axis(2, cex.axis = cex.axis, col = element.color)
85
axis(1, labels=columnnames, at=1:numgroups, las=las, cex.axis = cex.axis, col = element.color)
86
box(col = element.color)
87
}
88
}
89
90
#' barplot of group weights by group or category
91
#'
92
#' This function is called by chart.GroupWeights function if chart.type="barplot"
93
#'
94
#' @param object object of class \code{optimize.portfolio}
95
#' @param ... passthrough parameters to \code{\link{plot}}
96
#' @param grouping
97
#' \describe{
98
#' \item{groups: }{group the weights by group constraints}
99
#' \item{category_labels: }{group the weights by category_labels in portfolio object}
100
#' }
101
#' @param main an overall title for the plot: see \code{\link{title}}
102
#' @param las numeric in \{0,1,2,3\}; the style of axis labels
103
#' \describe{
104
#' \item{0:}{always parallel to the axis [\emph{default}],}
105
#' \item{1:}{always horizontal,}
106
#' \item{2:}{always perpendicular to the axis,}
107
#' \item{3:}{always vertical.}
108
#' }
109
#' @param xlab a title for the x axis: see \code{\link{title}}
110
#' @param cex.lab The magnification to be used for x and y labels relative to the current setting of \code{cex}
111
#' @param element.color color for the default border and axis
112
#' @param cex.axis The magnification to be used for x and y axis relative to the current setting of \code{cex}
113
#' @author Ross Bennett
114
barplotGroupWeights <- function(object, ..., grouping=c("groups", "category"), main="Group Weights", las=3, xlab=NULL, cex.lab=0.8, element.color="darkgray", cex.axis=0.8){
115
if(!inherits(object, "optimize.portfolio")) stop("object must be of class 'optimize.portfolio'")
116
117
constraints <- get_constraints(object$portfolio)
118
tmp <- extractGroups(object)
119
120
if(grouping == "groups"){
121
weights <- tmp$group_weights
122
if(is.null(weights)) stop("No weights detected for groups")
123
}
124
125
if(grouping == "category"){
126
weights <- tmp$category_weights
127
if(is.null(weights)) stop("No weights detected for category")
128
}
129
130
columnnames = names(weights)
131
numgroups = length(columnnames)
132
133
barplot(weights, ylab = "", names.arg=columnnames,
134
border=element.color, cex.axis=cex.axis, main=main, las=las,
135
cex.names=cex.lab, xlab=xlab, ...)
136
box(col=element.color)
137
}
138
139
###############################################################################
140
# R (https://r-project.org/) Numeric Methods for Optimization of Portfolios
141
#
142
# Copyright (c) 2004-2021 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt
143
#
144
# This library is distributed under the terms of the GNU Public License (GPL)
145
# for full details see the file COPYING
146
#
147
# $Id$
148
#
149
###############################################################################
150
151