Overview of function

geo_bubble_chart can be used to create the following kinds of charts:

Geo bubble charts contain circle that highlight important geographic positions, and the radius of this circle markers communicates additional information about these locations. Note that these visualisations can be become difficult to interpet where there are high point densities. For charts where you only wish to communicate the geographic locations of certain points, refer to geo_marker_plot instead.

As with all oidnChaRts libraries, you are advised to load the htmlwidget library you're using directly.

data_stacked_bar_chart

library(oidnChaRts)

This vignette covers the use of geo bubble charts for visualising data with a variety of htmlwidget libraries, for demonstration purposes we use the following dataset generated from https://doi.org/10.6084/m9.figshare.4516772.

head(data_geo_bubble_chart)

The data is generated from a historical analysis of the letters sent between German migrants and their families between the 18th and 19th Centuries, the columns may be summarised as follows:

Geo bubble chart specifications

Geo bubble charts provide multiple channels of communication:

There is significant variation in the counts column of the sample dataset:

library(tidyverse)
data_geo_bubble_chart %>%
  ggplot(aes(x = "", count)) + geom_violin() + coord_flip()

To improve the appearance of the chart, we will use the tidyverse to filter out points below the 50th quartile and use the scales library to rescale the counts from 3 to 20:

library(scales)
data_geo_bubble_chart %>%
  filter(count > 5) %>%
  mutate(count = rescale(count, to = c(3,20))) %>%
  ggplot(aes(x = "", count)) + geom_violin() + coord_flip()

Additionally, colour groupings will be added to the dataset as follows:

data_geo_bubble_chart %>%
  mutate(color = plyr::mapvalues(
    country,
    from = unique(country),
    to = RColorBrewer::brewer.pal(length(unique(country)), "Paired")
  ))

Geo bubble chart

The following will create a generic geo bubble chart with leaflet, note that significant work is required to make legends in leaflet charts "active" and you are recommended to build such charts from scratch.

library(leaflet)
data_geo_bubble_chart %>%
  filter(count > 5) %>%
  mutate(count = rescale(count, to = c(3,20))) %>%
  mutate(color = plyr::mapvalues(
    country,
    from = unique(country),
    to = RColorBrewer::brewer.pal(length(unique(country)), "Dark2")
  )) %>%
  geo_bubble_chart(
    library = "leaflet",
    bubble.radius = ~count,
    bubble.group = ~country,
    color = ~color
  )

The following will create a generic highchart geo bubble chart, note the addition of hc_mapNavigation to enable zooming.

library(highcharter)
data_geo_bubble_chart %>%
  filter(count > 5) %>%
  mutate(count = rescale(count, to = c(3,20))) %>%
  mutate(color = plyr::mapvalues(
    country,
    from = unique(country),
    to = RColorBrewer::brewer.pal(length(unique(country)), "Dark2")
  )) %>%
  geo_bubble_chart(
    library = "highcharter",
    bubble.radius = ~count,
    bubble.group = ~country
  ) %>%
    hc_mapNavigation(enabled = TRUE)

This will create a generic geo bubble chart using plotly, note that the legend items automatically scale based on the largest size bubble in the series.

library(plotly)
data_geo_bubble_chart %>%
  filter(count > 5) %>%
  mutate(count = rescale(count, to = c(3,20))) %>%
  mutate(color = plyr::mapvalues(
    country,
    from = unique(country),
    to = RColorBrewer::brewer.pal(length(unique(country)), "Dark2")
  )) %>%
  geo_bubble_chart(
    library = "plotly",
    bubble.radius = ~count,
    bubble.group = ~country
  )

Mouseover text

Mouseover text is supported in the leaflet library as follows, note that arbitrary information can easily be included in the text.

data_geo_bubble_chart %>%
  filter(count > 5) %>%
  mutate(count = rescale(count, to = c(3,20))) %>%
  mutate(color = plyr::mapvalues(
    country,
    from = unique(country),
    to = RColorBrewer::brewer.pal(length(unique(country)), "Dark2")
  )) %>%
  geo_bubble_chart(
    library = "leaflet",
    bubble.radius = ~count,
    bubble.group = ~country,
    mouseover.text = ~paste("Country:", country)
  )

It is easiest to add custom mouseover/tooltip content to highcharter charts using the hc_tooltip function directly:

data_geo_bubble_chart %>%
  filter(count > 5) %>%
  mutate(count = rescale(count, to = c(3,20))) %>%
  mutate(color = plyr::mapvalues(
    country,
    from = unique(country),
    to = RColorBrewer::brewer.pal(length(unique(country)), "Dark2")
  )) %>%
  geo_bubble_chart(
    library = "highcharter",
    bubble.radius = ~count,
    bubble.group = ~country
  ) %>%
    hc_mapNavigation(enabled = TRUE) %>%
  hc_tooltip(
        formatter = JS(
          "function(){
console.log(this)
          return '<b>Country: </b>' + this.series.name + 
                  '<br/>' +
                  '<b>City: </b>' + this.point.city +
                  '<br/>' +
                  '<b>Latitude: </b>' + this.point.lat +
                  '<br/>' +
                  '<b>Longitude: </b>' + this.point.lon;}"
          )
        )

Thoroughly customising the content of plotly tooltips is difficult, but it is possible to add additional information to mouseovers as with the mouseover.text argument:

data_geo_bubble_chart %>%
  filter(count > 5) %>%
  mutate(count = rescale(count, to = c(3,20))) %>%
  mutate(color = plyr::mapvalues(
    country,
    from = unique(country),
    to = RColorBrewer::brewer.pal(length(unique(country)), "Dark2")
  )) %>%
  geo_bubble_chart(
    library = "plotly",
    bubble.radius = ~count,
    bubble.group = ~country,
    mouseover.text = ~paste("City:", city, "<br>",
                            "Country:", country)
  )

Popup text

The leaflet library supports "popup text", content shown when the bubble is selected. The other libraries available for geo_bubble_chart do not support this without using Shiny or crosstalk.

data_geo_bubble_chart %>%
  filter(count > 5) %>%
  mutate(count = rescale(count, to = c(3,20))) %>%
  mutate(color = plyr::mapvalues(
    country,
    from = unique(country),
    to = RColorBrewer::brewer.pal(length(unique(country)), "Dark2")
  )) %>%
  geo_bubble_chart(library = "leaflet",
    bubble.radius = ~count,
    bubble.group = ~country,
                  mouseover.text = ~city,
                  popup.text = ~paste("Country:",country, "<br>", "City:", city))

Alternative base maps (projections, etc)

The leaflet library has a very flexible system for modifying the base maps and projections in visualisations, and this can be done "after the fact", i.e. the geo_bubble_chart can be used to generate the template map and other base maps applied.

data_geo_bubble_chart %>%
  filter(count > 5) %>%
  mutate(count = rescale(count, to = c(3,20))) %>%
  mutate(color = plyr::mapvalues(
    country,
    from = unique(country),
    to = RColorBrewer::brewer.pal(length(unique(country)), "Dark2")
  )) %>%
  geo_bubble_chart(
    library = "leaflet",
    bubble.radius = ~count,
    bubble.group = ~country
  ) %>%
  addProviderTiles(providers$Esri.NatGeoWorldMap)

The highcharter library requires the base map and other details to be specified in the original function call, the appriopriate documentation to refer to is here: http://jkunst.com/highcharter/highmaps.html

The plotly library also requires base maps and other geographic details to be specified in the original function call.



martinjhnhadley/oidnChaRts documentation built on May 21, 2019, 12:38 p.m.