lundi 3 avril 2017

How to run if statement on random markers in R leaflet?

I'am looking to run an if statement on some random markers plotted on a leaflet map, The if statement will check if certain conditions are true in regards to the random markers and if the condition is true have the markers change color to 'green' or if false then turn 'red':

Leaflet Map

For example, as shown above in the leaflet map. When the random markers are plotted, an if statement will check if the area that each random marker landed in has a population greater than 300 and doesn't fall into one of the distance radius's of the centres(Transparent circles on map). If this condition is met, change the marker color to 'green' otherwise turn 'red'. My entire r code is below and can be run and tested if you wish.

R code:

#Libraries
library(rgdal)
library(sp)
library(rgeos)
library(maptools)
library(ggplot2)
library(plyr)
library(leaflet)

#Download, Unzip and Read in shapefile
download.file("http://ift.tt/2oRGo26","Census2011_Small_Areas_generalised20m.zip")
unzip("Census2011_Small_Areas_generalised20m.zip")
data <- readOGR(".", "Census2011_Small_Areas_generalised20m")
View(data)

#Subset population shapefile to show only Leinster
newdata <- subset(data, data$COUNTYNAME=='Dun Laoghaire-Rathdown' | data$COUNTYNAME=='South Dublin' | data$COUNTYNAME=='Dublin City' | data$COUNTYNAME=='Carlow County' | data$COUNTYNAME=='Dublin County' |
                    data$COUNTYNAME=='Kildare County' | data$COUNTYNAME=='Kilkenny County' |
                    data$COUNTYNAME=='Laois County' | data$COUNTYNAME=='Longford County' |
                    data$COUNTYNAME=='Louth County' | data$COUNTYNAME=='Meath County' |
                    data$COUNTYNAME=='Offaly County' | data$COUNTYNAME=='Westmeath County' |
                    data$COUNTYNAME=='Wexford County' | data$COUNTYNAME=='Wicklow County' |
                    data$COUNTYNAME=='Fingal')

shapeData <- spTransform(newdata, CRS("+proj=longlat +datum=WGS84 +no_defs"))

#Read in centre dataset
centres <- read.csv(url("http://ift.tt/2nU70BG"))

#convert data into a data-frame
data.frame(centres)

#check data
centres

#subset healthcentre data to show only Leinster
leinster <- subset(centres, County =="Wicklow" | County =="Wexford" | County =="Dublin" | County =="Kildare" 
                   | County =="Meath" | County =="Carlow" | County =="Kilkenny" 
                   | County =="Laois" | County =="Offaly" | County =="Westmeath" 
                   | County =="Longford" | County =="Louth")

#Create data frame
data = data.frame(
  longitude = as.numeric(leinster$long_xcord),
  latitude = as.numeric(leinster$lat_ycord)
)

#Create a pop ups for the map
map_popup <- paste0("<strong>HSE Support Centre: </strong>",
                    leinster$Service.name,
                    "<br><strong>Services Provided: </strong>", 
                    leinster$Service.name, 
                    "<br><strong>Address: </strong>", 
                    leinster$Address)

map_popup2 <- paste0("<strong> Area Name: </strong>",
                     shapeData$EDNAME,
                     "<br><strong>Population: </strong>", 
                     shapeData$TOTAL2011)

heat <- colorNumeric(
  c("Yellow", "Red"),
  domain = shapeData$TOTAL2011)


#Create Leaflet map
map <- leaflet()  %>% addTiles() %>% 
  setView(lng = -7.1050, lat=53.2000,zoom=8) %>% 

  addPolygons(data=shapeData,weight=1, popup = map_popup2, fillOpacity = 0.5, 
              color = ~colorQuantile("YlOrRd", shapeData$TOTAL2011)(TOTAL2011)) %>% 

  addCircleMarkers(data = leinster, lng = ~long_xcord, 
                   lat = ~lat_ycord, radius=.5, color="green") %>%

  #PLOT 5 RANDOM POINTS WITHIN LEINSTER ON MAP HERE.....
  addMarkers(runif(3, -7.1050, -6.1050), runif(3, 53.2000, 54.2000), group = "Markers") %>%

  addCircles(data = leinster, lng = ~long_xcord, 
             lat = ~lat_ycord, popup = map_popup,
             radius = 4828.03, fillOpacity = 0.1, #3218.69 = 2 miles
             color = 'black', fillColor = 'blue',weight = 2, label=leinster$`Service name`) %>%


  #Print the map
  print(map)

Aucun commentaire:

Enregistrer un commentaire