Van Wyk, Jaap
2008-Sep-03 11:50 UTC
[R] Graphics: Improving a Lattice plot - can it be done ?
Hallo all
I realise this might be a "too much to ask" question, but can I
improve the Lattice plot produced by the following code? The type of figure I
would like to produce in each segment of the plot appears below the code.
library(reserving) # http://toolkit.pbwiki.com/RToolkit
<http://toolkit.pbwiki.com/RToolkit>
data(GenIns)
GenInsInc <- incrementalTriangle(GenIns)
basicCL <- ChainLadder(GenIns, quarterly=FALSE, usetail=FALSE,
YOA=c(1997:2006))
# lattice plot by underwriting years
plot(basicCL, byYOA=TRUE)
Instead of the ordinary "confidence bands" produced by the re lines, I
would like to display a type of "funnel", as illustrated in the code
below:
funnelGraph <- function(x, ylim, bands, ...)
{
#----------------------------------------------------------------------
# Function to plot a funnel graph
# Requires package: RColorBrewer
#
# Created by Nigel De Silva, 3 June 2006
# nigel.desilva@thomasmiller.com <mailto:nigel.desilva@thomasmiller.com>
#----------------------------------------------------------------------
require(RColorBrewer)
# Determine confidence levels for graph
if(missing(bands)) bands <- 40
ci <- seq(from=0.50, to=0.995, length=bands)
q <- c(0.5-ci/2, 0.5+ci/2)
# Calculate points to plot
central <- apply(x, 2, mean)
areas <- apply(x, 2, quantile, q)
time <- 1:dim(x)[2]
# Set up plot
if(missing(ylim))
ylim = range(pretty(range(areas)))
plot(time, central, ylim=ylim, type="n", ...)
# Draw areas
mypalette <- c(rev(brewer.pal(9, "Blues")), "White") #9
is max no of colours
mypalette <- colorRampPalette(mypalette, space="Lab", bias=0.25)
mypalette <- mypalette(bands)
for(i in rev(1:length(ci))){
x <- c(time, rev(time))
y <- c(areas[i, ], rev(areas[i + length(ci), ]))
polygon(x, y, col=mypalette[i], border=NA)
}
# Draw mean path
lines(time, central, lwd=2, col=2)
return(time)
}
library(MASS)
assets <- mvrnorm(10000, rep(0.05, 5), diag(rep(0.08, 5)))
assets <- exp(cbind(0, assets))
assets <- t(apply(assets, 1, cumprod))
funnelGraph(assets, xlab="Year", main="Projected Assets")
Thanks for any help!
Regards
Jacob
Jacob L van Wyk
Dept of Statistics
University of Johannesburg
P O Box 524
Auckland Park 2006
South Africa
Tel: +27-11-559-3080
Fax: +27-11-559-2499
Cell: +27-82-859-2031
[[alternative HTML version deleted]]
