从数据框映射R中的位置

时间:2017-02-20 20:05:59

标签: r mapping

我刚刚从福克兰岛获得了我的位置数据,我试图从标签中找到我所获得的位置,但是,当我运行此代码时,r停止工作。

我有30只企鹅的数据框,每个都有不同的位置,因此,表格如下所示: ID: penguin ID, V3 is longitud, V4 is latitude

这是我尝试过的代码:

gentoo<-read.csv("Regularised Gentoos.csv", header=F)

plot(gentoo$V3~gentoo$V4,ylab="Latitude",xlab="Longitude",
     col=gentoo$V1,aspect="iso")

2 个答案:

答案 0 :(得分:0)

我可以用该区域的水深测量来绘制我的数据:

#Load in libraries
library(sp)
library(rgdal)
library(rgeos)
library(maptools)
library(raster)
library(ggplot2)
library(scales)
library(gridExtra)
library(adehabitatHR)
library(maptools)
library(marmap)
library(maptools)

#Load in shapefile from NaturalEarthData.com
#Load in shapefile from NaturalEarthData.com
world_shp = rgdal::readOGR("/Users/danielgonzalez/Desktop/Thesis/DATA EMAILS/natural_earth_vector/10m_physical",layer = "ne_10m_land")
world_shp

#Load in .csv file
gentoo = read.csv("/Users/danielgonzalez/Desktop/Thesis/DATA EMAILS/Regularised Gentoos.csv")
#Create a spatial points dataframe
locs = sp::SpatialPointsDataFrame(coords = cbind(gentoo$lon, gentoo$lat), data = gentoo, proj4string=CRS("+proj=longlat +datum=WGS84"))
locs

#Extra...
#To download bathymetry data FALKLAND ISLAND MAP
#ETOPO1 database hosted on the NOAA website
library(marmap)
getNOAA.bathy(lon1=-70, lon2=-52, lat1=-57, lat2=-46, resolution = 1) -> bathy 
plot(bathy, image=TRUE, deep=-6000, shallow=0, step=1000)
bat = as.raster(bathy)
#Write
writeRaster(bat, filename = "~/bathy.asc")
#Read
bat = readGDAL("~/bathy.asc")


#Load in Raster data
#This is 1 min resolution bathymetry from ETOPO1 database hosted on the NOAA website

bathy = raster::raster("/Users/danielgonzalez/bathy.asc")
#Define projection of bathymetry data
raster::projection(bathy) = CRS("+proj=longlat +datum=WGS84")

#Create a spatialpoints object of the colony
Colonsay = sp::SpatialPoints(coords = cbind(-6.25, 56.07), proj4string = CRS("+proj=longlat +datum=WGS84"))

#Quick plot

#png("gentoo distribution.png",width=8,height=6,units="in",res=1800)
image(bathy,ylab="Latitude",xlab="Longitud")
lines(world_shp)
points(gentoo$lon,gentoo$lat, pch = 19, cex = 0.3,col=gentoo$id)
#dev.off()

答案 1 :(得分:-1)

只需使用ggmap

即可
library(ggmap)
library(ggplot2)

#lat/lon data
df <- as.data.frame(matrix(nrow = 3, ncol =3))

colnames(df) <- c("lat", "lon", "id")

df$lon <- c(-51.2798, -51.3558, -51.9)
df$lat <- c( -59.6387,  -59.7533, -59.4)
df$id <- c("1", "2", "3")

df
       lat      lon id
1 -59.6387 -51.2798  1
2 -59.7533 -51.3558  2
3 -59.4000 -51.9000  3

#get the map
library(ggmap)
mapImageData <- get_map(location = "Falkland Islands",
                        source = "google",  zoom = 9)

#plot the points
ggmap(mapImageData,
      extent = "panel",
      ylab = "Latitude",
      xlab = "Longitude",
      legend = "right") +
  geom_point(aes(x = lat, # path outline
                y = lon),
            data = df,
            colour = "black")  +
 labs(x = "Longitude",
   y = "Latitude") + ggtitle("Penguin Sightings") + 
  theme(plot.title = element_text(lineheight=.8, face="bold"))

enter image description here