ZHX
ZHX

Reputation: 77

ggplot Map shifts when adding geom_point

I am trying to create a map using ggplot and layering in my lat/lon points.

I created the map of the US without issue but when I layer in my geom_point lat/lon positions the map of the US shrinks and changes. Can someone please point me to why this is happening?

stateData <- map_data('state')
head(stateData)
us <- fortify(stateData, region = 'region')
gg <- ggplot() + geom_map(data  =  us, map = us,
                         aes(x = long, y = lat, map_id = region, group = group),
                         fill = 'white', color = 'black', size = 0.25) + 
  coord_map('albers', lat0 = 39, lat1 = 45) +
  theme_map()


gg + #add the data points with lon/lat declaring the columns
  geom_point(data=new_datav2, aes(x=lon, y=lat), color='red', alpha=0.15)  
postalCode  county                 lat       lon
94102   San Francisco County    37.77711868 -122.4196396
94102   San Francisco County    37.77711868 -122.4196396
94102   San Francisco County    37.77711868 -122.4196396
94102   San Francisco County    37.77711868 -122.4196396
94612   Alameda County  37.80508041 -122.2730713
94002   San Mateo County    37.51834106 -122.276207
94102   San Francisco County    37.77711868 -122.4196396
94102   San Francisco County    37.77711868 -122.4196396
94102   San Francisco County    37.77711868 -122.4196396
94612   Alameda County  37.80508041 -122.2730713
94102   San Francisco County    37.77711868 -122.4196396
94102   San Francisco County    37.77711868 -122.4196396
94102   San Francisco County    37.77711868 -122.4196396
94102   San Francisco County    37.77711868 -122.4196396
94102   San Francisco County    37.77711868 -122.4196396
94102   San Francisco County    37.77711868 -122.4196396
94102   San Francisco County    37.77711868 -122.4196396
94063   San Mateo County    37.48450089 -122.2277222
94102   San Francisco County    37.77711868 -122.4196396
94596   Contra Costa County 37.90118027 -122.0616226
94102   San Francisco County    37.77711868 -122.4196396
94704   Alameda County  37.86988068 -122.2705383
94612   Alameda County  37.80508041 -122.2730713


enter image description here

enter image description here

enter image description here

Upvotes: 1

Views: 308

Answers (1)

Nicol&#225;s Velasquez
Nicol&#225;s Velasquez

Reputation: 5908

By looking at your image it is evident that you have a red point that is way to the northeast of the USA. This point is absent from your supplied example set. I will simulate a similar outlier, but the code should fix the projection problem.

enter image description here

The points set:

df_points <- 
structure(list(
    postalCode = c(94102, 94612, 94102, 94063, 0), 
    County = c("San Francisco County", "Alameda County", "San Francisco County", 
    "San Mateo County", "This_is_the_outlier"), 
    lat = c(37.77711868, 37.80508041, 
    37.77711868, 37.48450089, 40), 
    lon = c(-122.4196396, -122.2730713, 
    -122.4196396, -122.2277222, -10)), 
  row.names = c(NA, -5L), 
  class = c("tbl_df", "tbl", "data.frame"))

The mapping propper:

library(tidyverse)
library(maps)
library(mapproj)
library(ggthemes)

us <- fortify(stateData, region = 'region')
gg <- ggplot() + 
  geom_map(data  =  us, map = us,
           aes(x = long, y = lat, map_id = region, group = group),
           fill = 'white', color = 'black', size = 0.25) +
  coord_map('albers', lat0 = 39, lat1 = 45) +
  theme_map()

First alternative, filter the outlier.

gg + #add the data points with lon/lat declaring the columns
  geom_point(data=df_points %>% filter(lon < -65), ## Here is where you filer the eastern outlier by excluding all data east of longitude 65W.
             aes(x=lon, y=lat), color='red', alpha=0.15) 

Second alternative, limit the plot's horizontal limits.

gg + #add the data points with lon/lat declaring the columns
  geom_point(data=df_points,
             aes(x=lon, y=lat), color='red', alpha=0.15) +
    coord_map(xlim = c(-130, -65)) # Here you crop the plotting images from 130W to 65W.

enter image description here

Upvotes: 1

Related Questions