2022-10-24
https://www.qgis.org/en/site/
MB_Shape = st_read(("Manitoba_map67.shp")) %>% #Read shapefile
st_transform(crs = 4326) #Transform coordinate system
Reading layer `Manitoba_map67' from data source
`/media/justin/Extension/Work Projects/Data Science/Workshops/SpatialEpi_Lecture/Manitoba_map67.shp'
using driver `ESRI Shapefile'
Simple feature collection with 67 features and 4 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -102.0123 ymin: 48.99876 xmax: -88.98969 ymax: 60.00171
Geodetic CRS: GCS_Assumed_Geographic_1
Simple feature collection with 6 features and 4 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -97.27762 ymin: 48.99875 xmax: -95.15352 ymax: 50.28392
Geodetic CRS: WGS 84
FIRST_NAME RHA NAME2 Number
1 BN2-20 Brokenhead North Eastman Brokenhead 11
2 BN5-20 Springfield North Eastman Springfield 13
3 BS1-25 Central South Eastman Central 16
4 BS2-25 Northern South Eastman Northern 17
5 BS3-25 South/East South Eastman Southern 18
6 BS4-25 Western South Eastman Western 19
geometry
1 MULTIPOLYGON (((-96.35287 5...
2 MULTIPOLYGON (((-96.70431 5...
3 MULTIPOLYGON (((-96.64578 4...
4 MULTIPOLYGON (((-96.36583 4...
5 MULTIPOLYGON (((-97.1671 49...
6 MULTIPOLYGON (((-96.98073 4...
Map = tm_shape(JoinedData) +
tm_polygons("RR",
palette = sequential_hcl(12, "reds 3", rev = T),
title = "Risk Ratio",
breaks = c(quantile(JoinedData$RR, probs = seq(0, 1, by = 0.2)))) +
tm_scale_bar(position = c("left","BOTTOM"))+ #Add a scale bar
tm_layout(title = "Example in MB", #layout options to make things pretty and annotate if necessary
legend.position = c("right","bottom"),
inner.margins = c(0.05,0.05,0.12,0.05),
frame=T,
legend.show=T,
bg.color = "white",
legend.bg.color = "white",
legend.frame = T)
WPG_Data = JoinedData %>%
filter(RHA == "Winnipeg")
WPG_Map = tm_shape(WPG_Data) +
tm_polygons("RR",
palette = sequential_hcl(12, "reds 3", rev = T),
title = "Risk Ratio",
breaks = c(quantile(JoinedData$RR, probs = seq(0, 1, by = 0.2)))) +
tm_scale_bar(position = c("left","BOTTOM"))+
tm_layout(title = "Example in WPG",
legend.position = c("right","bottom"),
inner.margins = c(0.05,0.05,0.12,0.05),
frame=F,
legend.show=F, #Hide legend here
bg.color = "white",
legend.bg.color = "white",
legend.frame = T)
pal = colorNumeric(
palette = sequential_hcl(12, "reds 3", rev=T),
domain = JoinedData$RR
)
LeafletMap = leaflet() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
setView(lng = -97.8, lat = 54.6, zoom = 5) %>%
addPolygons(data = JoinedData, weight=1, color="#000000",
opacity=0.4,
fillOpacity = 0.6, smoothFactor = 0.1,
fillColor = ~pal(JoinedData$RR)
) %>%
addLegend("bottomleft", pal = pal, values = quantile(JoinedData$RR,na.rm=T),
title = "Risk Ratio",
opacity = 1
)
- Saving a tmap with the jpeg()/tiff() functions, in the same way you would save a plot image to paste into a manuscript or report.
- Using rmarkdown to write a pdf manuscript or report. tmap’s can be called in line (similar to calling an image in latex, but using R-code rather than latex).
- html reports/vignettes/slides supports both tmap or interactive html images like leaflet!
- The flexdashboard package (https://rstudio.github.io/flexdashboard/articles/using.html), is a great way to use rmarkdown to create simple dashboards. This will support static or interactive maps.
- The Shiny package (https://shiny.rstudio.com/). This is a powerful tool to create complex dashboards and interactive data applications. The learning curve is a bit higher here, but it is well worth the investment if looking for a job outside of academia.