Need help with ifelse and geocoded2 in R to make a map with different colored markers for different amphibian types

I am trying to make a map for my thesis of sampling sites of salamanders. I am super new to any of this so I don't really understand code much, but my advisor has given me a general outline of the code she has used before for something else that I am putting my stuff into. Where I am running into trouble is with the site markers for the types and their colors. I did manage to make an original map that I got to work but it only has frogs and salamanders, but I am trying to fix it to include toads and newts separately and with different colors.

Here is what I have been trying to do so far for the site markers on the map for each type of amphibian.

getColor <- ifelse(geocoded2$Type =="frog", "green")
                   ifelse(geocoded2$Type=="toad", "orange")
                   ifelse(geocoded2$Type=="salamander", "blue",
                   ifelse(geocoded2$Type=="newt", "purple"))

The error I am getting for these is:

Error in ifelse(geocoded2$Type == "frog", "green") : 
  argument "no" is missing, with no default

I got the map and legend to work, but just can't get the types to actually go through that are from my excel file Here is what I used for the map & legend:

# create the map
map <- leaflet(geocoded2) %>%
  addProviderTiles(providers$Esri.OceanBasemap) %>%
  #addTiles() %>%  # use the default base map which is OpenStreetMap tiles
  fitBounds(-165,90,165,-90)%>%
  clearBounds()%>%
  addAwesomeMarkers(~Long, ~Lat, icon=icons, label=~Type) %>%
  addLegend("topright", colors= c("green", "blue", "red", "purple"), labels=c("Frog","Salamander", "toad", "Newt"), opacity=1 )

# plot the map
map

It is kind of difficult to put your code right without access to your data - but I get the feeling that the getColor call would benefit from a rewrite from a nested ifelse() to a dplyr::case_when() - such a structure is in general easier to understand, and thus debug (both for you and random strangers on the internet).

1 Like

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.