Tableau and R are usefull to visualize geospatial data. R provides several packages/functions to visulize geospatial data:
In this post I will compare leaflet and Tableau using two projects I did.
Usually when I try build interative geospatial visulization, I will combine both Shiny and leaflet.
What is Leaflet?
Leaflet is a R package that allows you to build interative map without strong knowledge in javascript and CSS.
The best way to present geospatial data is using both shiny and leaflet. Here is the project I did using leaflet and shiny: https://mingzhong.shinyapps.io/hospital_intelligence/
Pretty cool, right? Let’s start with several simple examples and get the gist of leaflet.
Installation
#install.packages("leaflet")
# to install the development version from Github, run
# devtools::install_github("rstudio/leaflet")
Basic Example
library(leaflet)
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")# add a marker in map
m # Print the map
You create a Leaflet map with these basic steps:
1.Create a map widget by calling leaflet().
2.Add layers (i.e., features) to the map by using layer functions (e.g. addTiles, addMarkers, addPolygons) to modify the map widget.
3.Repeat step 2 as desired.
4.Print the map widget to display it.
Read in Geospatial Data
Both leaflet() and the map layer functions have an optional data parameter that is designed to receive spatial data in one of several forms:
From base R: +lng/lat matrix +data frame with lng/lat columns From the sp package: +SpatialPoints[DataFrame] +Line/Lines +SpatialLines[DataFrame] +Polygon/Polygons +SpatialPolygons[DataFrame] *From the maps package: +the data frame from returned from map()
Here is an example receiving spatial data from library(maps)
library(maps)
mapStates = map("state", fill = TRUE, plot = FALSE)
leaflet(data = mapStates) %>% addTiles() %>%
addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE)
Change Maps
You can even select the customized map style. There are different kinds of style because of the open source characteristic of Leaflet.
m <- leaflet() %>% setView(lng = -71.0589, lat = 42.3601, zoom = 12)
m %>% addProviderTiles(providers$Stamen.Toner)
Shiny Integration
Finally, we want to inegrate Shiny with Leaflet. By building UI and SERVER, we can actually put leaflet map inside the R shiny app.
library(shiny)
library(leaflet)
r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()
shinyApp(
ui <- fluidPage(
leafletOutput("mymap"),
p(),
actionButton("recalc", "New points")
),
server <- function(input, output, session) {
points <- eventReactive(input$recalc, {
cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
}, ignoreNULL = FALSE)
output$mymap <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$Stamen.TonerLite,
options = providerTileOptions(noWrap = TRUE)
) %>%
addMarkers(data = points())
})
},options = list(height = 500)
)
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
We can even go further and add in some button to interact with map. Besides, we can even combine other kinds of graphs to make our data presentation even better !
What is Tableau
Tableau is a powerful softare that is intuitive and helps visualize complex datasets with ease without writing any code. If you wish to analyze your data geographically, Tableau is a clear choice since it is easy to use and can clearly highlight trends or patterns in your data
Installation
Tableau can be used online or a desktop version can be downloaded and used within the trial period.
Basic Example
In this example we used the city level data from https://simplemaps.com/data/us-cities This dataset consists of all US cities, their population, their coordinates and a few other fields.
For building a basic map, one needs to add longitudes as columns and latitudes as rows from Measures.
Once a basic map is built we can start visualizing patterns by adding Marks. The map shows below has the following Marks:
Detail: State Name
Size: Population
We can also color states by population by adding the population Measure to Marks, as shown below -
Proportional symbol maps
If one wants to visualize quantitative data proportional symbol maps are a good choice. We use the Earthquake dataset included in Create Proportional Symbol Maps in Tableau Example Workbook on Tableau Public. The steps for building a proportional symbols map are as follows:
Add following Marks: - Detail: ID (from Dimensions), Size: Magnitude^10 (from Measures).
This gives the following basic proportional symbol map. The larger data points represent earthquakes with larger magnitudes, and the smaller data points represent earthquakes with smaller magnitudes.
You can play around with Marks to add Color (it is also possible to change color in Edit Colors dialog box). The following plot was generated from experimenting with the workbook on Tableau Public - https://onlinehelp.tableau.com/current/pro/desktop/en-us/maps_howto_symbol.htm
It shows dark orange data points which represent earthquakes with higher magnitudes, while the dark blue data points represent earthquakes with lower magnitudes.
Heatmaps
Another way we can visualize the city dataset is by using heatmaps to show concentrations of cities in the US. This can be done easily by changing the Marks setting from Automatic to Density which will generate a heatmap. The following plot shows the heatmap of cities in the US. The darker portions represent a higher density of cities and vice versa for lighter portions.
Tableau can be used to build Flow Maps, Spider Maps, Point distribution maps and many other types of visualizations. The type of map used depends on the dataset.
Although Tableau can be used without any coding experience, if one wants to integrate Python or R with Tableau, they have the option to do so. Instructions and examples on how to do so can be found on the following links:
Tableau
All in all, leaflet and Tableau are both great tool to visualize geospatial. We would like to choose different tools based on the condition. For simplicity and convenience, Tableau is for sure the best visualization tool. You can easily visualize data without any code. Anyone can learn Tableau within a short time.
Leaflet
Although Tableau is easy to use and learn, leaflet is more preferable when we need to costomize your data presentation. Because of Shiny, we are able to combine Leaflet with other functions in R. if we would like to build a demo data visulization app using R, leaflet will be our top choice.
Both Tableau and Leaflet+Shiny can be shared using a link. That helps a lot when you try to share your work.
References
https://rstudio.github.io/leaflet/shiny.html
https://simplemaps.com/data/us-cities
https://onlinehelp.tableau.com/current/pro/desktop/en-us/maps_howto_symbol.htm