Markus Gesmann
KoelnR, 23 May 2014
The chart code has five generic parts:
plot(gvisMotionChart(Fruits, "Fruit", "Year"))
M <- gvisMotionChart(data,
idvar='id',
timevar='date',
options=list(),
chartid)
plot(M)
List structure:
M$html$chart
or M$html$chart["jsData"]
df <- data.frame(year=1:11, x=1:11)
plot(gvisScatterChart(df, options=list(width=600, height=400)))
df <- data.frame(label=c("US", "GB", "BR"), val1=c(1,3,4), val2=c(23,12,32))
Line <- gvisLineChart(df, xvar="label", yvar=c("val1","val2"),
options=list(title="Hello World", legend="bottom",
titleTextStyle="{color:'red', fontSize:18}",
vAxis="{gridlines:{color:'red', count:3}}",
hAxis="{title:'My Label',
titleTextStyle:{color:'blue'}}",
series="[{color:'green', targetAxisIndex: 0},
{color: 'blue',targetAxisIndex:1}]",
vAxes="[{title:'Value 1 (%)', format:'##,######%'},
{title:'Value 2 (\U00A3)'}]",
curveType="function", width=500, height=300
))
Options in googleVis have to follow the Google Chart API options
plot(Line)
Plot countries' S&P credit rating sourced from Wikipedia
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=": ")
G <- gvisGeoChart(x, "Country", "Ranking", hovervar="Rating",
options=
list(gvis.editor="S&P",
projection="kavrayskiy-vii",
colorAxis="{colors:['#91BFDB', '#FC8D59']}"))
plot(G)
df <- data.frame(year=1:11, x=1:11,
x.scope=c(rep(TRUE, 8), rep(FALSE, 3)),
y=11:1, y.html.tooltip=LETTERS[11:1],
y.certainty=c(rep(TRUE, 5), rep(FALSE, 6)),
y.emphasis=c(rep(FALSE, 4), rep(TRUE, 7)))
plot(gvisScatterChart(df,options=list(lineWidth=2, width=600, height=400)))
df <- data.frame(Adress=c("EC3M 7HA", "東京都新宿区北新宿2-21-1"),
Tip=c("Lloyd's", "Tokyo R User Group"))
plot(gvisMap(df, "Adress", "Tip",
options=list(mapType='normal',
icons="{'default': {'normal': 'http://icons.iconarchive.com/icons/icons-land/vista-map-markers/48/Map-Marker-Ball-Azure-icon.png',
'selected': 'http://icons.iconarchive.com/icons/icons-land/vista-map-markers/48/Map-Marker-Ball-Right-Azure-icon.png'}
}", width=800, height=250)))
dat <- data.frame(Room=c("Room 1","Room 2","Room 3"),
Language=c("Japanese", "English", "German"),
start=as.POSIXct(c("2014-04-17 14:00",
"2014-04-17 15:00",
"2014-04-17 14:30")),
end=as.POSIXct(c("2014-04-17 15:00",
"2014-04-17 16:00",
"2014-04-17 15:30")))
plot(gvisTimeline(data=dat, rowlabel="Room", barlabel="Language",
start="start", end="end", options=list(width=800, height=250)))
dat <- 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))
plot(gvisSankey(dat, from="From", to="To", weight="Weight",
options=list(height=250)))
plot(gvisCalendar(Cairo, datevar="Date",
numvar="Temp",
options=list(calendar="{ cellSize: 10 }"))
xtable(tail(Stock, 3))
Date | Device | Value | Title | Annotation | |
---|---|---|---|---|---|
10 | 13882.00 | Pens | 14334.00 | Out of stock | Ran out of stock of pens at 4pm |
11 | 13883.00 | Pens | 66467.00 | ||
12 | 13884.00 | Pens | 39463.00 |
A <- gvisAnnotationChart(
Stock, datevar="Date",
numvar="Value", idvar="Device",
titlevar="Title",
annotationvar="Annotation",
options=list(displayAnnotations=TRUE,
legendPosition='newRow',
fill=10, displayExactValues=TRUE,
colors="['#0000ff','#00ff00']",
width=600, height=300)
)
plot(A, tag='chart')
demo(googleVis)
to see examples of all charts {r setOptions, message=FALSE}
library(googleVis)
options(gvis.plot.tag="chart")
results='asis'
to display googleVis plot
statements
{r scatterchart, results="asis"}
plot(gvisScatterChart(women))
Markus Gesmann and Diego de Castillo. Using the Google Visualisation API with R, The R Journal, 3(2):40-44, December 2011 and googleVis package vignette
R markdown code of this presentation is available on GitHub
sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)
locale:
[1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] XML_3.98-1.1 xtable_1.7-3 googleVis_0.5.2 knitr_1.5
loaded via a namespace (and not attached):
[1] evaluate_0.5.5 formatR_0.10 RJSONIO_1.2-0.2 stringr_0.6.2
[5] tools_3.1.0