Debian Codegraph @

demo/googleVis.R

raw
1 ## ---- googleVis demo ----
2 ## ---- pauseFunction ----
3 pause <- function(){
4 invisible(readline("\nPress <return> to continue: "))
5 }
6
7 ## ---- testData ----
8 df=data.frame(country=c("US", "GB", "BR"),
9 val1=c(10,13,14),
10 val2=c(23,12,32))
11
12
13 ## ---- LineChart ----
14 Line <- gvisLineChart(df)
15 plot(Line)
16
17 ## ---- pause ----
18 pause()
19
20 ## ---- TwoAxis ----
21 Line2 <- gvisLineChart(df, "country", c("val1","val2"),
22 options=list(
23 series="[{targetAxisIndex: 0},
24 {targetAxisIndex:1}]",
25 vAxes="[{title:'val1'}, {title:'val2'}]"
26 ))
27 plot(Line2)
28
29 ## ---- pause ----
30 pause()
31
32 ## ---- SettingOptions ----
33 Line3 <- gvisLineChart(df, xvar="country", yvar=c("val1","val2"),
34 options=list(
35 title="Hello World",
36 titleTextStyle="{color:'red',
37 fontName:'Courier',
38 fontSize:16}",
39 backgroundColor="#D3D3D3",
40 vAxis="{gridlines:{color:'red', count:3}}",
41 hAxis="{title:'Country', titleTextStyle:{color:'blue'}}",
42 series="[{color:'green', targetAxisIndex: 0},
43 {color: 'orange',targetAxisIndex:1}]",
44 vAxes="[{title:'val1'}, {title:'val2'}]",
45 legend="bottom",
46 curveType="function",
47 width=500,
48 height=300
49 ))
50 plot(Line3)
51
52 ## ---- pause ----
53 pause()
54
55 ## ---- CustomizingLines ----
56 Dashed <- gvisLineChart(df, xvar="country", yvar=c("val1","val2"),
57 options=list(
58 series="[{color:'green', targetAxisIndex: 0,
59 lineWidth: 1, lineDashStyle: [2, 2, 20, 2, 20, 2]},
60 {color: 'blue',targetAxisIndex: 1,
61 lineWidth: 2, lineDashStyle: [4, 1]}]",
62 vAxes="[{title:'val1'}, {title:'val2'}]"
63 ))
64 plot(Dashed)
65
66 ## ---- pause ----
67 pause()
68
69 ## ---- EditButton ----
70 Line4 <- gvisLineChart(df, "country", c("val1","val2"),
71 options=list(gvis.editor="Edit me!"))
72 plot(Line4)
73
74
75
76 ## ---- pause ----
77 pause()
78
79 ## ---- BarChart ----
80 Bar <- gvisBarChart(df)
81 plot(Bar)
82
83 ## ---- pause ----
84 pause()
85
86 ## ---- ColumnChart ----
87 Column <- gvisColumnChart(df)
88 plot(Column)
89
90 ## ---- pause ----
91 pause()
92
93 ## ---- AreaChart ----
94 Area <- gvisAreaChart(df)
95 plot(Area)
96
97 ## ---- pause ----
98 pause()
99
100 ## ---- SteppedAreaChart ----
101 SteppedArea <- gvisSteppedAreaChart(df, xvar="country",
102 yvar=c("val1", "val2"),
103 options=list(isStacked=TRUE))
104 plot(SteppedArea)
105
106 ## ---- pause ----
107 pause()
108
109 ## ---- ComboChart ----
110 Combo <- gvisComboChart(df, xvar="country",
111 yvar=c("val1", "val2"),
112 options=list(seriesType="bars",
113 series='{1: {type:"line"}}'))
114 plot(Combo)
115
116 ## ---- pause ----
117 pause()
118
119 ## ---- ScatterChart ----
120 Scatter <- gvisScatterChart(women,
121 options=list(
122 legend="none",
123 lineWidth=2, pointSize=0,
124 title="Women", vAxis="{title:'weight (lbs)'}",
125 hAxis="{title:'height (in)'}",
126 width=300, height=300))
127 plot(Scatter)
128
129 ## ---- ScatterChartPoints ----
130 M <- matrix(nrow=6,ncol=6)
131 M[col(M)==row(M)] <- 1:6
132 dat <- data.frame(X=1:6, M)
133 SC <- gvisScatterChart(dat,
134 options=list(
135 title="Customizing points",
136 legend="right",
137 pointSize=30,
138 series="{
139 0: { pointShape: 'circle' },
140 1: { pointShape: 'triangle' },
141 2: { pointShape: 'square' },
142 3: { pointShape: 'diamond' },
143 4: { pointShape: 'star' },
144 5: { pointShape: 'polygon' }
145 }"))
146 plot(SC)
147
148 ## ---- pause ----
149 pause()
150
151 ## ---- BubbleChart ----
152 Bubble <- gvisBubbleChart(Fruits, idvar="Fruit",
153 xvar="Sales", yvar="Expenses",
154 colorvar="Year", sizevar="Profit",
155 options=list(
156 hAxis='{minValue:75, maxValue:125}'))
157 plot(Bubble)
158
159 ## ---- pause ----
160 pause()
161
162 ## ---- CandlestickChart ----
163 Candle <- gvisCandlestickChart(OpenClose,
164 options=list(legend='none'))
165 plot(Candle)
166
167 ## ---- pause ----
168 pause()
169
170 ## ---- PieChart ----
171 Pie <- gvisPieChart(CityPopularity)
172 plot(Pie)
173
174 ## ---- pause ----
175 pause()
176
177 ## ---- Gauge ----
178 Gauge <- gvisGauge(CityPopularity,
179 options=list(min=0, max=800, greenFrom=500,
180 greenTo=800, yellowFrom=300, yellowTo=500,
181 redFrom=0, redTo=300, width=400, height=300))
182 plot(Gauge)
183
184 ## ---- pause ----
185 pause()
186
187
188
189 ## ---- GeoChart ----
190 Geo=gvisGeoChart(Exports, locationvar="Country",
191 colorvar="Profit",
192 options=list(projection="kavrayskiy-vii"))
193 plot(Geo)
194
195 ## ---- pause ----
196 pause()
197
198
199 ## ---- USStateData ----
200 require(datasets)
201 states <- data.frame(state.name, state.x77)
202 GeoStates <- gvisGeoChart(states, "state.name", "Illiteracy",
203 options=list(region="US",
204 displayMode="regions",
205 resolution="provinces",
206 width=600, height=400))
207 plot(GeoStates)
208
209 ## ---- pause ----
210 pause()
211
212 ## ---- GeoChartHurricaneAndrew ----
213 GeoMarker <- gvisGeoChart(Andrew, "LatLong",
214 sizevar='Speed_kt',
215 colorvar="Pressure_mb",
216 options=list(region="US"))
217 plot(GeoMarker)
218
219 ## ---- pause ----
220 pause()
221
222 ## ---- GoogleMapHurricaneAndrew ----
223 AndrewMap <- gvisMap(Andrew, "LatLong" , "Tip",
224 options=list(showTip=TRUE,
225 showLine=TRUE,
226 enableScrollWheel=TRUE,
227 mapType='terrain',
228 useMapTypeControl=TRUE))
229 plot(AndrewMap)
230
231 ## ---- pause ----
232 pause()
233
234
235 ## ---- Table ----
236 Table <- gvisTable(Stock,
237 formats=list(Value="#,###"))
238 plot(Table)
239
240 ## ---- pause ----
241 pause()
242
243 ## ---- TableWithPages ----
244 PopTable <- gvisTable(Population,
245 formats=list(Population="#,###",
246 '% of World Population'='#.#%'),
247 options=list(page='enable'))
248 plot(PopTable)
249
250 ## ---- pause ----
251 pause()
252
253 ## ---- OrgChart ----
254 Org <- gvisOrgChart(Regions,
255 options=list(width=600, height=250,
256 size='large', allowCollapse=TRUE))
257 plot(Org)
258
259 ## ---- pause ----
260 pause()
261
262
263 ## ---- TreeMap ----
264 Tree <- gvisTreeMap(Regions,
265 "Region", "Parent",
266 "Val", "Fac",
267 options=list(fontSize=16))
268 plot(Tree)
269
270 ## ---- pause ----
271 pause()
272
273 ## ---- AnnotationChart ----
274 Anno <- gvisAnnotationChart(Stock,
275 datevar="Date",
276 numvar="Value",
277 idvar="Device",
278 titlevar="Title",
279 annotationvar="Annotation",
280 options=list(
281 width=600, height=350,
282 fill=10, displayExactValues=TRUE,
283 colors="['#0000ff','#00ff00']")
284 )
285 plot(Anno)
286
287 ## ---- pause ----
288 pause()
289
290 ## ---- SankeyChart ----
291 datSK <- data.frame(From=c(rep("A",3), rep("B", 3)),
292 To=c(rep(c("X", "Y", "Z"),2)),
293 Weight=c(5,7,6,2,9,4))
294
295 Sankey <- gvisSankey(datSK, from="From", to="To", weight="Weight",
296 options=list(
297 sankey="{link: {color: { fill: '#d799ae' } },
298 node: { color: { fill: '#a61d4c' },
299 label: { color: '#871b47' } }}"))
300 plot(Sankey)
301
302 ## ---- pause ----
303 pause()
304
305 # ---- CalendarChart ----
306 Cal <- gvisCalendar(Cairo,
307 datevar="Date",
308 numvar="Temp",
309 options=list(
310 title="Daily temperature in Cairo",
311 height=320,
312 calendar="{yearLabel: { fontName: 'Times-Roman',
313 fontSize: 32, color: '#1A8763', bold: true},
314 cellSize: 10,
315 cellColor: { stroke: 'red', strokeOpacity: 0.2 },
316 focusedCellColor: {stroke:'red'}}")
317 )
318 plot(Cal)
319
320 ## ---- pause ----
321 pause()
322
323 # ---- Timeline ----
324 datTL <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)),
325 Name=c("Washington", "Adams", "Jefferson",
326 "Adams", "Jefferson", "Burr"),
327 start=as.Date(x=rep(c("1789-03-29", "1797-02-03",
328 "1801-02-03"),2)),
329 end=as.Date(x=rep(c("1797-02-03", "1801-02-03",
330 "1809-02-03"),2)))
331
332 Timeline <- gvisTimeline(data=datTL,
333 rowlabel="Name",
334 barlabel="Position",
335 start="start",
336 end="end",
337 options=list(timeline="{groupByRowLabel:false}",
338 backgroundColor='#ffd',
339 height=350,
340 colors="['#cbb69d', '#603913', '#c69c6e']"))
341 plot(Timeline)
342
343 # ---- Gantt ----
344
345 daysToMilliseconds <- function(days){
346 days * 24 * 60 * 60 * 1000
347 }
348
349 dat <- data.frame(
350 taskID = c("Research", "Write", "Cite", "Complete", "Outline"),
351 taskName = c("Find sources", "Write Paper", "Create bibliography", "Hand in paper", "Outline paper"),
352 resource = c(NA, "write", "write", "complete", "write"),
353 start = c(as.Date("2015-01-01"), NA, NA, NA, NA),
354 end = as.Date(c("2015-01-05", "2015-01-09", "2015-01-07", "2015-01-10", "2015-01-06")),
355 duration = c(NA, daysToMilliseconds(c(3, 1, 1, 1))),
356 percentComplete = c(100, 25, 20, 0, 100),
357 dependencies = c(NA, "Research, Outline", "Research", "Cite, Write", "Research")
358 )
359
360 gntt <- gvisGantt(dat, taskID = "taskID",
361 taskName = "taskName",
362 resource = "resource",
363 start = "start",
364 end = "end",
365 duration = "duration",
366 percentComplete = "percentComplete",
367 dependencies = "dependencies",
368 options = list(height = '300',
369 width = 'auto'))
370
371 plot(gntt)
372
373 ## ---- pause ----
374 pause()
375
376 ## ---- Histogram ----
377 set.seed(123)
378 datHist=data.frame(A=rpois(100, 20),
379 B=rpois(100, 5),
380 C=rpois(100, 50))
381
382 Hist <- gvisHistogram(datHist, options=list(
383 legend="{ position: 'top', maxLines: 2 }",
384 colors="['#5C3292', '#1A8763', '#871B47']",
385 width=400, height=360))
386 plot(Hist)
387
388 ## ---- pause ----
389 pause()
390
391 ## ---- WordTree ----
392 wt1 <- gvisWordTree(Cats, textvar = "Phrase")
393 plot(wt1)
394
395 ## ---- gvisMerge ----
396 G <- gvisGeoChart(Exports, "Country", "Profit",
397 options=list(width=300, height=300))
398 T <- gvisTable(Exports,
399 options=list(width=220, height=300))
400
401 GT <- gvisMerge(G,T, horizontal=TRUE)
402 plot(GT)
403
404 ## ---- pause ----
405 pause()
406
407 ## See demo(package='googleVis') for other available demos.