Friday 27 July 2012

Using R to create a Nine Decile Plot





litig <- read.csv("C://temp/RandomInsurerData.csv")

insurers <- as.data.frame(table(litig$Insurer))
over20 <- as.character(subset(insurers,Freq>20)$Var1)

count <- length(over20)
shapes = c(15,16,17)
colours <- rainbow(count)

deciles = seq(0.1,0.9,0.1)
xmarks <- seq(1:10)
ylim = c(0,100) 

png("C:/temp/InsurerDeciles.png")

plot(quantile(seq(0,100),probs=deciles ),ylim=ylim,type="n", 
 xlab="Deciles",ylab="Amount",main="Decile Amounts by Insurer"  )
axis(side=1,at=xmarks)
labels <- c() 

for (i in 1:count) {
 xx <- subset(litig,Insurer==over20[i])$Claim
 labels <- rbind(labels,paste(over20[i], ":", 
  formatC(length(xx),format="d",big.mark=","), "items"))
 points(quantile(xx,deciles),col=colours[i],pch=shapes[i%%length(shapes)])
 lines(quantile(xx,probs=deciles),col=colours[i],lty=1)
}
legend(1,ylim[2],labels,col=colours,pch=shapes,lty=1)

dev.off()