2017-02-20 15 views
-1

我刚刚从福克兰岛得到了我的位置数据,并且试图映射从标签获取的位置,但是,当我运行此代码时r停止工作。从数据框中映射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

我们通常要求可重复的问题,即我们需要一个可以很容易地在我们的电脑上复制数据,所以表的图像通常不会有帮助。但是,从您的图像我看到,第一行不是数据,而是列标签。尝试删除第一行,或使用'read.csv()'中的'header = T'作为列名。 –

+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

刚使用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

+0

为什么downvote? –

+0

非常感谢您的帮助!添加到矩阵的所有数据从标签,当我尝试运行ggmap我有下一个消息 - > #错误:GeomRasterAnn与ggproto不兼容的版本构建。请重新安装提供此扩展名的软件包 我仍在尝试修复它 –

+0

我仍然无法创作剧情,这是我的剧本 > gentoo <-read.csv(“Regularized Gentoos.csv”,header = T ) mapImageData < - get_map(位置= “福克兰群岛”,源= “谷歌”,缩放= 9) ggmap(mapImageData, 程度= “面板”, ylab = “纬度”, xlab = “经度”, 图例= “右”)+ geom_point(AES(X = LAT,#路径轮廓 Y = LON), 数据=巴布亚, 颜色= “黑色”)+ 实验室(X = “经度”, y =“Latitude”)+ ggtitle(“Penguin Sightings”)+ theme(plot.title = element_text(lineheight = .8,face =“bold”)) –