Aaron A.
Aaron A.

Reputation: 33

How do I change the projection method used by R for mapping coordinates?

My question is very similar to this one: R Plot Filled Longitude-Latitude Grid Cells on Map

I used the solution given by Richard to plot the number of earthquakes over a certain time period onto a map of California

library(ggplot2)
library(maps)
caliMap <- map_data('state','california',lwd=50)
g <- (ggplot(aes(x=Longitude,y=Latitude,fill=Events),data=eqGrid) + 
     geom_tile()) + 
     geom_polygon(data=caliMap,aes(x=long, y=lat, group=group), 
                  colour='firebrick', fill='white', alpha=0, lwd=1.2)

This is a modified code from Richard's given solution. My question is, does this take into account the alteration of the geometry from the projection of a sphere onto a plane? If so I have difficulty seeing where this is being done. How can I change the form of projection used?

Also, as an aside, how can I change the titles of my legend, etc.? From what I see, if I change the titles, it won't properly pull them from the titles of my data.frame (I really need to change the legend to say 'Events (log n)', but I can't figure it out, and putting a space anywhere messes things up).

I just started using R today after seeing that it had a seemingly easy solution to a problem I couldn't find much help for elsewhere. I'm not a very experienced programmer either, so I appreciate if you can be as simple as possible.

Upvotes: 1

Views: 4911

Answers (2)

hrbrmstr
hrbrmstr

Reputation: 78842

The California Geological Survey recommends Lambert conformal for states (like California) that are elongated north-to-south.

Here's a comparison between Albers and Lambert:

library(ggplot2)
library(gridExtra)
library(ggthemes)

cali_map <- map_data('state','california',lwd=50)
gg <- ggplot()
gg <- gg + geom_map(data=cali_map, map=cali_map,
                    aes(x=long, y=lat, map_id=region),
                    color="black", fill="white", size=0.25)
gg <- gg + coord_map("lambert", lat0=32.5343, lat=142.0095)
gg <- gg + labs(x=NULL, y=NULL, title="Lambert")
gg <- gg + theme_map()
gg1 <- gg

gg <- ggplot()
gg <- gg + geom_map(data=cali_map, map=cali_map,
                    aes(x=long, y=lat, map_id=region),
                    color="black", fill="white", size=0.25)
gg <- gg + coord_map("albers", lat0=32.5343, lat=142.0095)
gg <- gg + labs(x=NULL, y=NULL, title="Albers")
gg <- gg + theme_map()
gg2 <- gg

grid.arrange(gg1, gg2, ncol=2)

enter image description here

Please use a separate question and post your data for your other issue.

Upvotes: 2

RHertel
RHertel

Reputation: 23818

There are numerous ways to project the map from the sphere onto a plane.

Here are a few examples:

library(ggplot2)
library(maps)
US_map <- map_data("state")
g <- ggplot(aes(x=long,y=lat,group=group),data=US_map) +  geom_path() 
g1 <- g + coord_map("albers",lat0=39, lat1=45)
g2 <- g + coord_map("mercator")
g3 <- g + coord_map("gilbert")
g4 <- g + coord_map("stereographic")
g5 <- g + coord_map("cylindrical")
g6 <- g + coord_map("azequalarea")

These are the plots for g1 to g6 in the example above: enter image description here

More information is available with:

> library(ggplot2)
> ?coord_map

PS: Thanks to @hrbrmstr for pointing out the relevance of the Albers projection for the US map.

Upvotes: 5

Related Questions