添加 geom_point 时 ggplot 地图移动
ggplot Map shifts when adding geom_point
我正在尝试使用 ggplot 创建地图并在我的 lat/lon 点中分层。
我毫无问题地创建了美国地图,但是当我在我的 geom_point lat/lon 位置中分层时,美国地图缩小并发生了变化。有人可以告诉我为什么会这样吗?
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
通过查看您的图像,很明显您有一个红点位于美国东北部。您提供的示例集中没有这一点。我将模拟一个类似的异常值,但代码应该可以解决投影问题。
积分设置:
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"))
映射属性:
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()
第一个备选方案,过滤异常值。
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)
第二种选择,限制绘图的水平范围。
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.
我正在尝试使用 ggplot 创建地图并在我的 lat/lon 点中分层。
我毫无问题地创建了美国地图,但是当我在我的 geom_point lat/lon 位置中分层时,美国地图缩小并发生了变化。有人可以告诉我为什么会这样吗?
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
通过查看您的图像,很明显您有一个红点位于美国东北部。您提供的示例集中没有这一点。我将模拟一个类似的异常值,但代码应该可以解决投影问题。
积分设置:
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"))
映射属性:
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()
第一个备选方案,过滤异常值。
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)
第二种选择,限制绘图的水平范围。
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.