-
Notifications
You must be signed in to change notification settings - Fork 92
/
timeComplementary.R
353 lines (292 loc) · 13.4 KB
/
timeComplementary.R
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
##################################################################
## Source code for the book: "Displaying time series, spatial and
## space-time data with R"
## Copyright (C) 2013-2012 Oscar Perpiñán Lamigueiro
## This program is free software you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; either version 2 of the License,
## or (at your option) any later version.
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.
####################################################################
##################################################################
## Initial configuration
##################################################################
## Clone or download the repository and set the working directory
## with setwd to the folder where the repository is located.
library(lattice)
library(ggplot2)
library(latticeExtra)
library(zoo)
myTheme <- custom.theme.2(pch=19, cex=0.7,
region=rev(brewer.pal(9, 'YlOrRd')),
symbol = brewer.pal(n=8, name = "Dark2"))
myTheme$strip.background$col='transparent'
myTheme$strip.shingle$col='transparent'
myTheme$strip.border$col='transparent'
xscale.components.custom <- function(...){
ans <- xscale.components.default(...)
ans$top=FALSE
ans}
yscale.components.custom <- function(...){
ans <- yscale.components.default(...)
ans$right=FALSE
ans}
myArgs <- list(as.table=TRUE,
between=list(x=0.5, y=0.2),
xscale.components = xscale.components.custom,
yscale.components = yscale.components.custom)
defaultArgs <- lattice.options()$default.args
lattice.options(default.theme = myTheme,
default.args = modifyList(defaultArgs, myArgs))
##################################################################
##################################################################
## Polylines
##################################################################
load('data/CO2.RData')
library(googleVis)
pgvis <- gvisMotionChart(CO2data, idvar='Country.Name', timevar='Year')
print(pgvis, 'html', file='figs/googleVis.html')
pdf(file="figs/CO2_GNI.pdf")
## lattice version
xyplot(GNI.capita ~ CO2.capita, data=CO2data,
xlab="Carbon dioxide emissions (metric tons per capita)",
ylab="GNI per capita, PPP (current international $)",
groups=Country.Name, type='b')
dev.off()
## ggplot2 version
ggplot(data=CO2data, aes(x=CO2.capita, y=GNI.capita,
color=Country.Name)) +
xlab("Carbon dioxide emissions (metric tons per capita)") +
ylab("GNI per capita, PPP (current international $)") +
geom_point() + geom_path() + theme_bw()
##################################################################
## Choosing colors
##################################################################
library(RColorBrewer)
nCountries <- nlevels(CO2data$Country.Name)
pal <- brewer.pal(n=5, 'Set1')
pal <- rep(pal, length = nCountries)
## Rank of average values of CO2 per capita
CO2mean <- aggregate(CO2.capita ~ Country.Name, data=CO2data, FUN=mean)
palOrdered <- pal[rank(CO2mean$CO2.capita)]
pdf(file="figs/hclust.pdf")
CO2capita <- CO2data[, c('Country.Name', 'Year', 'CO2.capita')]
CO2capita <- reshape(CO2capita, idvar='Country.Name', timevar='Year', direction='wide')
hCO2 <- hclust(dist(CO2capita[, -1]))
oldpar <- par(mar=c(0, 2, 0, 0) + .1)
plot(hCO2, labels=CO2capita$Country.Name,
xlab='', ylab='', sub='', main='')
par(oldpar)
dev.off()
idx <- match(levels(CO2data$Country.Name),
CO2capita$Country.Name[hCO2$order])
palOrdered <- pal[idx]
## simpleTheme encapsulates the palette in a new theme for xyplot
myTheme <- simpleTheme(pch=19, cex=0.6, col=palOrdered)
pCO2.capita <- xyplot(GNI.capita ~ CO2.capita,
xlab="Carbon dioxide emissions (metric tons per capita)",
ylab="GNI per capita, PPP (current international $)",
groups=Country.Name, data=CO2data,
par.settings=myTheme,
type='b')
gCO2.capita <- ggplot(data=CO2data, aes(x=CO2.capita, y=GNI.capita,
color=Country.Name)) +
geom_point() + geom_path() +
scale_color_manual(values=palOrdered, guide=FALSE) +
xlab('CO2 emissions (metric tons per capita)') +
ylab('GNI per capita, PPP (current international $)') +
theme_bw()
##################################################################
## Labels to show time information
##################################################################
xyplot(GNI.capita ~ CO2.capita,
xlab="Carbon dioxide emissions (metric tons per capita)",
ylab="GNI per capita, PPP (current international $)",
groups=Country.Name, data=CO2data,
par.settings=myTheme,
type='b',
panel=function(x, y, ..., subscripts, groups){
panel.text(x, y, ...,
labels=CO2data$Year[subscripts],
pos=2, cex=0.5, col='gray')
panel.superpose(x, y, subscripts, groups,...)
}
)
pCO2.capita <- pCO2.capita +
glayer_(panel.text(..., labels=CO2data$Year[subscripts],
pos=2, cex=0.5, col='gray'))
gCO2.capita <- gCO2.capita + geom_text(aes(label=Year),
colour='gray',
size=2.5,
hjust=0, vjust=0)
##################################################################
## Country names: positioning labels
##################################################################
pdf(file="figs/CO2_capita.pdf")
library(maptools)
## group.value provides the country name; group.number is the
## index of each country to choose the color from the palette.
pCO2.capita +
glayer(panel.pointLabel(mean(x), mean(y),
labels= group.value,
col=palOrdered[group.number],
cex=.8,
fontface=2, fontfamily='Palatino'))
dev.off()
pdf(file="figs/CO2_capitaDL.pdf")
library(directlabels)
direct.label(pCO2.capita, method='extreme.grid')
dev.off()
direct.label(gCO2.capita, method='extreme.grid')
##################################################################
## A panel for each year
##################################################################
pdf(file="figs/CO2_capita_panel.pdf")
xyplot(GNI.capita ~ CO2.capita | factor(Year), data=CO2data,
xlab="Carbon dioxide emissions (metric tons per capita)",
ylab="GNI per capita, PPP (current international $)",
groups=Country.Name, type='b',
auto.key=list(space='right'))
dev.off()
ggplot(data=CO2data, aes(x=CO2.capita, y=GNI.capita, colour=Country.Name)) +
facet_wrap(~ Year) + geom_point(pch=19) +
xlab('CO2 emissions (metric tons per capita)') +
ylab('GNI per capita, PPP (current international $)') +
theme_bw()
pdf(file="figs/CO2_capita_panel_labels.pdf")
xyplot(GNI.capita ~ CO2.capita | factor(Year), data=CO2data,
xlab="Carbon dioxide emissions (metric tons per capita)",
ylab="GNI per capita, PPP (current international $)",
groups=Country.Name, type='b',
par.settings=myTheme) +
glayer(panel.pointLabel(x, y, labels=group.value,
col=palOrdered[group.number], cex=0.7))
dev.off()
##################################################################
## Using variable size to encode an additional variable
##################################################################
library(classInt)
z <- CO2data$CO2.PPP
intervals <- classIntervals(z, n=4, style='fisher')
nInt <- length(intervals$brks) - 1
cex.key <- seq(0.5, 1.8, length=nInt)
idx <- findCols(intervals)
CO2data$cexPoints <- cex.key[idx]
pdf(file="figs/CO2pointsGG.pdf")
ggplot(data=CO2data, aes(x=CO2.capita, y=GNI.capita, colour=Country.Name)) +
facet_wrap(~ Year) + geom_point(aes(size=cexPoints), pch=19) +
xlab('Carbon dioxide emissions (metric tons per capita)') +
ylab('GNI per capita, PPP (current international $)') +
theme_bw()
dev.off()
pdf(file="figs/CO2points.pdf")
op <- options(digits=2)
tab <- print(intervals)
options(op)
key <- list(space='right',
title=expression(CO[2]/GNI.PPP),
cex.title=1,
## Labels of the key are the intervals strings
text=list(labels=names(tab), cex=0.85),
## Points sizes are defined with cex.key
points=list(col='black', pch=19,
cex=cex.key, alpha=0.7))
xyplot(GNI.capita ~ CO2.capita|factor(Year), data=CO2data,
xlab="Carbon dioxide emissions (metric tons per capita)",
ylab="GNI per capita, PPP (current international $)",
groups=Country.Name, key=key, alpha=0.7,
panel = panel.superpose,
panel.groups = function(x, y,
subscripts, group.number, group.value, ...){
panel.xyplot(x, y,
col = palOrdered[group.number],
cex = CO2data$cexPoints[subscripts])
panel.pointLabel(x, y, labels=group.value,
col=palOrdered[group.number],
cex=0.7)
}
)
dev.off()
##################################################################
## Travelling bubbles
##################################################################
library(gridSVG)
xyplot(GNI.capita ~ CO2.capita, data=CO2data,
xlab="Carbon dioxide emissions (metric tons per capita)",
ylab="GNI per capita, PPP (current international $)",
subset=Year==2000, groups=Country.Name,
## The limits of the graphic are defined
## with the entire dataset
xlim=extendrange(CO2data$CO2.capita),
ylim=extendrange(CO2data$GNI.capita),
panel=function(x, y, ..., subscripts, groups) {
color <- palOrdered[groups[subscripts]]
radius <- CO2data$CO2.PPP[subscripts]
## Size of labels
cex <- 1.1*sqrt(radius)
## Bubbles
grid.circle(x, y, default.units="native",
r=radius*unit(.25, "inch"),
name=trellis.grobname("points", type="panel"),
gp=gpar(col=color,
## Fill color ligther than border
fill=adjustcolor(color, alpha=.5),
lwd=2))
## Country labels
grid.text(label=groups[subscripts],
x=unit(x, 'native'),
## Labels above each bubble
y=unit(y, 'native') + 1.5 * radius *unit(.25, 'inch'),
name=trellis.grobname('labels', type='panel'),
gp=gpar(col=color, cex=cex))
})
## Duration in seconds of the animation
duration <- 20
nCountries <- nlevels(CO2data$Country.Name)
years <- unique(CO2data$Year)
nYears <- length(years)
## Intermediate positions of the bubbles
x_points <- animUnit(unit(CO2data$CO2.capita, 'native'),
id=rep(seq_len(nCountries), each=nYears))
y_points <- animUnit(unit(CO2data$GNI.capita, 'native'),
id=rep(seq_len(nCountries), each=nYears))
## Intermediate positions of the labels
y_labels <- animUnit(unit(CO2data$GNI.capita, 'native') +
1.5 * CO2data$CO2.PPP * unit(.25, 'inch'),
id=rep(seq_len(nCountries), each=nYears))
## Intermediate sizes of the bubbles
size <- animUnit(CO2data$CO2.PPP * unit(.25, 'inch'),
id=rep(seq_len(nCountries), each=nYears))
grid.animate(trellis.grobname("points", type="panel", row=1, col=1),
duration=duration,
x=x_points,
y=y_points,
r=size,
rep=TRUE)
grid.animate(trellis.grobname("labels", type="panel", row=1, col=1),
duration=duration,
x=x_points,
y=y_labels,
rep=TRUE)
countries <- unique(CO2data$Country.Name)
URL <- paste('http://en.wikipedia.org/wiki/', countries, sep='')
grid.hyperlink(trellis.grobname('points', type='panel', row=1, col=1),
URL, group=FALSE)
visibility <- matrix("hidden", nrow=nYears, ncol=nYears)
diag(visibility) <- "visible"
yearText <- animateGrob(garnishGrob(textGrob(years, .9, .15,
name="year",
gp=gpar(cex=2, col="grey")),
visibility="hidden"),
duration=20,
visibility=visibility,
rep=TRUE)
grid.draw(yearText)
grid.export("figs/bubbles.svg")