Tagged with geom_point

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 this using RColorBrewer, but you can choose whatever method you'd like.

library(RColorBrewer)
colors = brewer.pal(8, "Dark2")

The next section will be exactly the same as the previous example, except for removing the scale_color_discrete line to make way for the scale_color_manual we'll be adding later.

library(ggplot2)

data <- as.data.frame(USPersonalExpenditure) # data from package datasets
data$Category <- as.character(rownames(USPersonalExpenditure)) # this makes things simpler later

ggplot(data,
    aes(x = Expenditure,
        y = Category)) +
labs(x = "Expenditure",
    y = "Category") +
geom_segment(aes(x = data$"1940",
        y = Category,
        xend = data$"1960",
        yend = Category),
    size = 1) +
geom_point(aes(x = data$"1940",
        color = "1940"), # these can be any string, they just need to be unique identifiers
    size = 4,
    shape = 15) +
geom_point(aes(x = data$"1960",
        color = "1960"),
    size = 4,
    shape = 15) +
theme(legend.position = "bottom") +

And finally, we'll add a scale_color_manual line to our plot. We need to define the name, labels, and colors of the plot.

scale_color_manual(name = "Year", # or name = element_blank()
    labels = c(1940, 1960),
    values = colors)

And here's our final plot, complete with whatever custom colors we've chosen in both the plot and legend:

geom_point in ggplot with custom colors in the graph and legend

I've updated the gist from the previous post to also include a file that has custom colors.

Tagged , , , , , ,

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_map

Helpfully, 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 instead you can just use get_map for the same functionality as all of those combined. This makes it easy to try out different basemaps for your data.

You need to supply get_map with your location data and the color, source, maptype, and zoom of the base map.

Let's go ahead and map the trails in Elwyn John Wildlife Sanctuary here in Atlanta. The csv data and R file are available in a gist.

gps <- read.csv("elwyn.csv",
    header = TRUE)

library(ggmap)
mapImageData <- get_map(location = c(lon = mean(gps$Longitude),
    lat = 33.824),
    color = "color", # or bw
    source = "google",
    maptype = "satellite",
    # api_key = "your_api_key", # only needed for source = "cloudmade"
    zoom = 17)

pathcolor <- "#F8971F"

ggmap(mapImageData,
    extent = "device", # "panel" keeps in axes, etc.
    ylab = "Latitude",
    xlab = "Longitude",
    legend = "right") +
    geom_path(aes(x = Longitude, # path outline
    y = Latitude),
    data = gps,
    colour = "black",
    size = 2) +
    geom_path(aes(x = Longitude, # path
    y = Latitude),
    colour = pathcolor,
    data = gps,
    size = 1.4) # +
# labs(x = "Longitude",
#   y = "Latitude") # if you do extent = "panel"

We'll be changing the four lines marked above in orange to change what basemap is used.

source = "google"

get_map option source = "google" (or using get_googlemap) downloads a map from the Google Maps API. The basemaps are © Google. Google Maps have four different maptype options: terrain, satellite, roadmap, and hybrid.

source = "google", maptype = "terrain"

source = "google", maptype = "terrain", zoom = 14

Max zoom: 14

source = "google", maptype = "satellite"

source = "google", maptype = "satellite", zoom = 17

Max zoom: 20

source = "google", maptype = "roadmap"

source = "google", maptype = "roadmap", zoom = 17

source = "google", maptype = "hybrid"

Hybrid combines roadmap and satellite. source = "google", maptype = "hybrid", zoom = 17

Max zoom: 14

source = "osm"

get_map option source = "osm" (or using get_openstreetmap) downloads a map from OpenStreetMap. These maps are Creative Commons licensed, specifically Attribution-ShareAlike 2.0 (CC-BY-SA). This means you are free to use the maps for commercial purposes, as long as you release your final product under the same Creative Commons license. OpenStreetMap has no maptype options.

source = "osm" (no maptype needed)

source = "osm", zoom = 17

Max zoom: 20

source = "stamen"

get_map option source = "stamen" (or using get_stamenmap) downloads a map from Stamen Maps. The map tiles are by Stamen Design, licensed under CC BY 3.0. The data for Stamen Maps is by OpenStreetMap, licensed under CC BY SA. Stamen has three different maptype options: terrain, watercolor, and toner.

source = "stamen", maptype = "terrain"

source = "stamen", maptype = "terrain", zoom = 17

Max zoom: 18

source = "stamen", maptype = "watercolor"

source = "stamen", maptype = "watercolor", zoom = 17

Max zoom: 18

source = "stamen", maptype = "toner"

source = "stamen", maptype = "toner", zoom = 17

Max zoom: 18

source = "cloudmade"

N.B. As of March 2014, CloudMade no longer provides this API service.

CloudMade styles build on top of OpenStreetMap data. Thousands of CloudMade styles are available. You can browse them on the CloudMade site. You can also make your own styles.

To use CloudMade map styles in R, you will first need to get an API key to insert into your R code so it can access the maps. You can get an API key from the CloudMade site.

Here are just a couple examples of CloudMade basemaps:

source = "cloudmade", maptype = "1", api_key="your_api_key_here, zoom = 17

source = "cloudmade", maptype = "67367", api_key="your_api_key_here, zoom = 17

Max zoom: 18

The code and data are available in a gist.

Tagged , , , , , , , , , , , , ,

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 variable
  • The new value of the variable
  • The change between old and new

One method I like to use to do this is using geom_segment and geom_point in the ggplot2 package.

First, let's load ggplot2 and our data:

library(ggplot2)

data <- as.data.frame(USPersonalExpenditure) # data from package datasets
data$Category <- as.character(rownames(USPersonalExpenditure)) # this makes things simpler later

Next, we'll set up our plot and axes:

ggplot(data,
    aes(y = Category)) +
labs(x = "Expenditure",
    y = "Category") +

For geom_segment, we need to provide four variables. (Sometimes two of the four will be the same, like in this case.) x and y provide the start points, and xend and yend provide the endpoints.

In this case, we want to show the change between 1940 and 1960 for each category. Therefore our variables are the following:

  • x: "1940"
  • y: Category
  • xend: "1960"
  • yend: Category
geom_segment(aes(x = data$"1940",
  y = Category,
  xend = data$"1960",
  yend = Category),
 size = 1) +

Next, we want to plot points for the 1940 and 1960 values. We could do the same for the 1945, 1950, and 1955 values, if we wanted to.

geom_point(aes(x = data$"1940",
    color = "1940"),
    size = 4, shape = 15) +
geom_point(aes(x = data$"1960",
    color = "1960"),
    size = 4, shape = 15) +

Finally, we'll finish up by touching up the legend for the plot:

scale_color_discrete(name = "Year") +
theme(legend.position = "bottom")

geom_segment, then geom_point

The order of geom_segment and the geom_points matters. The first geom line in the code will get plotted first. Therefore, if you want the points displayed over the segments, put the segments first in the code. Likewise, if you want the segments displayed over the points, put the points first in the code.

For example, we could change the middle section of the code to:

geom_point(aes(x = data$"1940",
  color = "1940"),
  size = 4, shape = 15) +
geom_point(aes(x = data$"1960",
  color = "1960"),
  size = 4, shape = 15) +

geom_segment(aes(x = data$"1940",
    y = Category,
    xend = data$"1960",
    yend = Category),
  size = 1) +

And the output would look like:

geom_point, then geom_segment

Similarly, if you have points that will be overlapping, make sure you think about which of the point lines you want R to plot first.

The code is available in a gist.

Tagged , , , , , ,

Mapping GPS Tracks in R

This is an explanation of how I used R to combine all my GPS cycling tracks from my Garmin Forerunner 305.

Converting to CSV

You can convert pretty much any GPS data to .csv by using GPSBabel. For importing directly from my Garmin, I used the command:

gpsbabel -t -i garmin -f usb: -o unicsv -F out.csv

[Note: you'll probably need to work as root to access your device directly]

For importing from a .tcx file, you can use:

gpsbabel -t -i gtrnctr -f test2.tcx -o unicsv -F old.csv

Mapping in R

After converting to .csv, we'll have a file with several columns, such as latitude, longitude, date, and time. We can now easily import this into R.

gps <- read.csv("out.csv", 
    header = TRUE)

Next we want to load up ggmap and get our base map. To determine how zoomed in we are, we can set zoom and size. We can also choose the maptype, with options of terrain, satellite, roadmap, or hybrid (satellite + roadmap).

library(ggmap)
mapImageData <- get_googlemap(center = c(lon = median(gps$Longitude), lat = median(gps$Latitude)),
    zoom = 11,
# size = c(500, 500),
    maptype = c("terrain"))

I chose to set the center of the map to the median of my latitudes and the median of my longitudes. I've done some biking when traveling, so median made more sense for me than mean. Finally we want to map our GPS data. There are several pch options to try.

ggmap(mapImageData,
    extent = "device") + # takes out axes, etc.
    geom_point(aes(x = Longitude,
        y = Latitude),
    data = gps,
    colour = "red",
    size = 1,

All my metro Atlanta bike rides

Previously, I've used Google Earth to create these maps, but I actually found it to be easier and way less time and resource efficient to do it in R. The only tricky part was converting the data into .csv, and there are other ways to do that, if GPSBabel isn't working for you. You might also be interested in trying Google Earth for mapping your tracks, instead of R.

Here's the gist with the code.

This post is one part of my series on Mapping GPS Tracks.

Citations and Further Reading

pch = 20)

Tagged , , , , ,