gravatar

Daniel_Nolan

Daniel Nolan

Recently Published

Assignment 2: Option 1. Rainfall Map of Weather Stations
Introduction: This Blog highlights a interactive map of 25 weather stations across Ireland. This map displays median January rainfall levels from 1850-2014. Each weather station in the map displays the median January rainfall levels. Later in the blog I will discuss the trends found in these results. For the ease of interpretation, I have included a minimap and a legend, displaying the various bands of rainfall levels. Code used: Below I have listed the code used to create this map, and an explanation of the data used to create the map. Step 1: load('rainfall.RData') library(dplyr) rain %>% arrange(Year,Month) -> rain2 rain2 Step 2: This step allocates a filename for each weather station. library(leaflet) > library(mapview) > library(leafpop) > > stations %>% mutate(Filename=file.path(getwd(),paste0(Station,'.png'))) -> files > files %>% select(Station,Filename) Step 3: This step created the first map which I could then further embellish library(sf) library(tmap) st_read('counties.geojson',quiet=TRUE) %>% st_transform(4326) -> counties # leaflet objects must be long/lat (epsg 4326) tmap_mode('view') tm_start <- tm_shape(counties) + tm_borders() Step 4: adding further graphics to the map tmap_leaflet(tm_start) %>% addPolygons(data=counties, color = NA, popup = popupImage('test.png', embed = TRUE)) Step 5: Generating a monthplot for each station for (i in 1:nrow(files)) with(files, { png(Filename[i],width=400,height=300) local_monthplot(Station[i],rain2) dev.off()} ) Step 6: Making each month a popup files %>% mutate(Popup=paste0('<img src="',Filename,'">')) -> files files %>% select(Station,Popup) *I used the function View(files) to generate an excel spreadsheet so that I could verify the Stations were all present and correct. Step 7: Linking popups to station rain %>% group_by(Station) %>% summarise(mrain=mean(Rainfall)) %>% left_join(files) %>% select(Station,Long,Lat,mrain,Filename) %>% st_as_sf(coords=c('Long','Lat'),crs=4326) -> station_means station_means Step 8: Adding pop-ups to the map tmap_leaflet(tm_start) %>% addCircleMarkers(data=station_means, popup=popupImage( station_means$Filename, embed=TRUE)) Step 9: Further embellishment, adding month plots to each point on map, adding month mean tm_start2 <- tm_shape(station_means) + tm_dots(col='mrain',popup.vars=FALSE,scale=1.5) tmap_leaflet(tm_start2) %>% addCircleMarkers(data=station_means, stroke = NA, popup=popupImage( station_means$Filename, embed=TRUE)) Step 10: Generating Final Map using leaflet library(leaflet) > > final_map <- leaflet(station_means) %>% + addProviderTiles(providers$CartoDB.Positron) %>% + addCircleMarkers( + radius = 6, + stroke = FALSE, + fillOpacity = 0.8, + color = ~colorNumeric("viridis", jan_median)(jan_median), + popup = ~paste0( + "<strong>", Station, "</strong><br>", + "Median January rainfall: ", + round(jan_median, 1), + " mm" + ) + ) > > final_map Step 11: Adding Mini-Map ) %>% addLegend( pal = pal, values = ~jan_median, title = "Median January rainfall (mm)", opacity = 1 ) %>% addMiniMap(zoomLevelOffset = -3) Step 12: tm_start
Document