gvisGeoChart: Google Geo Chart with R

Description Usage Arguments Value Note Author(s) References See Also Examples

View source: R/gvisGeoChart.R

Description

The gvisGeoChart function reads a data.frame and creates text output referring to the Google Visualisation API, which can be included into a web page, or as a stand-alone page.

A geo chart is a map of a country, a continent, or a region with two modes: The region mode colorizes whole regions, such as countries, provinces, or states. The marker mode marks designated regions using bubbles that are scaled according to a value that you specify.

A geo chart is rendered within the browser using SVG or VML. Note that the map is not scrollable or draggable.

Usage

1
2
gvisGeoChart(data, locationvar = "", colorvar="", 
             sizevar="", hovervar="", options = list(), chartid)

Arguments

data

a data.frame. The data has to have at least one column with location name (locationvar), value to be mapped to location. The format of the data varies depending on which display mode that you use: Regions or Markers.

locationvar

column name of data with the geo locations to be analysed. The locations can be provide in two formats:

Format 1

'latitude:longitude'. See the example below.

Format 2

Address, country name, region name locations, or US metropolitan area codes, see http://code.google.com/apis/adwords/docs/developer/adwords_api_us_metros.html. This format works with the dataMode option set to either 'markers' or 'regions'. The following formats are accepted: A specific address (for example, "1600 Pennsylvania Ave"). A country name as a string (for example, "England"), or an uppercase ISO-3166 code or its English text equivalent (for example, "GB" or "United Kingdom"). An uppercase ISO-3166-2 region code name or its English text equivalent (for example, "US-NJ" or "New Jersey").

colorvar

column name of data with the optional numeric column used to assign a color to this marker, based on the scale specified in the colorAxis.colors property. If this column is not present, all markers will be the same color. If the column is present, null values are not allowed. Values are scaled relative to each other, or absolutely to values specified in the colorAxis.values property.

sizevar

only used for displayMode='markers'. Column name of data with the optional numeric column used to assign the marker size, relative to the other marker sizes. If this column is not present, the value from the previous column will be used (or default 'size, if no color column is provided as well). If the column is present, null valuesare not allowed.

hovervar

column name of data with the additional string text displayed when the user hovers over this region.

options

list of configuration options for Google Geo Chart.

gvis.editor

a character label for an on-page button which opens an in-page dialog box that enables users to edit, change and customise the chart. By default no value is given and therefore no button is displayed.

Further possible components are, taken from https://google-developers.appspot.com/chart/interactive/docs/gallery/geochart.html#Configuration_Options:

backgroundColor

a string or object. Default white. The background color for the main area of the chart. Can be either a simple HTML color string, for example: 'red' or '#00cc00', or an object with the following properties.

backgroundColor.fill

a string. Default white. The chart fill color, as an HTML color string.

backgroundColor.stroke

a string. Default '#666'. The color of the chart border, as an HTML color string.

backgroundColor.strokeWidth

a number. Default 0. The border width, in pixels.

colorAxis

a string. Default 'null' An object that specifies a mapping between colors and color column values. To specify properties of this object, you can use object literal notation, as shown here:

{minValue: 0,  colors: ['#FF0000', '#00FF00']}
colorAxis.minValue

a number. Default minimum value of color column in chart data. If present, specifies a minimum value for chart color data. Color data values of this value and lower will be rendered as the first color in the colorAxis.colors range.

colorAxis.maxValue

a number. Default maximum value of color column in chart data If present, specifies a maximum value for chart color data. Color data values of this value and higher will be rendered as the last color in the colorAxis.colors range.

colorAxis.values

a JSON array of numbers. Default 'null'. Controls how values are associated with colors. Each value is associated with the corresponding color in the colorAxis.colors array. These values apply to the color value for a region or marker. Regions are colored according to a gradient of the values specified here. Not specifying a value for this option is equivalent to specifying [minValue, maxValue].

colorAxis.colors

a JSON array of color strings. Default 'null'. Colors to assign to values in the visualization. An array of strings, where each element is an HTML color string, for example: colorAxis:

{colors:['red','#004411']}.

You must have at least two values; the gradient will include all your values, plus calculated intermediary values, with the first color as the smallest value, and the last color as the highest.

datalessRegionColor

a string. Default 'F5F5F5'. Colors to assign to regions with no associated data.

displayMode

a string. Default 'auto'. Which type of map this is. The DataTable format must match the value specified. The following values are supported:

'auto': Choose based on the format of the DataTable. 'regions': This is a region map 'markers': This is a marker map.

enableRegionInteractivity

boolean. Default automatic. If true, enable region interactivity, including focus and tool-tip elaboration on mouse hover, and region selection and firing of regionClick and select events on mouse click.

The default is TRUE in region mode, and FALSE in marker mode.

height

number. The default height is 347 pixels, unless the width option is specified and keepAspectRatio is set to true - in which case the height is calculated accordingly.

keepAspectRatio

boolean. Default TRUE. If true, the map will be drawn at the largest size that can fit inside the chart area at its natural aspect ratio. If only one of the width and height options is specified, the other one will be calculated according to the aspect ratio.

If false, the map will be stretched to the exact size of the chart as specified by the width and height options.

legend

a JSON object / 'none'. Default 'null'. An object with members to configure various aspects of the legend, or 'none', if no legend should appear. To specify properties of this object, you can use object literal notation, as shown here:

{textStyle: {color: 'blue', fontSize: 16}}
legend.numberFormat

a string. Default 'auto'. A format string for numeric axis labels. This is a subset of the ICU pattern set. For instance,

{numberFormat:'.##'}.

will display values "10.66", "10.6", and "10.0" for values 10.666, 10.6, and 10.

legend.textStyle

a JSON object. Default

{color:
	  'black', fontName: <global-font-name>, fontSize:
	  <global-font-size>}.

An object that specifies the legend text style. The object has this format:

{color: <string>, fontName: <string>, fontSize: <number>}

The color can be any HTML color string, for example: 'red' or '#00cc00'. Also see fontName and fontSize.

region

string, default 'world'. The area to display on the map. (Surrounding areas will be displayed as well.) Can be one of the following:

  • 'world' A map of the entire world.

  • A continent or a sub-continent, specified by its 3-digit code, e.g., '011' for Western Africa.

  • A country, specified by its ISO 3166-1 alpha-2 code, e.g., 'AU' for Australia.

  • A state in the United States, specified by its ISO 3166-2:US code, e.g., 'US-AL' for Alabama. Note that the resolution option must beset to either 'provinces' or 'metros'.

For more information see: http://code.google.com/apis/chart/interactive/docs/gallery/geochart.html#Configuration_Options

magnifyingGlass

an object. Default

{enable: true, zoomFactor: 5.0}

An object with members to configure various aspects of the magnifying glass. To specify properties of this object, you can use object literal notation, as shown here:

{enable: true, zoomFactor: 7.5}
magnifyingGlass.enable

boolean. Default TRUE. If true, when the user lingers over a cluttered marker, a magnifiying glass will be opened.

Note: this feature is not supported in browsers that do not support SVG, i.e. Internet Explorer version 8 or earlier.

magnifyingGlass.zoomFactor

a number. Default 5.0. The zoom factor of the magnifying glass. Can be any number greater than 0.

markerOpacity

number, between 0.0 - 1.0. Default 1.0.

resolution

a string. Default 'countries' The resolution of the map borders. Choose one of the following values:

'countries'
'provinces'

Not supported for all countries; please test a country to see whether this option is supported.

'metros'

Supported for the US country region and US state regions only.

sizeAxis

a JSON object. Default 'null'. An object with members to configure how values are associated with bubble size. To specify properties of this object, you can use object literal notation, as shown here:

{minValue: 0,  maxSize: 20}
sizeAxis.maxSize

a number. Default 30. Maximum size of the largest marker, in pixels.

sizeAxis.maxValue

a number. Default maximum value of size column in chart data Maximum size column value. Larger values will be cropped to this value.

sizeAxis.minSize

a number. Default 2. Mininum size of the smallest marker, in pixels.

sizeAxis.minValue

a number. Default minimum value of size column in chart data Minimum size column value. Smaller values will be clamped to this value.

tooltip

a JSON object. Default 'null'. An object with members to configure various tooltip elements. To specify properties of this object, you can use object literal notation, as shown here:

{textStyle: {color: '#FF0000'}, showColorCode: true}
tooltip.textStyle

a JSON object. Default

{color:
	  'black', fontName: <global-font-name>, fontSize:
	  <global-font-size>}

An object that specifies the tooltip text style. The object has this format:

{color: <string>, fontName: <string>, fontSize: <number>}

The color can be any HTML color string, for example: 'red' or '#00cc00'.

tooltip.trigger

a string. Default 'focus'. The user interaction that causes the tooltip to be displayed:

'focus'

The tooltip will be displayed when the user hovers over an element.

'none'

The tooltip will not be displayed.

width

number, default 556. Width of the visualization. If no units are given, the default unit is pixels.

chartid

character. If missing (default) a random chart id will be generated based on chart type and tempfile

Value

gvisGeoChart returns list of class "gvis" and "list".

An object of class "gvis" is a list containing at least the following components:

type

Google visualisation type, here 'GeoChart'

chartid

character id of the chart object. Unique chart ids are required to place several charts on the same page.

html

a list with the building blocks for a page

header

a character string of a html page header: <html>...<body>,

chart

a named character vector of the chart's building blocks:

jsHeader

Opening <script> tag and reference to Google's JavaScript library.

jsData

JavaScript function defining the input data as a JSON object.

jsDrawChart

JavaScript function combing the data with the visualisation API and user options.

jsDisplayChart

JavaScript function calling the handler to display the chart.

jsFooter

End tag </script>.

jsChart

Call of the jsDisplayChart function.

divChart

<div> container to embed the chart into the page.

caption

character string of a standard caption, including data name and chart id.

footer

character string of a html page footer: </body>...</html>, including the used R and googleVis version and link to Google's Terms of Use.

Note

This is the new version of the GeoChart chart.

Author(s)

Markus Gesmann [email protected],

Diego de Castillo [email protected]

References

Google Geo Chart API: https://google-developers.appspot.com/chart/interactive/docs/gallery/geochart.html

Follow the link for Google's data policy.

See Also

See also print.gvis, plot.gvis for printing and plotting methods, gvisGeoMap and gvisIntensityMap for an alternative to gvisGeoChart.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
## Please note that by default the googleVis plot command
## will open a browser window and requires Internet
## connection to display the visualisation.

## Regions examples
## The regions style fills entire regions (typically countries) with
## colors corresponding to the values that you assign

G1 <- gvisGeoChart(Exports, locationvar='Country', colorvar='Profit') 

plot(G1)

## Plot only Europe
G2 <- gvisGeoChart(Exports, "Country", "Profit",
                   options=list(region="150"))

plot(G2)


## Example showing US data by state 
require(datasets)

states <- data.frame(state.name, state.x77)
G3 <- gvisGeoChart(states, "state.name", "Illiteracy",
                 options=list(region="US", displayMode="regions", resolution="provinces",
 		 width=600, height=400))
plot(G3)

## Markers Example
## A marker style map renders bubble-shaped markers at specified
## locations with the color and size that you specify.

G4 <- gvisGeoChart(CityPopularity, locationvar='City', colorvar='Popularity',
                      options=list(region='US', height=350, 
                                   displayMode='markers',
				   colorAxis="{values:[200,400,600,800],
                                   colors:[\'red', \'pink\', \'orange',\'green']}")
                      ) 
plot(G4)

G5 <- gvisGeoChart(Andrew, "LatLong", colorvar='Speed_kt',
                   options=list(region="US"))
plot(G5)


G6 <- gvisGeoChart(Andrew, "LatLong", sizevar='Speed_kt',
                   colorvar="Pressure_mb", options=list(region="US"))
plot(G6)

## Create lat:long values and plot a map of Oceania
## Set background colour to light-blue

require(stats)
data(quakes)
head(quakes)
quakes$latlong<-paste(quakes$lat, quakes$long, sep=":")

G7 <- gvisGeoChart(quakes, "latlong", "depth", "mag",
                   options=list(displayMode="Markers", region="009",
                   colorAxis="{colors:['red', 'grey']}",
                   backgroundColor="lightblue"))

plot(G7)

## Not run: 
# Plot S&P countries' credit rating sourced from Wikipedia
# Use the hovervar to show the rating
library(XML)
url <- "http://en.wikipedia.org/wiki/List_of_countries_by_credit_rating"
x <- readHTMLTable(readLines(url), which=3)
levels(x$Rating) <- substring(levels(x$Rating), 4, 
                              nchar(levels(x$Rating)))
x$Ranking <- x$Rating
levels(x$Ranking) <- nlevels(x$Rating):1
x$Ranking <- as.character(x$Ranking)
x$Rating <- paste(x$Country, x$Rating, sep=": ")
#### Create a geo chart
G8 <- gvisGeoChart(x, "Country", "Ranking", hovervar="Rating",
                options=list(gvis.editor="S&P", 
                             colorAxis="{colors:['#91BFDB', '#FC8D59']}"))
plot(G8)


## Plot world wide earth quakes of the last 30 days with magnitude >= 4.0 
library(XML)
## Get earthquake data of the last 30 days
eq <- read.csv("http://earthquake.usgs.gov/earthquakes/feed/v0.1/summary/2.5_week.csv")
eq$loc=paste(eq$Latitude, eq$Longitude, sep=":")

G9 <- gvisGeoChart(eq, "loc", "Depth", "Magnitude",
                   options=list(displayMode="Markers", 
                   colorAxis="{colors:['purple', 'red', 'orange', 'grey']}",
                   backgroundColor="lightblue"), chartid="EQ")
plot(G9)

## End(Not run)

barrosm/google-motion-charts-with-r documentation built on June 4, 2017, 1:13 a.m.