gvisBarChart: Google Bar Chart with R

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

Description

The gvisBarChart 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. The actual chart is rendered by the web browser using SVG or VML.

Usage

1
gvisBarChart(data, xvar = "", yvar = "", options = list(), chartid)

Arguments

data

a data.frame to be displayed as a bar chart

xvar

name of the character column which contains the category labels for the x-axes.

yvar

a vector of column names of the numerical variables to be plotted. Each column is displayed as a separate bar/column.

options

list of configuration options for Google Bar 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/barchart.html#Configuration_Options:

axisTitlesPosition

a string. Default 'out'. Where to place the axis titles, compared to the chart area. Supported values:

'in'

Draw the axis titles inside the the chart area.

'out'

Draw the axis titles outside the chart area.

'none'

Omit the axis titles.

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.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.

backgroundColor.fill

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

chartArea

a string. Default 'null'. An object with members to configure the placement and size of the chart area (where the chart itself is drawn, excluding axis and legends). Two formats are supported: a number, or a number followed by %. A simple number is a value in pixels; a number followed by % is a percentage. Example:

{left:20,top:0,width:\"50%\",height:\"75%\"}
chartArea.height

a number or string. Default auto. Chart area height.

chartArea.left

a number or string. Default auto. How far to draw the chart from the left border.

chartArea.top

a number or string. Default auto. How far to draw the chart from the top border.

chartArea.width

a number or string. Default auto. Chart area width.

colors

A JSON array of strings. Default 'colors'. The colors to use for the chart elements. An array of strings, where each element is an HTML color string, for example: colors:['red','#004411'].

enableInteractivity

boolean. Default TRUE. Whether the chart throws user-based events or reacts to user interaction. If false, the chart will not throw 'select' or other interaction-based events (but will throw ready or error events), and will not display hovertext or otherwise change depending on user input.

focusTarget

a string. Default 'datum' . The type of the entity that receives focus on mouse hover. Also affects which entity is selected by mouse click, and which data table element is associated with events. Can be one of the following:

'datum'

Focus on a single data point. Correlates to a cell in the data.

'category'

Focus on a grouping of all data points along the major axis. Correlates to a row in the data table.

In focusTarget 'category' the tooltip displays all the category values. This may be useful for comparing values of different series.

fontSize

a number. Default automatic. The default font size, in pixels, of all text in the chart. You can override this using properties for specific chart elements.

fontName

a string. Default 'Arial'. The default font face for all text in the chart. You can override this using properties for specific chart elements.

hAxes

a JSON array of objects, or object with child objects null. Specifies properties for individual horizontal axes, if the chart has multiple horizontal axes. Each child object is a hAxis object, and can contain all the properties supported by hAxis. These property values override any global settings for the same property.

To specify a chart with multiple horizontal axes, first define a new axis using series.targetAxisIndex, then configure the axis using vAxes. The following example assigns series 2 to the bottom axis and specifies a custom title and text style for it:

series:{2:{targetAxisIndex:1}},
	  vAxes:{1:{title:'Losses',textStyle:{color: 'red'}}}
	  

This property can be either an object or an array: the object is a collection of objects, each with a numeric label that specifies the axis that it defines–this is the format shown above; the array is an array of objects, one per axis. For example, the following array-style notation is identical to the hAxis object shown above:

	    hAxes:[
	    {}, // Nothing specified for axis 0
	    {title:'Losses',textStyle:{color: 'red'}} // Axis 1
	    ]
	  
hAxis

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

{title: 'Hello', titleTextStyle: {color: '#FF0000'}}
hAxis.baseline

a number. Default automatic. hAxis property that specifies the baseline for the horizontal axis. If the baseline is smaller than the highest grid line or smaller than the lowest grid line, it will be rounded to the closest gridline.

hAxis.baselineColor

a string. Default 'black'. hAxis property that specifies the color of the baseline for the horizontal axis. Can be any HTML color string, for example: 'red' or '#00cc00'.

hAxis.direction

1 or -1. Default 1. The direction in which the values along the horizontal axis grow. Specify -1 to reverse the order of the values.

hAxis.format

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

{format:'#,###%'}.

will display values 1,000%, 750%, and 50% for values 10, 7.5, and 0.5.

hAxis.gridlines

a JSON object. Default NULL. An object with members to configure the gridlines on the vertical axis. To specify properties of this object, you can use object literal notation, as shown here:

	  {color: '#333', count: 4}
	
hAxis.gridlines.color

a string. Default '#CCC'. The color of the horizontal gridlines inside the chart area. Specify a valid HTML color string.

hAxis.gridlines.count

a number. Default 5.The number of vertical gridlines inside the chart area. Minimum value is 2.

hAxis.logScale

boolean. Default FALSE. vAxis property that makes the vertical axis a logarithmic scale (requires all values to be positive). Set to TRUE for yes.

hAxis.textPosition

a string. Default 'out' Position of the horizontal axis text, relative to the chart area. Supported values: 'out', 'in', 'none'.

hAxis.textStyle

a JSON object. Default

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

An object that specifies the horizontal axis 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.

hAxis.title

a string. Default 'null'. hAxis property that specifies the title of the horizontal axis.

hAxis.titleTextStyle

a JSON object. Default

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

An object that specifies the horizontal axis title 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.

hAxis.maxValue

a number. Default automatic. hAxis property that specifies the highest vertical axis grid line. The actual grid line will be the greater of two values: the maxValue option value, or the highest data value, rounded up to the next higher grid mark.

hAxis.minValue

a number. Default automatic. hAxis property that specifies the lowest vertical axis grid line. The actual grid line will be the lower of two values: the minValue option value, or the lowest data value, rounded down to the next lower grid mark.

hAxis.viewWindowMode

a string. Default "pretty" if hAxis.viewWindow is null, "explicit" otherwise. Specifies how to scale the horizontal axis to render the values within the chart area. The following string values are supported:

'pretty'

Scale the horizontal values so that the maximum and minimum data values are rendered a bit inside the left and right of the chart area.

'maximized'

Scale the horizontal values so that the maximum and minimum data values touch the left and right of the chart area.

'explicit'

Specify the left and right scale values of the chart area. Data values outside these values will be cropped. You must specify a

hAxis.viewWindow

object describing the maximum and minimum values to show.

hAxis.viewWindow

JSON object. Default NULL. Specifies the maximum and minimum data values to show on the horizontal axis. Present only if

vAxis.viewWindowMode = 'explicit'
hAxis.viewWindow.max

number. Default 0. The maximum vertical data value to render.

hAxis.viewWindow.min

number. Default 0. The minimum vertical data value to render.

height

number. Default height of the containing element. Height of the chart, in pixels.

isStacked

boolean. Default FALSE. If set to TRUE, bar values are stacked (accumulated).

legend

a JSON object. Default NULL. An object with members to configure various aspects of the legend. To specify properties of this object, you can use object literal notation, as shown here:

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

a string. Default 'right'. Position of the legend. Can be one of the following:

'right'

To the right of the chart.

'top'

Above the chart.

'bottom'

Below the chart.

'none'

No legend is displayed.

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.

reverseCategories

boolean. Default FALSE. If set to true, will draw series from right to left. The default is to draw left-to-right.

series

a JSON array of objects, or object with nested objects. Default {}. An array of objects, each describing the format of the corresponding series in the chart. To use default values for a series, specify an empty object . If a series or a value is not specified, the global value will be used. Each object supports the following properties:

color

The color to use for this series. Specify a valid HTML color string.

targetAxisIndex

Which axis to assign this series to, where 0 is the default axis, and 1 is the opposite axis. Default value is 0; set to 1 to define a chart where different series are rendered against different axes. You can define a different scale for different axes.

pointSize

Overrides the global pointSize value for this series.

lineWidth

Overrides the global lineWidth value for this series.

curveType

Overrides the global curveType value for this series.

visibleInLegend

A boolean value, where true means that the series should have a legend entry, and false means that it should not. Default is TRUE.

You can specify either an array of objects, each of which applies to the series in the order given, or you can specify an object where each child has a numeric key indicating which series it applies to. For example, the following two declarations are identical, and declare the first series as black and absent from the legend, and the fourth as red and absent from the legend:

	    series: [{color: 'black', visibleInLegend: false},{},
	    {}, {color: 'red', visibleInLegend: false}]
	  
	    series: {0:{color: 'black', visibleInLegend: false},
	    3:{color: 'red', visibleInLegend: false}}
	  
theme

a string. Default NULL. A theme is a set of predefined option values that work together to achieve a specific chart behavior or visual effect. Currently only one theme is available:

maximized

Maximizes the area of the chart, and draws the legend and all of the labels inside the chart area. Sets the following options:

		chartArea: {width: '100%', height: '100%'},
		legend: {position: 'in'},
		titlePosition: 'in', axisTitlesPosition: 'in',
		hAxis: {textPosition: 'in'}, vAxis: {textPosition: 'in'}
	      
title

a string. Default no title. Text to display above the chart.

titlePosition

a string. Default 'out'. Where to place the chart title, compared to the chart area. Supported values:

'in'

Draw the title inside the chart area.

'out'

Draw the title outside the chart area.

'none'

Omit the title.

titleTextStyle

a JSON object. Default

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

An object that specifies the title 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.

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.showColorCode

boolean. Default automatic. If true, show colored squares next to the series information in the tooltip. The default is true when focusTarget is set to 'category', otherwise the default is FALSE.

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'. Also see fontName and fontSize.

tooltip.trigger

The user interaction that causes the tooltip to be displayed:

'hover'

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

'none'

The tooltip will not be displayed.

vAxis

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

{title: 'Hello', titleTextStyle: {color: '#FF0000'}}
vAxis.direction

1 or -1. Default 1. The direction in which the values along the vertical axis grow. Specify -1 to reverse the order of the values.

vAxis.textPosition

a string. Default 'out'. Position of the vertical axis text, relative to the chart area. Supported values: 'out', 'in', 'none'.

vAxis.textStyle

a JSON object. Default

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

An object that specifies the vertical axis 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.

vAxis.title

a string. Default no title. vAxis property that specifies a title for the vertical axis.

vAxis.titleTextStyle

a JSON object. Default

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

An object that specifies the vertical axis title 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.

width

a number. Default width of the containing element. Width of the chart, in pixels.

chartid

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

Value

gvisBarChart returns a 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 'BarChart'

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.

Warning

Google Visualisation API: You cannot load both barchart/columnchart and corechart packages at the same time on the same page.

Author(s)

Markus Gesmann markus.gesmann@gmail.com,

Diego de Castillo decastillo@gmail.com

References

Google Bar Chart API: http://code.google.com/apis/chart/interactive/docs/gallery/barchart.html

Follow the link for Google's data policy.

See Also

See also print.gvis, plot.gvis for printing and plotting methods

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
## Please note that by default the googleVis plot command
## will open a browser window and requires an internet
## connection to display the visualisation.

df=data.frame(country=c("US", "GB", "BR"), val1=c(1,3,4), val2=c(23,12,32))

## Bar chart
Bar1 <- gvisBarChart(df, xvar="country", yvar=c("val1", "val2"))
plot(Bar1)

## Stacked bar chart
Bar2 <- gvisBarChart(df, xvar="country", yvar=c("val1", "val2"),
     options=list(isStacked=TRUE))
plot(Bar2)


## Add a customised title and smoothed curve
Bar3 <- gvisBarChart(df, xvar="country", yvar=c("val1", "val2"),
             options=list(title="Hello World",
                          titleTextStyle="{color:'red',fontName:'Courier',fontSize:16}",
                          curveType='function'))
plot(Bar3)

## Not run: 
## Change x-axis to percentages
Bar4 <- gvisBarChart(df, xvar="country", yvar=c("val1", "val2"),
                       options=list(hAxis="{format:'#,###%'}"))
plot(Bar4)

## The following example reads data from a Wikipedia table and displays
## the information in a bar chart.
## We use the readHMLTable function of the XML package to get the data
library(XML)
## Get the data of the biggest ISO container companies from Wikipedia
##(table 3):
df=readHTMLTable(readLines("http://en.wikipedia.org/wiki/Intermodal_freight_transport"))[[3]][,1:2]
## Rename the second column
names(df)[2]="TEU capacity"
## The numbers are displayed with commas to separate thousands, so let's
## get rid of them:
df[,2]=as.numeric(gsub(",", "", as.character(df[,2])))

## Finally we can create a nice bar chart:
Bar5 <- gvisBarChart(df, options=list(
                    chartArea="{left:250,top:50,width:\"50%\",height:\"75%\"}",
                    legend="bottom", 
                    title="Top 20 container shipping companies in order of TEU capacity"))

plot(Bar5)


## End(Not run)

jburos/GoogleVis documentation built on May 18, 2019, 9:19 p.m.