将坐标映射到R中的位置

时间:2015-03-12 11:16:25

标签: r maps rgooglemaps

我正在使用埃博拉数据集,在数据集中没有给出位置坐标。我想手动将经度和纬度分配给该位置,以便我可以将位置绘制到Google地图。我想要在R.Any想法实现这一点,我一直在尝试这个,但不适合我:

if(ebola$Country == 'Guinea' & ebola$Location == 'Conakry')
{
conakry <- c(9.5092,13.7122)
}

1 个答案:

答案 0 :(得分:0)

#Dataset example
df <- read.table(text = "Col1   Col2   Location  
1      2      Conkary
3      1      Monrovia
2      2      Freetown
1      3      Greenville", 
                 header = TRUE, stringsAsFactors = FALSE)

#Create a Dataframe with geographic information
Geo <- read.table(text = "Country   Location   LON LAT  
Guinea      Conkary   9.5092    -13.7122    
Liberia     Monrovia  6.3000    -10.7659       
    ", 
                 header = TRUE, stringsAsFactors = FALSE)
library(plyr)
df_Geo <- join(df, Geo, by = "Location", type= "left")

df_Geo2 <- join(df, Geo, by = "Location", type="right")

#Create a SpatialPoints Object
library(sp)
Coords <- cbind(as.numeric(df_Geo2$LON),as.numeric(df_Geo2$LAT)) 
row.names(Coords) <- 1:nrow(Coords) 
#Choosing the projection, should be good for google maps
LLCRS <- CRS("+proj=longlat +ellps=WGS84")
Coords_sp <- SpatialPoints(Coords, proj4string = LLCRS)

#Create a SpatialPointsDataFrame
Ebola.sp <- SpatialPointsDataFrame(Coords_sp, df_Geo2, proj4string = LLCRS, match.ID = TRUE)

#Plot on GoogleMaps
library(plotGoogleMaps)
map <- plotGoogleMaps(Ebola.sp, filename='MapEbola.html')
#Coordinates are inexact due to bad projection.