Coloured output in the R console

Just a little fun today… the R console isn’t the most interesting of things… text is typically either black or red (assuming default settings in RStudio). There’s a package though called crayon which allows one to change the style of text in terms of colour, background and some font-type settings. It could be an interesting way to spice up summaries (I’m not recommending it, but it’s a possibility. As far as packages are concerned, it’s just another dependency…)… devtools::install_github(“r-lib/crayon”) library(crayon) cat(green( ‘I am a green line ‘ %+% blue$underline$bold(‘with a blue substring’) %+% yellow$italic(‘ that becomes yellow and italicised!n’) )) Related R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time…
Original Post: Coloured output in the R console

rOpenSci’s drake package

If you don’t know rOpenSci, then I recommend checking them out. They write a lot of really good packages for R. A relatively new seems to be drake. I’ve not played with it yet, but it looks to be very useful at giving indications about which parts of an analysis are subject to changes, and only rerunning those parts to speed up redoing an analysis (envisage the overlays for some version control systems or dropbox that show the status of files, although it’s more complicated than that). Knitr has caching, which goes some way to handling this, but here you can see where outdated parts are in the scope of the entire analysis… It looks like a super tool! Related R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs,…
Original Post: rOpenSci’s drake package

Rough looking figures from R

A recent blog post regarding data visualization had some barplots I liked the look of (aesthetically…for research purposes, they wouldn’t be suitable). They look as if they’ve be coloured in with a pencil, rather than having solid blocks of colour… I wondered whether it’s possible with R, and indeed it is. There’s a github project called ggrough that interacts with ggplot2. Related R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more… If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook…
Original Post: Rough looking figures from R

Beeswarms instead of histograms

Histograms are good, density plots are also good. Violin and bean plots too. Recently I had someone ask for a plot where you could see each individual point along a continuum, give the points specific colours based on a second variable (similar to the figure), which deviates somewhat from the typical density type plots. Apparently, they’re called beeplots or beeswarms. And there’s a way to make them in R (of course, there’s probably more than one… ggplot2??). Here’s one way (slightly modified from the packages help files)… library(beeswarm) # assuming you’ve installed it 😉 data(breast) beeswarm(time_survival ~ ER, data = breast, pch = 16, pwcol = 1 + as.numeric(event_survival), xlab = “”, ylab = “Follow-up time (months)”, horizontal = TRUE, labels = c(“ER neg”, “ER pos”), method = “c”) legend(“topright”, legend = c(“Yes”, “No”), title = “Censored”, pch = 16,…
Original Post: Beeswarms instead of histograms

Merging spatial buffers in R

I’m sure there’s a better way out there, but I struggled to find a way to dissolve polygons that touched/overlapped each other (the special case being buffers). For example,  using the osmdata package, we can download the polygons representing hospital buildings in Bern, Switzerland. library(osmdata) library(rgdal) ; library(maptools) ; library(rgeos) q0 <- opq(bbox = “Bern, Switzerland”, timeout = 60) q1 <- add_osm_feature(q0, key = ‘building’, value = “hospital”) x <- osmdata_sp(q1) library(leaflet) spChFIDs(x$osm_polygons) <- 1:nrow([email protected]) cent <- gCentroid(x$osm_polygons, byid = TRUE) leaflet(cent) %>% addTiles() %>% addCircles() Here we plot the building centroids. Each point represents a hospital building. We don’t particularly care about the buildings themselves though. We want to create hospitals. To do so, we try a 150m buffer around each centroid. buff <- gBuffer(cent, byid = TRUE, width = 0.0015) leaflet(cent) %>% addTiles() %>% addPolygons(data = buff,…
Original Post: Merging spatial buffers in R

Intersecting points and overlapping polygons

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

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