Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.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
带ggplot的MAPR数据坐标?_R_Ggplot2 - Fatal编程技术网

带ggplot的MAPR数据坐标?

带ggplot的MAPR数据坐标?,r,ggplot2,R,Ggplot2,我试图将地图和地图数据包与ggplot一起使用,但我似乎无法从数据中提取坐标。具体地说,我正试图策划美国的某些州和加拿大的一些省份 library(maps) library(mapdata) library(ggplot2) library(ggthemes) library(raster) us <- getData("GADM", country = "USA", level = 1) canada <- getData("GADM", country = "CAN", lev

我试图将地图和地图数据包与ggplot一起使用,但我似乎无法从数据中提取坐标。具体地说,我正试图策划美国的某些州和加拿大的一些省份

library(maps)
library(mapdata)
library(ggplot2)
library(ggthemes)
library(raster)
us <- getData("GADM", country = "USA", level = 1)
canada <- getData("GADM", country = "CAN", level = 1)
states <- c('connecticut', 'new york', 'new jersey', 'massachusetts')
provinces <- c('Ontario', 'Alberta', 'Quebec')

us.states <- us[us$NAME_1 %in% states, ]
ca.provinces <- canada[canada$NAME_1 %in% provinces, ]

ggplot(us.states, aes(x = longitude, y = latitude, group = group)) +
  geom_path()+
  geom_path(data = ca.provinces)+
  coord_map()

有人帮忙吗?我不熟悉R中的映射。

代码几乎不错。我只是做了一些小改动。 1.州名称和省名称需要在空间多边形数据框中精确匹配。所以州名的第一个字母必须是大写。“魁北克”必须是“魁北克”。 2.将
经度
纬度
分别更改为
纬度

us <- getData("GADM", country = "USA", level = 1)
canada <- getData("GADM", country = "CAN", level = 1)
states <- c('Connecticut', 'New York', 'New Jersey', 'Massachusetts')
provinces <- c('Ontario', 'Alberta', 'Québec')

us.states <- us[us$NAME_1 %in% states, ]
ca.provinces <- canada[canada$NAME_1 %in% provinces, ]

ggplot(us.states, aes(x = long, y = lat, group = group)) +
  geom_path()+
  geom_path(data = ca.provinces)+
  coord_map()

us-Huh,我复制粘贴了您的代码,得到了相同的错误:为每个多边形定义的区域为.Call.graphics中的每个多边形定义的区域错误(C_palette2,.Call(C_palette2,NULL)):无效的图形状态可能是依赖项?啊。我修正了错误。请参阅此线程:
us <- getData("GADM", country = "USA", level = 1)
canada <- getData("GADM", country = "CAN", level = 1)
states <- c('Connecticut', 'New York', 'New Jersey', 'Massachusetts')
provinces <- c('Ontario', 'Alberta', 'Québec')

us.states <- us[us$NAME_1 %in% states, ]
ca.provinces <- canada[canada$NAME_1 %in% provinces, ]

ggplot(us.states, aes(x = long, y = lat, group = group)) +
  geom_path()+
  geom_path(data = ca.provinces)+
  coord_map()