Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot地图在添加geom_点时移动_R_Ggplot2_Maps_Geom Point - Fatal编程技术网

R ggplot地图在添加geom_点时移动

R ggplot地图在添加geom_点时移动,r,ggplot2,maps,geom-point,R,Ggplot2,Maps,Geom Point,我正在尝试在我的lat/lon点中使用ggplot和分层创建地图 我毫无疑问地创建了美国地图,但当我在geom_point lat/lon位置分层时,美国地图缩小并发生变化。有人能告诉我为什么会这样吗 stateData <- map_data('state') head(stateData) us <- fortify(stateData, region = 'region') gg <- ggplot() + geom_map(data = us, map = us,

我正在尝试在我的lat/lon点中使用ggplot和分层创建地图

我毫无疑问地创建了美国地图,但当我在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)  


通过查看您的图像,很明显您有一个红点,位于美国东北部。您提供的示例集中没有该点。我将模拟一个类似的异常值,但是代码应该修复投影问题

设置的点:

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"))

你好,ZHX。我不能重现你的错误。我得到了相同的地图对齐与否你的点。您首先加载了哪些软件包?谢谢您的回复。我加载了以下软件包(如果您需要其他信息或我的数据集,请告诉我):库(dplyr)库(地图)库(mapproj)库(ggplot2)库(tidyverse)库(比例尺)库(传单)库(plyr)库(GGRINGS)库(HRBRTHEMS)库(fmsb)库(radarchart)库(forcats)库(ggthemes)我正在运行RStudio 1.4和R4.03。使用Windows 10。请参阅我编辑的文章,了解我在使用geom_point之前和使用geom_point之后的屏幕链接。你解决了它!你是最棒的!!!!非常感谢你,我一直在为这个问题绞尽脑汁。你能解释一下我为什么会遇到问题吗?我很高兴它奏效:)东北点是一个离群点,它迫使地图向东延伸。在coord_map()的默认投影下,如此广阔的领土的矩形显示将始终显示“弯曲”的北美。想想那些覆盖北美和欧亚大陆的地图。
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.