Plotting Portfolio Weight Changes Over Time in R
Summary
The document answers a practical question about displaying portfolio allocations across rebalancing dates. The example uses a matrix whose rows correspond to annual dates and whose columns represent stocks; the weights are equal in the sample. The desired chart places time on the horizontal axis and asset allocations on the vertical axis, with multiple stocks shown together.
The suggested workflow is to convert the dated matrix into a time-indexed object with R’s zoo package and use its plotting function. The response notes an option for drawing all series on one plot. This is a plotting technique rather than a portfolio construction method: the example does not explain how to calculate weights, evaluate a strategy, or handle irregular dates or changing asset membership. The supplied sample also has constant allocations, so it illustrates the data layout and plotting call more than meaningful weight transitions.
Key ideas
- Portfolio weights can be organized as a time-indexed matrix with assets in columns and rebalancing dates in rows.
- The zoo package can convert dated tabular data into a time series object for plotting.
- A single-plot option can display multiple asset weight series together.
- The example illustrates visualization of weights and does not assess portfolio performance or allocation quality.
Tags
Full text
# Plot Evolution of portfolio weights over time in R
# Plot Evolution of portfolio weights over time in R
Is there any function for plotting the evolution of portfolio weights over time in r?. I have a matrix of portfolio weights from an equal weighting strategy at rebalancing times and want to plot weights of the stocks over time on a single plot.
for example:you want to backtest equal weight strategy where you allocate weights equally at each rebalancing dates for each stock. I have a matrix of weights:
```
A B C D E F
```
2001-12-01 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
2002-12-01 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
2003-12-01 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
2004-12-01 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
2005-12-01 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
2006-12-01 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
2007-12-01 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
2008-12-01 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
2009-12-01 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
2010-12-01 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
2011-12-01 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
I want to plot the stocks(A,B,..) on a single plot where the y axis will be the allocatons and x axis the time ie plotting a transition map for weights over time using R
Regards
## Answer by vonjd (score 1)
https://quant.stackexchange.com/a/12848
You can use the `zoo` package:
```
library(zoo)
W <- matrix(rep(0.03225806,66),nrow=11)
T <- seq(as.Date("2001-12-01"), as.Date("2011-12-01 "), "years")
M <- read.zoo(data.frame(T,W))
colnames(M) <- c(LETTERS[1:6])
plot.zoo(M)
plot.zoo(M,plot.type = "single")
```Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.