University of Wisconsin–Madison

Tag: R

Distance to a lake

How far are you from a lake? Living in Wisconsin, I wanted to know. There’s a lot of lakes in Wisconsin, but it was hard to visualize their distribution. Step 1) Download the high-resolution National Hydrography Dataset for Wisconsin (I host this locally as a shapefile). I only kept lakes with a surface area larger than …

Extracting polygon areas in R

Suppose I have a shapefile (lakes.shp) which consists of 5 polygons (Figure 1a). library(raster) lakes = shapefile(’lakes.shp’) plot(lakes, col=’dodgerblue’,main=’a) lakes’,lty=0) The shapefile is stored as a SpatialPolygonsDataFrame. The data underlying these polygons can be viewed with: lakes@data There is a column called SHAPE_Area, which is the area of each polygon. Now let’s say, we want …

readr 0.2

If your life as an ecologist is spent loading/writing .csv files in R, you should be using readr. Why? Well it’s a Hadley Wickham package for one. But really, it’s defaults make sense! read_csv: strings are assumed as characters, not factors. FINALLY! write_csv: doesn’t include row numbers by default. readr recognizes YYYY-mm-dd format. Goodbye strptime! …

Find my weather station

Ever wonder how much it snowed last year, or the year before, or every year since 1950? Well, to find this data, you’ll first need to choose a weather station. As much as I like wasting time on the NOAA website, I figured this could be easier in R. Introducing nearWX (found here) All you …

Oh god, umlauts!

Sweden has more lake data than most countries have lakes. However, they also have a 29 letter alphabet including ‘å’, ‘ä’ and ‘ö’. This results in lake names like Fräcksjön and Östra Helgtjärnen. Goal Combine two .csv files (data and metadata). The common ID is lake name. Problem Every lake name with a ‘swedish character’ …

Watercolor your lakes in R

All lakes are beautiful. Or at least when they’re coded in watercolor! For this you will need to install and load ggmap. I am a huge Hadley Wickham fan (obvs), but am not usually a proponent of plotting packages. I love base. However, you can’t beat this one. install.packages(‘ggmap’) library(ggmap) You will need two customize …