I want to write a code to read data from an excel file and plot the points on a map.
I want the color of the marker to be determined based on the ship type.
library(leaflet)
library(readxl)
Read the Excel file
data <- read_excel("/Users/jevondixon/Downloads/Book1.xlsx")
Define a function to color the markers based on ship type
color_by_shiptype <- function(ShipType) {
if (ShipType == "Product tanker") {
return("red")
} else if (ShipType == "Container ship") {
return("blue")
} else if (ShipType == "Fishing vessel") {
return("green")
} else if (ShipType == "Chemical tanker") {
return("orange")
} else {
return("black")
}
}
Create a leaflet map object
map <- leaflet() %>% addTiles()
Add markers to the map based on the data from Excel sheet
for (i in 1:nrow(data)) {
map <- addCircleMarkers(map,
lat = data$Latitude[i],
lng = data$Longitude[i],
popup = paste(data$Name[i], ": ", data$Date[i], data$ShipType[i]),
color = ~color_by_shiptype(data$ShipType[i]))
}
Display the map
map