Plot a geographical map with symbols according to frequencies

I would like to plot a geographical map with n numbers of squares according to the frequency of a disease within a county. Like this one here:

But I couldn't figure out how to do this with R or qGIS. Thanks for y'all help.

Answers


First write a little function called 'stackbox' that plots (using "rect") the squares for the stack in the right place.

Here's the first line of that function:

stackbox <- function(x,y,n,size,maxheight=5){
  • where 'size' is the height and width of the boxes, and 'maxheight' lets you have a stack that is 5 high or 10 high or whatever.

Then call that function for every county that has cases.

Where exactly were you stuck in the process?

Here's the function in full:

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 the sp and maptools packages, and a shapefile or other geospatial data source that has your data in it:

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've picked colours that look a bit like your original. Note that the boxes are in plot coordinates, so I had to make them 0.1 of a degree. You may wish to transform your map to a euclidean projection (using spTransform from package:gdal) and then they'll be in those units and properly square.

Doing nice text labels like in your original would be tricky though...


Need Your Help

How to release memory used by IHTMLDocument?

delphi winapi memory com twebbrowser

is there a way to release memory after using IHTMLDocument (IHTMLDocument2) ?

Changing an existing template's background color in Ember

css templates ember.js handlebars.js

I have a lot of templates in Emberjs, one of them is a login template. What I want to do is change the background color of just that one template.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.