Blog Archives

Using colClasses to Load Data More Quickly in R

September 5, 2013
By

Specifying a colClasses argument to read.table or read.csv can save time on importing data, while also saving steps to specify classes for each variable later.For example, loading a 893 MB took 441 seconds to load when not using colClasses, b...

Read more »

Plot Weekly or Monthly Totals in R

August 29, 2013
By
Plot Weekly or Monthly Totals in R

When plotting time series data, you might want to bin the values so that each data point corresponds to the sum for a given month or week. This post will show an easy way to use cut and ggplot2's stat_summary to plot month totals in R wi...

Read more »

Date Formats in R

August 22, 2013
By

Importing DatesDates can be imported from character, numeric, POSIXlt, and POSIXct formats using the as.Date function from the base package.If your data were exported from Excel, they will possibly be in numeric format. Otherwise, they will m...

Read more »

geom_point Legend with Custom Colors in ggplot

March 7, 2013
By
geom_point Legend with Custom Colors in ggplot

Formerly, I showed how to make line segments using ggplot.Working from that previous example, there are only a few things we need to change to add custom colors to our plot and legend in ggplot.First, we'll add the colors of our choice. I'll do th...

Read more »

Shapefiles in R

February 28, 2013
By
Shapefiles in R

Let's learn how to use Shapefiles in R. This will allow us to map data for complicated areas or jurisdictions like zipcodes or school districts. For the United States, many shapefiles are available from the Census Bureau. Our example will map U.S. nati...

Read more »

Elevation Profiles in R

February 21, 2013
By
Elevation Profiles in R

First, let's load up our data. The data are available in a gist. You can convert your own GPS data to .csv by following the instructions here, using gpsbabel.gps <- read.csv("callan.csv",  header = TRUE)Next, we can use the function SMA fr...

Read more »

GPS Basemaps in R Using get_map

February 14, 2013
By
GPS Basemaps in R Using get_map

There are many different maps you can use for a background map for your gps or other latitude/longitude data (i.e. any time you're using geom_path, geom_segment, or geom_point.)get_mapHelpfully, there's just one function that will allow you to query Google Maps, OpenStreetMap, Stamen maps, or CloudMade maps: get_map in the ggmap package. You could also use either get_googlemap, get_openstreetmap, get_stamenmap, or get_cloudmademap, but...

Read more »

Using Line Segments to Compare Values in R

January 31, 2013
By
Using Line Segments to Compare Values in R

Sometimes you want to create a graph that will allow the viewer to see in one glance:The original value of a variableThe new value of the variableThe change between old and newOne method I like to use to do this is using geom_segment and geom_poin...

Read more »

Storing a Function in a Separate File in R

January 24, 2013
By

If you're going to be using a function across several different R files, you might want to store the function in its own file.If you want to name the function in its own fileThis is probably the best option in general, if only because you may want to p...

Read more »

Calculating a Gini Coefficients for a Number of Locales at Once in R

January 17, 2013
By
Calculating a Gini Coefficients for a Number of Locales at Once in R

The Gini coefficient is a measure of the inequality of a distribution, most commonly used to compare inequality in income or wealth among countries.Let's first generate some random data to analyze. You can download my random data or use the code below ...

Read more »