File: AnimatedGeoChart.R

package info (click to toggle)
r-cran-googlevis 0.6.2-1~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 2,968 kB
  • sloc: sh: 17; makefile: 5
file content (148 lines) | stat: -rw-r--r-- 3,658 bytes parent folder | download | duplicates (2)
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
## Animated Geo Maps based on ideas and code by:
## Manoj Ananthapadmanabhan and Anand Ramalingam

## Here we animate the percentage of vote for president won by the
## Democratic candidate in the elections from 1932 - 2008

## This demo requires the 'pscl' package
if( !is.element("pscl", installed.packages()[,1]) )
  install.packages("pscl")


require(pscl)
data(presidentialElections)

## Add min and max values to the data
df = data.frame(state=rep(c("Min", "Max"),20), 
		 		demVote=rep(c(0, 100),20),
  		 		year=sort(rep(seq(1932,2008,4),2)))


presidentialElections <- rbind(presidentialElections[,1:3], df)

## Create Geo Maps for each election
gvisData <- by(presidentialElections, list(year=presidentialElections$year), function(x){
	
	year <- x$year[1]	
	g <- gvisGeoChart(x, "state", "demVote", 
		options=list(region="US", dataMode="regions", resolution="provinces"),
		 chartid=paste("[", year, "]", sep=""))
	.data <- g$html$chart["jsData"]
	.data <-gsub("function ", "", .data)
	.data <- sub("\\] ()", "\\] = function ", .data)
	return(.data)	
}
)

animation <- "
var gvisData = {};

var Animation = {};
Animation.startYear = 1;
Animation.endYear = 20;
Animation.currentYear = Animation.startYear;
Animation.divCharts = {};

Animation.playAnimation = function() {
  if (Animation.currentYear > Animation.endYear) {
   	return;
  }
  document.getElementById('chart-header').innerHTML = 'Percent of the vote for president won by the Democratic candidate in year: '+ (1928 + 4*Animation.currentYear);
  if (Animation.currentYear > Animation.startYear) {
  	Animation.divCharts[Animation.currentYear-1].style.display = 'none';
  }
  Animation.divCharts[Animation.currentYear++].style.visibility = 'visible';
  setTimeout(Animation.playAnimation, 2000);
};

"

gvisChart <- '

// jsDrawChart
function drawChart() {
	var chart = {};
	var options ={};
	options["displayMode"] = "regions";
	options["width"] =   600;
	options["region"] = "US";
	options["height"] =  400;
  options["resolution"] = "provinces"
	options["colors"] = ["red", "blue"];
	
	for (var i = Animation.startYear; i<=Animation.endYear; i++) {
	   Animation.divCharts[i] = document.createElement("div");
	   Animation.divCharts[i].className = "pop-chart";
	   document.body.appendChild(Animation.divCharts[i]);
	   chart[i] = new google.visualization.GeoChart(Animation.divCharts[i]);
	   
	   var data = gvisData[(1928+4*i)]();
	   options["title"] = i;
	   chart[i].draw(data,options);
	}
		
	// Animation.playAnimation();
	setTimeout(Animation.playAnimation, 5000);
}

 
// jsDisplayChart 
function displayChart() {
  google.load("visualization", "1", { packages:["geochart"] }); 
  google.setOnLoadCallback(drawChart);
}
// jsChart 
displayChart()

'


htmlHead <- '

<html>
<head>
<title>US Presidential Election by State: 1932 - 2008</title>

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">

'

htmlFoot <-'

</script>
	
<style>
	.pop-chart {
		position: absolute;
		top: 50;
		left: 10;
		display: block;
		visibility: hidden;
	}
</style>
</head>
<body>

<div id="chart-header"></div>

</body>
</html>

'

page <- structure(
                  list(type="AnimatedGeoChart",
                       chartid="presidentialElections",
                       html=list(
                         header=htmlHead,
                         chart=c(animation, gvisData, gvisChart),
                         caption="",
                         footer=htmlFoot)
                       ),
                  class = c("gvis", "list")
                  )
plot(page)


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