Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Mon Oct 18 19:57:15 CEST 2010

```Hello!

The code below works - if you run it you'll see a stacked area chart
generated based on the data example.
I only have one understanding question about the legend location (the
very last snippet of code):

legend(par()\$usr[2],
mean(par()\$usr[3:4]),
rev(order.of.vars),
xpd=T,
bty="n",
pch=15,
col=all.colors[rev(order.of.colors)])

I see that par()\$usr[2] = 14763.72 and mean(par()\$usr[3:4]) = 6.215.
I've read in ?par that usr is "A vector of the form c(x1, x2, y1, y2)
giving the extremes of the user coordinates of the plotting region."
However, I am not sure I understand par()\$usr[subscript] well. Sorry
for a very stupid question: Could someone please confirm that my
interpretation is correct:

Place the left edge of the legend (on x) where the area of the plot
ends on the right (on y).
Place the left edge of the legend (on y) between the bottom and the
top of the current y coordinates of the plot area.

Thanks a lot for confirming!
Dimitri

### Creating a data set with both positives and negatives
my.data<-data.frame(date=c(20080301,20080401,20080501,20080601,20080701,20080801,20080901,20081001,20081101,20081201,20090101,20090201,20090301,20090401,20090501,20090601,20090701,20090801,20090901,20091001,20091101,20091201,20100101,20100201,20100301,20100402,20100503),
x=c(1.1, 1, 1.6, 1, 2, 1.5, 2.1, 1.3, 1.9, 1.1, 1, 1.6, 1, 2, 1.5,
2.1, 1.3, 1.9, 1.1, 1, 1.6, 1, 2, 1.5, 2.1, 1.3, 1.9),
y=c(-4,-3,-6,-5,-7,-5.2,-6,-4,-4.9,-4,-3,-6,-5,-7,-5.2,-6,-4,-4.9,-4,-3,-6,-5,-7,-5.2,-6,-4,-4.9),
z=c(-0.2,-0.3,-0.4,-0.1,-0.2,-0.05,-0.2,-0.15,-0.06,-0.2,-0.3,-0.4,-0.1,-0.2,-0.05,-0.2,-0.15,-0.06,-0.06,-0.2,-0.3,-0.4,-0.1,-0.2,-0.05,-0.2,-0.15),
a=c(10,13,15,15,16,17,15,16,14,10,13,15,15,16,17,15,16,14,10,13,15,15,16,17,15,16,14))
my.data\$date<-as.character(my.data\$date)
my.data\$date<-as.Date(my.data\$date,"%Y%m%d")
(my.data)
str(my.data)

### !!! Enter predictor column numbers here !!!
predictor.indexes<-2:5

positives<-which(colSums(my.data[predictor.indexes])>0) # which vars
have positive column sums?
negatives<-which(colSums(my.data[predictor.indexes])<0) # which vars
have negative column sums?

y.max<-1.1*max(rowSums(my.data[names(positives)])) # the max on the y
axis of the chart
y.min<-1.1*min(rowSums(my.data[names(negatives)])) # the min on the y
axis of the chart
ylim <- c(y.min, y.max)
then second-largest, etc.
order.of.pos.vars<-names(order.positives)
negative, then second-largest, etc.
order.of.neg.vars<-names(order.negatives)
order<-c(order.negatives,order.positives)
order.of.vars<-names(order)   # the order of variables on the chart -
from the bottom up
### so, the bottom-most area should be for z, the second from the
bottom area- for y (above z)

### Creating a palette of 20 colors:
"#99CCFF","#6495ED","#66CDAA","#EEC900","#BC8F8F",
"#FF7F00","#C00000","#9370DB","#473C8B","#696969",
"#8B4500","#800000","#0000CD","#104E8B","#228B22")

### Check them out:
temp<-barplot(1:20,rep(1,20),col=all.colors,horiz=T)

xx <- c(my.data\$date, rev(my.data\$date))
bottom.y.coordinates<-rowSums(my.data[names(negatives)])

par(mar=c(5,4,4,6),xpd=F)
plot(x=my.data\$date, y=bottom.y.coordinates, ylim=ylim, col='white',
type='l', xaxt='n',
ylab='Title for Y', xlab="", main='Chart Title')

for(var in order.of.neg.vars){
top.line.coords<-bottom.y.coordinates-my.data[[var]]
bottom.coords<-c(bottom.y.coordinates,rev(top.line.coords))
polygon(xx,bottom.coords,col=all.colors[which(names(my.data[predictor.indexes])
%in% var)])
bottom.y.coordinates<-top.line.coords
}

for(var in order.of.pos.vars){
top.line.coords<-bottom.y.coordinates+my.data[[var]]
bottom.coords<-c(bottom.y.coordinates,rev(top.line.coords))
polygon(xx,bottom.coords,col=all.colors[which(names(my.data[predictor.indexes])
%in% var)])
bottom.y.coordinates<-top.line.coords
}

axis(1, labels =format(as.Date(my.data\$date, origin="1970-01-01"),
"%Y-%m-%d"), at=my.data\$date, las=2,cex.axis=0.7)
abline(v=my.data\$date,lty="dotted",col = "lightgray")
abline(h=axTicks(2), lty="dotted",col = "lightgray")

order.of.colors<-NULL
for(var in 1:length(order.of.vars)){ # var<-2
order.of.colors[[var]]<-which(names(my.data[predictor.indexes]) %in%
order.of.vars[var])
}
str(order.of.colors)

legend(par()\$usr[2],
mean(par()\$usr[3:4]),
rev(order.of.vars),
xpd=T,
bty="n",
pch=15,
col=all.colors[rev(order.of.colors)])

```