* You are viewing the archive for the ‘R-project’ Category

R plotmath functions combined with variable values

Getting certain special symbols into R plots, combined with values that are currently stored in variables, has been an ongoing headache of mine. In particular, plotmath symbols such as the plus-minus sign (±), for which the plotmath command is %+-%, had always caused problems due to my inability to parse the R help documentation. For an example, I was trying to get the following simple value (a mean ± 1 SE for a temperature difference) inserted into a plot (a minimalist example):

It turns out that the plotmath operators that are surrounded … Continue Reading

rtide: a R package for predicting tide heights (US locations only currently)

Joe Thorley at Poisson Consulting has released a new R package, rtide, (on which I am listed as a co-author) that provides the ability to predict future (and past) tide heights for 637 different tide stations in the United States and associated territories. The underlying data, consisting of tide harmonic constituents, are collected and released by the National Oceanic and Atmospheric Administration. The author of the definitive open source tide prediction software, XTide, collates those harmonic data into a usable format, and we have harvested the data to create the … Continue Reading

Using knitr and R to make instructor/student handout versions

I teach some of my lab sections using R, and so I need to create lab handouts that include nicely formatted R commands and R output as an example for the students. These handouts will also include exercises where the students will be writing their own R code, or interpreting the results, or generating figures. For these exercises, it is useful to also have an instructor version of the handout so that I can recall what I was hoping to have the students do, and so that other instructors in the course have some clue as to what I might … Continue Reading

Basic text string functions in R

To get the length of a text string (i.e. the number of characters in the string):

 nchar()

Using length() would just give you the length of the vector containing the string, which will be 1 if the string is just a single string.

To get the position of a regular expression match(es) in a text string x:

pos = regexpr('pattern', x) # Returns position of 1st match in a string
pos = gregexpr('pattern', x) # Returns positions of every match in a string

To get the position of a regular expression match in a vector x of text strings … Continue Reading

A plot of co-authorships in my little corner of science

author year count image

 
Here’s a mostly useless visualization of the collection of journal articles that sits in my reference database in Endnote. I deal mostly in marine biology, physiology, biomechanics, and climate change papers, with a few molecular/genetics papers thrown in here and there. The database has 3325 entries, 2 of which have ambiguous publication years and aren’t represented above. This is by no means an exhaustive survey of the literature in my field, it’s just an exhaustive survey of the literature on my computer.

To make this figure, I first had Endnote export the database to … Continue Reading

Extracting NOAA sea surface temperatures with ncdf4

I’ve written previously about some example R scripts I created to extract sea surface temperature data from NOAA’s Optimum Interpolated Sea Surface Temperature products. If you want daily global sea surface temperatures on a 0.25×0.25° grid, they gather those into 1-year files available at http://www.esrl.noaa.gov/psd/data/gridded/data.noaa.oisst.v2.highres.html. If you want weekly average SST values on a 1×1° grid, you can get those back to 1981 at http://www.esrl.noaa.gov/psd/data/gridded/data.noaa.oisst.v2.html. If you just want a smaller file for a single day on the 0.25×0.25° grid, you can find those at http://www.ncdc.noaa.gov/sst/griddata.php.

Continue Reading

NOAA OISST v2 High Resolution daily sea surface temperatures with R

Update, 2015-11-30 It appears that NOAA has gone through and upgraded all of the OISST files to the newer version of the NetCDF file format. As a result, the functions outlined in this post don’t work any longer. Instead, see the updated functions in my newer post, http://lukemiller.org/index.php/2014/11/extracting-noaa-sea-surface-temperatures-with-ncdf4/. The concepts are the same as described here, but the newer functions use the ncdf4 package to access the newer NetCDF file format.

The National Ocean and Atmospheric Administration generates freely-available world-wide estimates of mean daily sea surface temperature, and has been doing so back to 1981. The data are on … Continue Reading

Part 2: Make your R figures legible in Powerpoint/Keynote presentations

In the previous post, I outlined some tips for increasing the size of figure labels for figures that are meant to be displayed on a projector. The previous post used the base R plot() function, but the procedure when plotting with ggplot2 is different and usually quite a bit simpler than the stock R plotting functions. As before, I’m outputting the figures here as 1024×768 PNG image files, since they’re sure to work in whatever version of PowerPoint or Keynote you’re stuck using.

I’ll begin by generating some random data and dates to use in the plots. … Continue Reading

Make your R figures legible in Powerpoint/Keynote presentations

Having just returned from the SICB 2014 meetings, the appearance of many people’s Powerpoint figures is fresh on my mind. The sheer number of tiny figure labels (tick marks, axis titles, legend text etc) is disappointing. If we want to point fingers, MATLAB users are clearly the worst offenders because of the microscopic default label sizes in that program, but there are plenty of illegible R and matplotlib figures out there as well. Excel is obviously its own special class of terrible, but we will speak of it no more. The default settings in most of these programs … Continue Reading

More tide prediction with R

Edit: There is now a full-fledged R package, rtide, to accomplish the same basic task of generating time series of tide predictions that is outlined here. See this more recent post for information.

In the previous post I outlined how to query the XTide software with R and parse the results into a handy-dandy data frame. The biggest hurdle with that method is getting XTide up and running on your computer. The code outlined here works entirely within R, so you don’t need XTide installed on your computer. The trade-off is that it provides substantially … Continue Reading

Page 1 of 41234