Learn R Programming

twitterreport (version 0.15.11)

tw_leaflet: Creates a leaflet map

Description

This function works as a wrapper of the leaflet function, creating a map with circles which sizes are set by the number of observations in those coordinates.

Usage

tw_leaflet(dat, coordinates = NULL, popup = NULL, lat = NULL, lng = NULL, radii = ~sqrt(n) * 1000, weight = 1, cluster.method = "centroid", nclusters = min(c(50, nrow(dat)%/%3)), ...)

Arguments

dat
A twitter dataset (with coordinates)
coordinates
Name of the coordinates variable (see details)
popup
Name of the grouping variable (for example, screen_name)
lat
Name of the latitude variable
lng
Name of the longitude variable
radii
Formula to compute the radious of the circles
weight
Thickness of the circles' borders
cluster.method
Clustering method (see hclust)
nclusters
Max number of clusters to include
...
Further arguments to be passed to addCircles

Value

A map object (see the leaflet package)

Details

The coordinates must be in the format of longitude:latitude (as the twitter API returns).

In order to improve visualization, the function performs Hierarchical Clustering via hclust (from the stats package), grouping observations by geo coordinates. For each cluster, the final lat/lng coords are defined as the mean within the cluster.

In the case of radii, it is measured in meters (in the map). The n is the number of observations in that cluster.

For performance considerations, it is recommended not to use more than 1,000 observations (Try using a random sample from your data!) as hclust requires computing square matrices.

Examples

Run this code
## Not run: 
# # Getting the data
# data(senate_tweets)
# 
# # using formulas to pass the variables names
# tw_leaflet(senate_tweets,~coordinates,~screen_name)
# tw_leaflet(senate_tweets,~coordinates)
# 
# # Using characters to pass the variables names
# tw_leaflet(senate_tweets,'coordinates','screen_name')
# tw_leaflet(senate_tweets,'coordinates')
# 
# # Aggregating until get only 3 big groups
# tw_leaflet(senate_tweets,'coordinates', nclusters=3)
# ## End(Not run)

Run the code above in your browser using DataLab