Creating multiple station location map plots using ggmap & purrr::map

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 computing plot = purrr::map(Sta_Map, ~StaMapF).
:heavy_multiplication_x: plot must be size 9 or 1, not 4.
Backtrace:

  1. ... %>% mutate(plot = purrr::map(Sta_Map, ~StaMapF))
  2. 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.

At first glance I think this is a syntax issue. When the tilde ~ is used in purrr::map family of functions, you need to specify the variable .x. In your case though the easiest approach is to just remove the tilde. Does this work?

Sta_Map_Plots <- Sta_Map %>% dplyr::select(LAT,LON,YEAR) %>%  dplyr::group_by(YEAR) %>% tidyr::nest() %>% ungroup() %>% mutate(plot = purrr::map(Sta_Map,StaMapF))

I had tried that previously. When I did, I received the following error:

Error in mutate():
! Problem while computing plot = purrr::map(Sta_Map, StaMapF).
:information_source: The error occurred in group 1: YEAR = 2003.
Caused by error in fortify():
! data must be a <data.frame>, or an object coercible by fortify(), not a character vector.

That's the better error ;).

In that case take a look at what is being passed. It is not a data frame as what you are intending. When you tidyr::nest you are creating a column of data frames called data, so you need to pass that instead of Sta_Map. And since in your function you want to label with YEAR which is not in the new column of tibbles called data, you use map2. How is this?

# Station Map function
StaMapF <- function(z, zz){
  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 = zz)+
    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::map2(data,YEAR, StaMapF))

Thank you!! I am new to applying functions through purrr. This will be a great help to me in the future.

For the record, the first code worked:

mutate(plot = purrr::map2(data,YEAR, StaMapF))

I tried this code:

mutate(plot = StaMapF(data,YEAR))

and received the same 'forify' error

I believe this is because I have two elements: YEAR and data?

Yea I deleted the second post after realizing that! Glad to have helped!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.