I have a data set of station location data by year for 9 years of data: odd years from 2003 - 2019. My objective is to create station location maps for each year individually using ggmap & purrr::map.
Here's head() of the data:
head(Sta_Map)
HAUL_ID LAT LON YEAR
1 7MF03 37 2 ANCHO 0 56.03300 -156.9937 2003
2 7MF03 9 2 ANCHO 0 55.47033 -159.1238 2003
3 7MF03 15 2 ANCHO 0 55.81473 -158.4438 2003
4 7MF03 32 2 ANCHO 0 56.14533 -157.7527 2003
5 7MF03 51 2 ANCHO 0 56.70183 -156.5823 2003
6 7MF03 52 2 ANCHO 0 56.42300 -156.2093 2003
Here, "Haul_ID" won't be used, just LAT, LON, and YEAR.
First, I created a bounding box and the underlying map information for the base map for all the data. The survey region is in the same location year to year, so all years can share the same base map. Next is a function I created to generate each plot. I then used dplyr::select to used on LAT,LON, and YEAR, dplyr::group_by to group by YEAR, then finally attempted to use tidyr::nest and purrr::map to apply the function to each YEAR of data:
# Nest Data by Year, apply station mapping function, then plot all maps via ggarrange
# Create Bounding Box for survey area. Only need one as Survey Grid is in Same region for all surveys.
bbox <- make_bbox(Sta_Map$LON,Sta_Map$LAT)
#Get Background Map: stamen, toner
MapInfo <- get_map(bbox,maptype="toner-lite", source="stamen")
# Station Map function
StaMapF <- function(z){ggmap(MapInfo)+ #plot base layer map
geom_point(data=z, mapping= aes(x=LON, y=LAT), colour="black")+
xlab("Longitude (W)")+
ylab("Latitude (N)")+
labs(title = paste(unique(z$YEAR)))+
theme(axis.text.x=element_text(face="bold"), axis.text.y = element_text(face="bold"))}
Sta_Map_Plots <- Sta_Map %>% dplyr::select(LAT,LON,YEAR) %>% dplyr::group_by(YEAR) %>% tidyr::nest() %>% ungroup() %>% mutate(plot = purrr::map(Sta_Map,~StaMapF))
From this I have received the following error:
Error in
mutate()
:
! Problem while computingplot = purrr::map(Sta_Map, ~StaMapF)
.
plot
must be size 9 or 1, not 4.
Backtrace:
- ... %>% mutate(plot = purrr::map(Sta_Map, ~StaMapF))
- dplyr:::mutate.data.frame(., plot = purrr::map(Sta_Map, ~StaMapF))
This is has close as I've gotten it to work, as it understands that I have 9 years of data. Any help would be appreciated. Thanks.