Ошибка неопределенных столбцов, выбранных с помощью plyr (d_ply) и ggplot2 - PullRequest
0 голосов
/ 02 апреля 2020

Я работаю с телеметрией животных, и у меня есть около 40 животных (DeployID) и три areas (AR,AM,AA), поэтому я хочу сделать одну карту на животное и сохранить ее. Но это не работает, я не знаю почему:

> str(df)
'data.frame':   99381 obs. of  28 variables:
 $ DeployID         : chr  "111868_16" "111868_16" "111868_16" "111868_16" ...
 $ Start            : POSIXct, format: "2016-10-24 15:44:28" "2016-10-24 15:45:46" "2016-10-24 15:46:42" "2016-10-24 15:47:24" ...
 $ End              : POSIXct, format: "2016-10-24 15:45:44" "2016-10-24 15:46:40" "2016-10-24 15:47:22" "2016-10-24 15:49:30" ...
 $ What             : Factor w/ 3 levels "Dive","Message",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Shape            : Factor w/ 4 levels "","Square","U",..: 2 4 3 2 2 2 3 3 3 2 ...
 $ DepthMean        : num  14.5 16.5 13 14.5 11 12.5 12.5 12.5 12 10.5 ...
 $ DurationMean     : num  76 54 40 126 198 62 40 48 46 124 ...
 $ DepthMin         : num  14.5 16.5 13 14.5 11 12.5 12.5 12.5 12 10.5 ...
 $ DepthMax         : num  14.5 16.5 13 14.5 11 12.5 12.5 12.5 12 10.5 ...
 $ depth_range      : Ord.factor w/ 2 levels "shallow"<"deep": 1 1 1 1 1 1 1 1 1 1 ...
 $ MidTime          : POSIXct, format: "2016-10-24 15:45:06" "2016-10-24 15:46:13" "2016-10-24 15:47:02" "2016-10-24 15:48:27" ...
 $ year             : num  2016 2016 2016 2016 2016 ...
 $ id               : chr  "111868_16" "111868_16" "111868_16" "111868_16" ...
 $ segmentid        : chr  "111868_16" "111868_16" "111868_16" "111868_16" ...
 $ mu.x             : num  -4446545 -4446557 -4446566 -4446581 -4446625 ...
 $ mu.y             : num  -2305424 -2305462 -2305490 -2305538 -2305681 ...
 $ lon              : num  -39.9 -39.9 -39.9 -39.9 -39.9 ...
 $ lat              : num  -20.4 -20.4 -20.4 -20.4 -20.4 ...
 $ lq_closest_filt  : int  7 7 7 7 7 7 7 7 7 7 ...
 $ dt_closest_filt  : num  0.0517 0.0703 0.0839 0.1075 0.1775 ...
 $ dist_closest_filt: num  0.104 0.141 0.168 0.216 0.356 ...
 $ rel.angle        : num  NA NA NA NA NA NA NA NA NA NA ...
 $ speed            : num  NA NA NA NA NA NA NA NA NA NA ...
 $ depth_bin        : Factor w/ 14 levels "(0,50]","(50,100]",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ bat              :'data.frame':  99381 obs. of  1 variable:
  ..$ depth: int  -59 -59 -59 -59 -59 -59 -59 -59 -59 -59 ...
 $ area             : chr  "AR" "AR" "AR" "AR" ...
 $ next_area        : Factor w/ 3 levels "AA","AM","AR": 2 2 2 2 2 2 2 2 2 2 ...
 $ count            : int  888 888 888 888 888 888 888 888 888 888 ...

world <- maps::map("world2", fill=TRUE, plot=FALSE) # world2 is the Pacific centered map with longitude [0,360]

IDs <- sapply(strsplit(world$names, ":"), function(x) x[1])
world <- map2SpatialPolygons(world, IDs=IDs, proj4string=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

world_map <- fortify(world)

#load the bathymetry 
bat2 <- marmap::getNOAA.bathy(lon1 = -5.0000, lon2 = -80.0000, lat1 = 14, lat2 = -70.61, keep=T) 

#converting the bathymetry in data.frame
bat2 = fortify.bathy(bat2) 

plyr::d_ply(df, ~DeployID , function(d){
  g<-ggplot(d, aes(x = lon, y = lat)) + 
    geom_path(aes(group = area, col = area), size = 1) +
    geom_map(data = world_map, map=world_map, aes(x = long, y = lat, map_id = id), fill = "black") +
    geom_contour(data = bat2, 
                 aes(x=x, y=y, z=z),
                 breaks=c(-200),
                 size=c(0.3),
                 colour="blue")+
    xlim(extendrange(d$lon, f = 0.1)) + 
    ylim(extendrange(d$lat, f = 0.1)) + 
    coord_map("ortho", orientation = c(-40, -40, 0)) +
    ggtitle(paste("Tag", d$DeployID[1], sep = " ")) 

  ggsave(plot = g, file = paste("/Users/erika/Dropbox/Érika Project/mestrado_R/bhv_loc_R/analysis/habitats/map_idarea_original/all/Map_all_", d$DeployID[1],".png", sep = ""), width = 9, height = 4, dpi=500)
})

Но показывает эту ошибку:

Ошибка в [.data.frame (col, i): неопределенные столбцы выбранный

Кто-то знает, что происходит? Спасибо!

РЕДАКТИРОВАТЬ 1

Я создал этот пример (долгота и широта не совпадают с моим фреймом данных), я не могу вставить точно строки, потому что у каждого животного более 300 локаций

DeployID     lon         lat         area 
1289         -39.94400   -20.39859   AR
1289         -40.04400   -20.59859   AR
1289         -40.94400   -21.39859   AR
1289         -42.54400   -22.49859   AM
1289         -42.94400   -22.39859   AM
1289         -43.44400   -22.59859   AM
1289         -52.94400   -24.39859   AA
2435         -39.94400   -20.39859   AR
2435         -40.04400   -20.59859   AR
2435         -40.94400   -21.39859   AR
2435         -42.54400   -22.49859   AM
2435         -42.94400   -22.39859   AM
2435         -43.44400   -22.59859   AM
2435         -52.94400   -24.39859   AA

> dput(head(df))
structure(list(DeployID = c("111868_16", "111868_16", "111868_16", 
"111868_16", "111868_16", "111868_16"), Start = structure(c(1477323868, 
1477323946, 1477324002, 1477324044, 1477324260, 1477324480), class = c("POSIXct", 
"POSIXt"), tzone = "GMT"), End = structure(c(1477323944, 1477324000, 
1477324042, 1477324170, 1477324458, 1477324542), class = c("POSIXct", 
"POSIXt"), tzone = "GMT"), What = structure(c(1L, 1L, 1L, 1L, 
1L, 1L), .Label = c("Dive", "Message", "Surface"), class = "factor"), 
    Shape = structure(c(2L, 4L, 3L, 2L, 2L, 2L), .Label = c("", 
    "Square", "U", "V"), class = "factor"), DepthMean = c(14.5, 
    16.5, 13, 14.5, 11, 12.5), DurationMean = c(76, 54, 40, 126, 
    198, 62), DepthMin = c(14.5, 16.5, 13, 14.5, 11, 12.5), DepthMax = c(14.5, 
    16.5, 13, 14.5, 11, 12.5), depth_range = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("shallow", "deep"), class = c("ordered", 
    "factor")), MidTime = structure(c(1477323906, 1477323973, 
    1477324022, 1477324107, 1477324359, 1477324511), class = c("POSIXct", 
    "POSIXt"), tzone = "GMT"), year = c(2016, 2016, 2016, 2016, 
    2016, 2016), id = c("111868_16", "111868_16", "111868_16", 
    "111868_16", "111868_16", "111868_16"), segmentid = c("111868_16", 
    "111868_16", "111868_16", "111868_16", "111868_16", "111868_16"
    ), mu.x = c(-4446545.25191192, -4446557.10576816, -4446565.77504969, 
    -4446580.81370994, -4446625.40007808, -4446652.29459533), 
    mu.y = c(-2305423.86124176, -2305461.88537725, -2305489.69364377, 
    -2305537.93137917, -2305680.93056743, -2305767.17264774), 
    lon = c(-39.9439956132156, -39.944102098218, -39.944179975699, 
    -39.9443150702825, -39.9447155964422, -39.9449571940013), 
    lat = c(-20.3985940756941, -20.3989161274532, -20.3991516537744, 
    -20.3995602097098, -20.4007713539709, -20.4015017842338), 
    lq_closest_filt = c(7L, 7L, 7L, 7L, 7L, 7L), dt_closest_filt = c(0.0516666666666667, 
    0.0702777777777778, 0.0838888888888889, 0.1075, 0.1775, 0.219722222222222
    ), dist_closest_filt = c(0.103680210832692, 0.141026573116106, 
    0.168339162761167, 0.215717097671267, 0.356168027785347, 
    0.440874049523752), rel.angle = c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), speed = c(NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), depth_bin = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("(0,50]", "(50,100]", "(100,150]", 
    "(150,200]", "(200,250]", "(250,300]", "(300,350]", "(350,400]", 
    "(400,450]", "(450,500]", "(500,550]", "(550,600]", "(600,650]", 
    "(650,700]"), class = "factor"), bat = structure(list(depth = c(-59L, 
    -59L, -59L, -59L, -59L, -59L)), row.names = c(NA, 6L), class = "data.frame"), 
    area = c("AR", "AR", "AR", "AR", "AR", "AR")), row.names = c(NA, 
6L), class = "data.frame") 

#and this is the way that I do out of function

ggplot(df[df$DeployID== "87780_17",], aes(x = lon, y = lat)) + 
  geom_path(aes(group = area, col = area), size = 1) +
  geom_map(data = world_map, map=world_map, aes(x = long, y = lat, map_id = id), fill = "black") +
  geom_contour(data = bat2, 
               aes(x=x, y=y, z=z),
               breaks=c(-200),
               size=c(0.3),
               colour="blue")+
  xlim(extendrange(df$lon, f = 0.1)) + 
  ylim(extendrange(df$lat, f = 0.1)) + 
  coord_map("ortho", orientation = c(-40, -40, 0)) 
...