Examples from the googleVis package

It may take a little while to load all charts. Please be patient. All charts require an Internet connection.

These examples are taken from the googleVis demo. You can execute the demos by

library(googleVis)
demo(googleVis)

For more details about the charts and further examples see the helpfiles of the individual googleVis function and review the Google Chart Tools API documentation and Terms of Service.

Line chart

df=data.frame(country=c("US", "GB", "BR"), 
              val1=c(10,13,14), 
              val2=c(23,12,32))
Line <- gvisLineChart(df)
plot(Line)

Line chart with two axis

Line2 <- gvisLineChart(df, "country", c("val1","val2"),
                       options=list(
                         series="[{targetAxisIndex: 0},
                                 {targetAxisIndex:1}]",
                         vAxes="[{title:'val1'}, {title:'val2'}]"
                       ))
plot(Line2)

Setting options, it works similar for other charts

Line3 <-  gvisLineChart(df, xvar="country", yvar=c("val1","val2"),
                        options=list(
                          title="Hello World",
                          titleTextStyle="{color:'red', 
                                           fontName:'Courier', 
                                           fontSize:16}",                         
                          backgroundColor="#D3D3D3",                          
                          vAxis="{gridlines:{color:'red', count:3}}",
                          hAxis="{title:'Country', titleTextStyle:{color:'blue'}}",
                          series="[{color:'green', targetAxisIndex: 0}, 
                                   {color: 'orange',targetAxisIndex:1}]",
                          vAxes="[{title:'val1'}, {title:'val2'}]",
                          legend="bottom",
                          curveType="function",
                          width=500,
                          height=300                         
                        ))
plot(Line3)

Add edit button for on the fly customisation

Line4 <-  gvisLineChart(df, "country", c("val1","val2"),
                        options=list(gvis.editor="Edit me!"))
plot(Line4)

The same option is available for all other charts

Bar chart

Bar <- gvisBarChart(df)
plot(Bar)

Column chart

Column <- gvisColumnChart(df)
plot(Column)

Area chart

Area <- gvisAreaChart(df)
plot(Area)

Stepped Area chart

SteppedArea <- gvisSteppedAreaChart(df, xvar="country", 
                                    yvar=c("val1", "val2"),
                                    options=list(isStacked=TRUE))
plot(SteppedArea)

Combo chart

Combo <- gvisComboChart(df, xvar="country",
                        yvar=c("val1", "val2"),
                        options=list(seriesType="bars",
                                     series='{1: {type:"line"}}'))
plot(Combo)

Scatter chart

Scatter <- gvisScatterChart(women, 
                            options=list(
                              legend="none",
                              lineWidth=2, pointSize=0,
                              title="Women", vAxis="{title:'weight (lbs)'}",
                              hAxis="{title:'height (in)'}", 
                              width=300, height=300))
plot(Scatter)

Bubble chart

Bubble <- gvisBubbleChart(Fruits, idvar="Fruit", 
                          xvar="Sales", yvar="Expenses",
                          colorvar="Year", sizevar="Profit",
                          options=list(
                            hAxis='{minValue:75, maxValue:125}'))
plot(Bubble)

Candlestick chart

Candle <- gvisCandlestickChart(OpenClose, 
                               options=list(legend='none'))
plot(Candle)

Pie chart

Pie <- gvisPieChart(CityPopularity)
plot(Pie)

Gauge

Gauge <-  gvisGauge(CityPopularity, 
                    options=list(min=0, max=800, greenFrom=500,
                                 greenTo=800, yellowFrom=300, yellowTo=500,
                                 redFrom=0, redTo=300, width=400, height=300))
plot(Gauge)

Intensity Map

Intensity <- gvisIntensityMap(df)
plot(Intensity)

Geo Chart

Geo=gvisGeoChart(Exports, locationvar="Country", 
                 colorvar="Profit",
                 options=list(projection="kavrayskiy-vii"))
plot(Geo)

Example showing US data by state

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

Show Hurricane Andrew (1992) storm track with markers

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

Google Maps

AndrewMap <- gvisMap(Andrew, "LatLong" , "Tip", 
                     options=list(showTip=TRUE, 
                                  showLine=TRUE, 
                                  enableScrollWheel=TRUE,
                                  mapType='terrain', 
                                  useMapTypeControl=TRUE))
plot(AndrewMap)

Table

Table <- gvisTable(Exports, 
                   options=list(width=400, 
                                height=300))
plot(Table)

Click on the column header to sort the rows

Table with pages

PopTable <- gvisTable(Population, 
                      options=list(width=600, 
                                   height=300, 
                                   page='enable'))
plot(PopTable)

Org chart

Regions
Region  Parent Val Fac

1 Global 10 2 2 America Global 2 4 3 Europe Global 99 11 4 Asia Global 10 8 5 France Europe 71 2 6 Sweden Europe 89 3 7 Germany Europe 58 10 8 Mexico America 2 9 9 USA America 38 11 10 China Asia 5 1 11 Japan Asia 48 11

Org <- gvisOrgChart(Regions, 
                    options=list(width=600, height=250,
                                 size='large', allowCollapse=TRUE))
plot(Org)

Double click on a parent to collapse all its children.

Tree Map

Tree <- gvisTreeMap(Regions,  
                    "Region", "Parent", 
                    "Val", "Fac", 
                    options=list(fontSize=16))
plot(Tree)

Left mouse-click to drill down, right mouse-click to move up a hierarchy.

Annotation chart

Stock
     Date  Device  Value          Title

1 2008-01-01 Pencils 3000 2 2008-01-02 Pencils 14045 3 2008-01-03 Pencils 5502 4 2008-01-04 Pencils 75284 5 2008-01-05 Pencils 41476 Bought pencils 6 2008-01-06 Pencils 333222 7 2008-01-01 Pens 40645 8 2008-01-02 Pens 20374 9 2008-01-03 Pens 50766 10 2008-01-04 Pens 14334 Out of stock 11 2008-01-05 Pens 66467 12 2008-01-06 Pens 39463 Annotation 1 2 3 4 5 Bought 200k pencils 6 7 8 9 10 Ran out of stock of pens at 4pm 11 12

Anno <- gvisAnnotationChart(Stock, 
                            datevar="Date",
                            numvar="Value", 
                            idvar="Device",
                            titlevar="Title", 
                            annotationvar="Annotation",
                            options=list(
                              width=600, height=350,
                              fill=10, displayExactValues=TRUE,
                              colors="['#0000ff','#00ff00']")
)
plot(Anno)

Sankey chart

datSK <- data.frame(From=c(rep("A",3), rep("B", 3)),
                    To=c(rep(c("X", "Y", "Z"),2)),
                    Weight=c(5,7,6,2,9,4))

Sankey <- gvisSankey(datSK, from="From", to="To", weight="Weight",
                     options=list(
                       sankey="{link: {color: { fill: '#d799ae' } },
                            node: { color: { fill: '#a61d4c' },
                            label: { color: '#871b47' } }}"))
plot(Sankey)

Histogram

set.seed(123)
datHist=data.frame(A=rpois(100, 20),
                   B=rpois(100, 5),
                   C=rpois(100, 50))

Hist <- gvisHistogram(datHist, options=list(
  legend="{ position: 'top', maxLines: 2 }",
  colors="['#5C3292', '#1A8763', '#871B47']",
  width=400, height=360))
plot(Hist)

Calendar chart

head(Cairo)
       Date  Temp

2558 2002-01-01 13.61 2559 2002-01-02 15.17 2560 2002-01-03 12.00 2561 2002-01-04 12.00 2562 2002-01-05 13.06 2563 2002-01-06 10.06

Cal <- gvisCalendar(Cairo, 
                    datevar="Date", 
                    numvar="Temp",
                    options=list(
                      title="Daily temperature in Cairo",
                      height=500,
                      calendar="{yearLabel:{fontName: 'Times-Roman',
                                cellSize: 10,
                                fontSize:32, color:'#1A8763', bold: true},
                                cellColor:{ stroke:'red', strokeOpacity:0.2},
                                focusedCellColor:{stroke:'red'}}"))
plot(Cal)

Timeline chart

datTL <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)),
                    Name=c("Washington", "Adams", "Jefferson",
                           "Adams", "Jefferson", "Burr"),
                    start=as.Date(x=rep(c("1789-03-29", "1797-02-03", 
                                          "1801-02-03"),2)),
                    end=as.Date(x=rep(c("1797-02-03", "1801-02-03", 
                                        "1809-02-03"),2)))

Timeline <- gvisTimeline(data=datTL, 
                         rowlabel="Name",
                         barlabel="Position",
                         start="start", 
                         end="end",
                         options=list(timeline="{groupByRowLabel:false}",
                                      backgroundColor='#ffd', 
                                      height=350,
                                      colors="['#cbb69d', '#603913', '#c69c6e']"))
plot(Timeline)

Merging charts

G <- gvisGeoChart(Exports, "Country", "Profit", 
                  options=list(width=220, height=100))
T <- gvisTable(Exports, 
               options=list(width=220, height=260))

GT <- gvisMerge(G,T, horizontal=FALSE) 
plot(GT)
H <- gvisHistogram(datHist, options=list(width=400, height=360))
GTH <- gvisMerge(GT, H, horizontal=TRUE,
                 tableOptions="bgcolor=\"#CCCCCC\" cellspacing=10")
plot(GTH)



## Flash charts

All the following charts require Flash.

Geo Map

AndrewGeo <- gvisGeoMap(Andrew, 
                        locationvar="LatLong", 
                        numvar="Speed_kt", 
                        hovervar="Category", 
                        options=list(height=350, 
                                     region="US", 
                                     dataMode="markers"))
plot(AndrewGeo)

Annotated time line chart

AnnoTimeLine  <- gvisAnnotatedTimeLine(Stock, 
                                       datevar="Date",
                                       numvar="Value", 
                                       idvar="Device",
                                       titlevar="Title", 
                                       annotationvar="Annotation",
                                       options=list(displayAnnotations=TRUE,
                                                    width=600, height=350))
plot(AnnoTimeLine)

Motion chart

Motion=gvisMotionChart(Fruits, 
                       idvar="Fruit", 
                       timevar="Year")
plot(Motion)

You can change some of displaying settings via the browser, e.g. the level of opacity of non-selected items, or the chart type. The state string from the 'Advanced' tab can be used to set those settings via R. Just copy and past the string from the browser into the argument state of the options list. Here is an example of a motion chart, with an initial line chart displayed.

myStateSettings <-'
{"xZoomedDataMin":1199145600000,"colorOption":"2",
"duration":{"timeUnit":"Y","multiplier":1},"yLambda":1,
"yAxisOption":"4","sizeOption":"_UNISIZE",
"iconKeySettings":[],"xLambda":1,"nonSelectedAlpha":0,
"xZoomedDataMax":1262304000000,"iconType":"LINE",
"dimensions":{"iconDimensions":["dim0"]},
"showTrails":false,"uniColorForNonSelected":false,
"xAxisOption":"_TIME","orderedByX":false,"playDuration":15000,
"xZoomedIn":false,"time":"2010","yZoomedDataMin":0,
"yZoomedIn":false,"orderedByY":false,"yZoomedDataMax":100}
'
M <- gvisMotionChart(Fruits, "Fruit", "Year", options=list(state=myStateSettings))
plot(M)


## See demo(package='googleVis') for other available demos.