First write a small function called 'stackbox' that displays (using "rect") the squares for the stack in the right place.
Here is the first line of this function:
stackbox <- function(x,y,n,size,maxheight=5){
- where "size" is the height and width of the margins, and "maxheight" allows you to have a stack height of 5 or 10 or more.
Then call this function for each county that has cases.
Where exactly are you stuck in the process?
Here's the full function:
stackbox <- function(x,y,n,size,maxheight=5,...){ stackheight = seq(0,n,by=maxheight) stackheight=diff(unique(c(stackheight,n))) for(col in 1:length(stackheight)){ xl=rep(x+(col-1)*size,stackheight[col]) - (length(stackheight)/2)*size yb=y+size*((1:stackheight[col])-1) - (max(stackheight)/2)*size xr=xl+size yt=yb+size rect(xl,yb,xr,yt,...) } }
Example:
plot(1:10) for(i in 1:10){ stackbox(i,i,i,3,size=.1,col="red",border="white") }
To do this on a map, you need sp and maptools packages, as well as a shapefile or other source of geospatial data that contains your data:
africa=readShapeSpatial(file.path(mapLib,"africa.shp")) plot(africa,border="gray") coords=coordinates(africa) for(i in 1:nrow(africa)){ if(cases[i]>0){ stackbox(coords[i,1],coords[i,2],africa$cases[i],1,border="#606060",col="#0083FE") } }

I chose colors that are a bit like your original. Please note that the fields are in the coordinates of the graph, so I had to make them 0.1 degrees. You might want to convert your map to a Euclidean projection (using spTransform from the package: gdal), and then they will be in these units and correctly square.
Making nice text labels like your original would be tricky though ...