Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating An Orthographic Map Using ggmap

In the following script, I read in a number of pairs of points and draw lines on a map. I am using ggmap to pull in a map from google to plot this lines over:

source('./library/latlong2state.R')

library(maps)
library(mapproj)
library(mapdata)
library(geosphere)
library(ggmap)

fileName = "_CanadaData/CanadaHospitalComplete.csv"

getLineColor <- function(val) {
  pal <- colorRampPalette(lineColours)
  colors <- pal(80)
  val.log <- log(val)

  if (val > 50) {
    col <- colors[80]
  } else {
    colindex <- max(1, round( 80 * val / 50))
    col <- colors[colindex]
  }
  return(col)
}

# Load the data
location <- read.csv(fileName, stringsAsFactors=FALSE)

# Omit locations that are not on the map of focus (not needed for city maps unless they are on a border)
location$state <- latlong2state(data.frame(location$lng, location$lat))
location$nearstate <- latlong2state(data.frame(location$lngnear, location$latnear))
location <- na.omit(location)

createMap <- function(bbox, thedata, mapzoom=3, linesize=0.6, pointsize=2) {
  basemap <- get_map(location=bbox, zoom=mapzoom, source='google', maptype="roadmap", color="color")
  ggmap(basemap) + geom_segment(aes(x=lng, xend=lngnear, y=lat, yend=latnear, color=dist_miles), size=0.6, data=thedata) + geom_point(aes(x=lngnear, y=latnear), size=2, color="#000000", border="black", data=thedata) + scale_color_gradient(low="blue", high="red", limits=c(0, max(thedata$dist_miles))) + coord_map("orthographic")
}

# Country bounding box c(left, bottom, right, top)
canada <- c(-140.920514, 42.016722, -52.524864, 83.2911)
createMap(canada, location)

Unfortunately, this results in massive distortions of distance at the top of the map because of Canada's northern latitude:

Distances to hospitals in Canada

This can be easily fixed by switching to an orthographic plot, and I can change the projection of how the lines are plotted by adding projection=mapprojection(orthographic) inside the createMap function, but I cannot change the projection of the map image I get from Google - it is stuck in the Web Mercator projection. Is there a way to do this using ggmap, or will I have to try a different package? If so, what do you recommend?

like image 562
Brideau Avatar asked Jan 05 '14 17:01

Brideau


People also ask

How to use ggplot + ggmap for Geospatial Visualization?

These maps can then be used as layers within the ggplot2 plotting system. Essentially, you can plot maps from ggmap, and then use ggplot2 to plot points and other geoms on top of the map. Using ggplot + ggmap this way is extremely useful for quick-and-dirty geospatial visualization.

How does orthographic mapping work?

When utilizing orthographic mapping a reader is using the oral pronunciation of words. The words that are already stored in their memory with accurate letter sequences. David Kilpatrick believes that letters or graphemes are anchored into the pronunciation of words. These pronunciations of words are already stored in our long-term memory.

What is the ggmap package on GitHub?

The ggmap package “makes it easy to retrieve raster map tiles from popular online mapping services like Google Maps and Stamen Maps and plot them using the ggplot2 framework”. To use the package you’ll need to setup an API key to google maps. For information on how to do this see the package homepage on GitHub.

How do I view the map in ggmap?

To view the map, use ggmap (): The zoom argument in get_stamenmap () controls the level of detail in the map. The larger the number, the greater the detail. The smaller the number, the lesser the detail. Trial and error will help you decide on the appropriate level of detail depending on what data you need to visualize on the map.


1 Answers

The short answer is "No". You're stuck with Google's so-called "Web Mercator" projection. (It is not WGS84. That's a datum, not a projection.)

I don't know if there are any R libraries for it, but you'll probably need to use a WMS server. A good choice would probably be GeoBase, which is specific to Canada. http://www.geobase.ca/geobase/en/wms/index.html

Also, I would suggest avoiding an orthographic projection, and instead use one that's been selected as appropriate for Canada, such as that chosen for the Atlas of Canada: EPSG:42304.

like image 154
Darrell Fuhriman Avatar answered Sep 19 '22 17:09

Darrell Fuhriman