2017-02-20 18 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」)) –