Graph of a geographical map with symbols according to frequencies

I would like to build a geographic map with n numbers of squares according to the frequency of the disease within the county. Like here: enter image description here

But I could not figure out how to do this with R or qGIS. Thanks for the help.

+4
source share
2 answers

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") } } 

Map using stacked boxes

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 ...

+7
source

As an alternative, I would suggest a bubble chart, which is pretty easy to make using ggplot2 using point geometry. An example of the help page form ( http://had.co.nz/ggplot2/geom_point.html ):

 p <- ggplot(mtcars, aes(wt, mpg)) p + geom_point(aes(size = qsec)) 

that leads to:

ggplot bubble plot

Bubble size is a measured value. The example does not use geographic coordinates, but they can be easily used. The ggplot2 package also supports adding spatial polygons as an extra layer, for example. using geom_path. The coord_map help file shows some examples using map data. See also the fortify function to convert a SpatialPolygons object to data.frame (which is necessary for ggplot2).

+5
source

Source: https://habr.com/ru/post/1384098/


All Articles