Maps

Animated Maps

mapcrimes.gif
require(shiny)
require(shinyBS)
require(data.table)
require(DT)
require(dataRetrieval)
require(devtools)
require(httr)
require(ggplot2)
require(ggmap)
require(stringr)
require(scales)
require(leaflet)
require(rCharts)
require(dplyr)


#get staion info and nutrient data
station <- read.csv("station.csv")
result <- read.csv("result.csv")

station2 <- dplyr::select(station, OrganizationIdentifier, StateCode, CountyCode, MonitoringLocationIdentifier, LatitudeMeasure, LongitudeMeasure)

result2 <- dplyr::select(result, OrganizationIdentifier, ActivityStartDate, ActivityMediaName, CharacteristicName, ResultMeasureValue, ResultMeasure.MeasureUnitCode, MonitoringLocationIdentifier)

#join based on monitoring site
dat <- left_join(result2, station2, by = "MonitoringLocationIdentifier")

#get map from ggmap
li <- get_map(location = 'long island', zoom = 8,
maptype = "roadmap",
color = "bw")
 
datN <- filter(dat, CharacteristicName=="Nitrate")
datN$nutrient <- "nitrate"
datN2 <- filter(dat,CharacteristicName=="Nitrite")
datN2$nutrient <- "nitrite"

datA <- filter(dat, CharacteristicName=="Ammonia")
datA$nutrient <- "Ammonia"

datA2 <- filter(dat, CharacteristicName=="Ammonium")
datA2$nutrient <- "Ammonium"

dat2 <- rbind(datN, datN2, datA, datA2)

dat2$timek <- format(as.Date(dat2$ActivityStartDate), "%Y-%m")

#create list of times used to save unique plots
times <- unique(dat2$timek)

#loop through all times, and save ggplots as jpeg


for(i in 1:length(times)){
    t <- times[i]
    m <- ggmap(li) + geom_point(data = dat2[dat2$timek == t,],
                                     mapping = aes(x = LongitudeMeasure, y = LatitudeMeasure, color= nutrient),
                                     size=3) +
    ggtitle(paste("Nutrient Measurements"), t) +
    theme(legend.position="bottom") +
    scale_colour_discrete(drop = FALSE)
    title <- paste("map",i,".jpg",sep="")
    jpeg(title)
    print(m)
    dev.off()
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s