I’ve been doing some spatial stuff of late and the next little step will involve intersecting points with possibly many overlapping polygons. The sp package has a function called over which returns the polygons that points intersects with. The catch though, is that it only returns the last (highest numerical value) polygon a point overlaps with. So it’s not so useful if you have many overlapping polygons. A little playing, and I’ve overcome that problem… Here’s a toy example. Create a couple of polygons and put them into a SpatialPolygons object. library(sp) p1 <- matrix(c(1,1, 2,1, 4,2, 3,2), ncol = 2, byrow = TRUE) p2 <- matrix(c(2.2,1, 3,1, 3,2, 3,3, 2.8,3), ncol = 2, byrow = TRUE) p1s <- Polygons(list(Polygon(p1)), 3) p2s <- Polygons(list(Polygon(p2)), 4) sps <- SpatialPolygons(list(p1s, p2s)) Define a few points and put them in a SpatialPoints object…

Original Post: Intersecting points and overlapping polygons

# Posts by aghaynes

## Tips for great graphics

R is a great program for generating top-notch graphics. But to get the best out of it, you need to put in a little more work. Here are a few tips for adapting your R graphics to make them look a little better. 1) Dont use the “File/Save as…/” menu. If you set up your graphic in the first place then theres no need to post-process (eg crop, scale etc) the graphic in other software. Use the graphic devices (jpeg(), tiff(), postscript(), etc), set your height and width to whatever you want the finished product to be and then create the graph. tiff(“~/Manuscript/Figs/Fig1.tiff”, width =2, height =2, units =”in”, res = 600) plot(dist ~ speed, cars) # cars is a base R dataset – data(cars) dev.off() The first argument to a graphic device such as tiff or jpeg is the…

Original Post: Tips for great graphics

## openrouteservice – geodata!

The openrouteservice provides a new method to get geodata into R. It has an API (or a set of them) and an R package has been written to communicate with said API(s) and is available from GitHub. I’ve just been playing around with the examples on this page, in the thought of using it for a project (more on that later if I get anywhere with it). Anyways…onto the code…which is primarily a modification from the examples page I mentioned earlier (see that page for more examples). devtools::install_github(“GIScience/openrouteservice-r”) Load some libraries library(openrouteservice) library(leaflet) Set the API key ors_api_key(“your-key-here”) Locations of interest and send the request to the API asking for the region that is accessible within a 15 minute drive of the coordinates. coordinates <- list(c(8.55, 47.23424), c(8.34234, 47.23424), c(8.44, 47.4)) x <- ors_isochrones(coordinates, range = 60*15, # maximum time…

Original Post: openrouteservice – geodata!

## grconvertX and grconvertY

These two functions are unbelievably useful for positioning graphical elements (text, axes, labels, …) in R. They allow one to convert coordinates between various different formats. For instance, you can convert your user coordinate (say 5 where x ranges from 0 to 200) to normalized device coordinates (proportional distance across the device) and vice versa. Very useful for positioning panel labels… grconvertX(.1, “npc”, “user”) returns the x coordinate that is 10% across the plot region. So with that and the equivalent y function, you can place your labels in exactly the same position on every panel and plot… e.g. plot(rnorm(20), rnorm(20)) text(grconvertX(.1, “npc”, “user”), grconvertY(.9, “npc”, “user”), “a)”) To show the difference between the from side, run the following… text(grconvertX(.1, “npc”, “user”), grconvertY(.9, “npc”, “user”), “npc”) text(grconvertX(.1, “ndc”, “user”), grconvertY(.9, “ndc”, “user”), “ndc”, xpd = “n”) # the text will…

Original Post: grconvertX and grconvertY

## Flow charts in R

Flow charts are an important part of a clinical trial report. Making them can be a pain though. One good way to do it seems to be with the grid and Gmisc packages in R. X and Y coordinates can be designated based on the center of the boxes in normalized device coordinates (proportions of the device space – 0.5 is this middle) which saves a lot of messing around with corners of boxes and arrows. A very basic flow chart, based very roughly on the CONSORT version, can be generated as follows… library(grid) library(Gmisc) grid.newpage() # set some parameters to use repeatedly leftx <- .25 midx <- .5 rightx <- .75 width <- .4 gp <- gpar(fill = “lightgrey”) # create boxes (total <- boxGrob(“Totaln N = NNN”, x=midx, y=.9, box_gp = gp, width = width)) (rando <- boxGrob(“Randomizedn…

Original Post: Flow charts in R

## Count models in JAGS

Looks like I’ll be diving into some Bayesian analyses using JAGS. This post is primarily intended as a collection of links to [potentially] useful information, but also includes a few initial thoughts (I might update it occasionally with new links). In terms of R packages, a very brief play suggests that R2jags is more user friendly than rjags (seems to be easier to get hold of the output and chains etc). Both packages seem to be missing good vignettes. The latter misses good examples too (although there are quite a few examples for both on the internet). A tip from Zuur & Ieno 2016: use a model matrix and assign priors based on the number of columns of the model matrix. Saves having to change the model description when adding/removing variables (how to include different priors for different betas though…?).…

Original Post: Count models in JAGS