Skip to content

Commit bde8451

Browse files
committed
Added shapefile of borough borders
1 parent 833ca0e commit bde8451

File tree

1 file changed

+83
-43
lines changed

1 file changed

+83
-43
lines changed

src/R/vis.R

+83-43
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,105 @@
1-
## Functions for predictions and visualizations of Bixi data
1+
## Visualizations of Bixi data
22
library(KernSmooth)
3-
rm(list=ls())
4-
## Data
5-
6-
#d<-read.csv('montreal-2011-sample.csv',header=FALSE)
7-
name <- function(day)
8-
{
9-
if (day <= 31)
10-
return(paste("Jan ", day))
11-
if (day <= 60)
12-
return(paste("Feb ", (day - 31)))
13-
if (day <= 91)
14-
return(paste("Mar ", (day - 60)))
15-
if (day <= 121)
16-
return(paste("Apr ",(day - 91)))
17-
}
18-
19-
3+
library(lubridate)
4+
library(maptools)
205

6+
rm(list=ls())
217

228

9+
## Data ##
2310
d<-read.csv('results2.out',header=FALSE,sep=' ') # Vic's
11+
mtl<-readShapePoly('./mtlshp/montreal_borough_borders.shp')
12+
13+
lon_index<-3 #
14+
lat_index<-2 #
15+
response_index<-4 #
16+
times_index<-1 #
2417

25-
lon_index<-3 #6
26-
lat_index<-2 #5
27-
response_index<-4 #12
28-
times_index<-1 #1
18+
## GIS business ##
19+
nit <- nrow(mtl)
20+
boundaries<-list()
21+
allb<-NULL
22+
for(i in 1:nit)
23+
{
24+
boundaries[[i]]<-slot(slot((slot(mtl, 'polygons'))[[i]], 'Polygons')[[1]], 'coords')
25+
allb<-rbind(allb,boundaries[[i]])
26+
}
27+
#plot(allb)
28+
add_lines<-function()
29+
{
30+
for(i in 1:nit)
31+
lines(boundaries[[i]],col='white')
32+
}
33+
2934

30-
## Colour setup
35+
## Colour setup ##
3136
colors<-c('black','blue','lightblue','lightblue','white')
3237
cus_col<-colorRampPalette(colors=colors, bias = 1, space = c("rgb", "Lab"),interpolate = c("linear", "spline"))
3338

3439
tcol <- topo.colors(12)
3540
xrange=list(range(d[,lon_index]),range(d[,lat_index]))
3641

37-
## vector of timestamps
42+
## Vector of timestamps ##
43+
hours<-d[,times_index] %% 100
44+
days<-floor( d[,times_index] / 100)
45+
day_0 <- ymd("2011-12-31",tz='GMT')
46+
47+
postimes<- day_0 + ddays(days) + dhours(hours+1)
48+
postimes<-with_tz(postimes,tz='EST')
49+
d[,times_index]<-postimes
50+
51+
valid_times <- interval(ymd(20120403,tz='EST'), ymd(20120501,tz='EST'))
52+
d<-subset(d,postimes %within% valid_times)
3853
times<-unique(d[,times_index])
39-
#times<-sort(times)
40-
54+
55+
flux<-numeric(length(times))
56+
maxdens<-1
57+
4158
## One map per unique timestamp
42-
for(i in 1:length(times))
59+
collect<-function(gen_plot=FALSE)
4360
{
44-
45-
at_time<-which(d[,times_index]==times[i])
46-
hour<-(times[i] %% 100) +1
47-
lon<-d[at_time,lon_index]
48-
lat<-d[at_time,lat_index]
49-
response<-d[at_time,response_index]
50-
51-
dens<-bkde2D(cbind(lon,lat,response),range.x=xrange,gridsize=c(300,300),bandwidth=0.0015)
52-
png(paste(10000+i,".png",sep=''))
53-
image(dens$x1,dens$x2,dens$fhat,col=cus_col(30),xlab='Lon',ylab='lat',main=paste(name(floor(times[i]/100)),'hour:',hour))
54-
dev.off()
55-
cat(times[i],'\n')
61+
for(i in 1:length(times)) #150:700) #150:700)
62+
{
63+
at_time<-which(d[,times_index]==times[i])
64+
lon<-d[at_time,lon_index]
65+
lat<-d[at_time,lat_index]
66+
response<-d[at_time,response_index]
67+
68+
flux[i]<-sum(response)
69+
70+
dens<-bkde2D(cbind(lon,lat,response),range.x=xrange,gridsize=c(300,300),bandwidth=0.0015)
71+
maxdens<-max(c(maxdens,dens$fhat*flux[i]))
72+
73+
if(gen_plot)
74+
{
75+
png(paste("imgs/",format(times[i],"%y-%m-%d-%H"),".png",sep='') )
76+
image(dens$x1,dens$x2,dens$fhat*flux[i],col=cus_col(30),xlab='Lon',ylab='lat',main= format(times[i],"%y-%m-%d: %H h") ,zlim=c(0,360900))
77+
add_lines()
78+
dev.off()
79+
}
80+
print(format(times[i],"%y-%m-%d: %H h"))
81+
}
82+
return(flux)
5683
}
5784

58-
## Roll it
59-
#system('convert -delay 10 *.png head.gif')
6085

86+
flux<-collect()
87+
88+
ind_order<-order(times)
89+
90+
png('whole_span.png',width=960)
91+
par(cex=1.5,lwd=2)
92+
plot(times[ind_order],flux[ind_order],xlab='Time',ylab='Flux (bikes/hour)',type='l')
93+
dev.off()
94+
95+
## Pull out a single week
96+
april18 <- interval(ymd(20120407,tz='EST'), ymd(20120415,tz='EST'))
97+
span_index<-times[ind_order] %within% april18
98+
99+
png('one_week.png',width=960)
100+
par(cex=1.5,lwd=2)
101+
plot(times[ind_order[span_index]],flux[ind_order[span_index]],xlab='Time',ylab='Flux (bikes/hour)',type='l')
102+
dev.off()
61103

62104

63-
# Legending
64-
# colorlegend(posx=c(0.84,0.87),posy=c(0.15,0.85),col=cus_col(100),zlim=c(0,0.3),digit=2,main='Posterior\nprobability',zval=seq(0,0.3,0.05) )
65105

0 commit comments

Comments
 (0)