-
Notifications
You must be signed in to change notification settings - Fork 0
/
polar_plot.R
165 lines (126 loc) · 5.89 KB
/
polar_plot.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
#' polar_plot
#'
#' Produces a polar plot similar to that used in Cannon and Whitfield 2000. It used output from
#' binned_MannWhitney or a data structure create using polar_plot_prep
#'
#' @param bmw output from binned_MannWhitney
#' Additional parameters that can be set in the plot
#' @param llines names of periods or lines, default is c("Period 1","Period 2")
#' @param lcol1 line colour, default is c("black","gray50")
#' @param lcol2 point colour, default is c("black","gray50")
#' @param lfill fill colour, default is c("yellow","green")
#' @param lsig significance symbol colour, default is ("red","blue")
#'
#' @references
#' Whitfield, P.H. and A.J. Cannon. 2000. Polar plotting of seasonal hydrologic
#' and climatic data. Northwest Science 74: 76-80.
#'
#' Whitfield, P.H., Cannon, A.J., 2000. Recent variations in climate and hydrology
#' in Canada. Canadian Water Resources Journal 25: 19-65.
#'
#' @author Paul Whitfield <[email protected]>
#'
#' @export
#'
#'
#'
#'
polar_plot <- function (bmw, lcol1=c("black","gray50"),lcol2=c("black","gray50"),
lfill=c("yellow","green"),lsig=c("red","blue"))
{
dlabels <- c("Jan","Feb","Mar","Apr", "May", "Jun", "Jul","Aug","Sep","Oct","Nov","Dec")
dbreaks <- c(1,32,60,91,121,152,182,213,244,274,305, 335)
dbreaks <-dbreaks/365*2*pi
series <-bmw$series
llines <-array(NA,dim=2)
llines[1] <- paste(bmw$range1[1],"-",bmw$range1[2], bmw$bin_method)
llines[2] <- paste(bmw$range2[1],"-",bmw$range2[2], bmw$bin_method)
bins <- length(series[,1])
cpos <- c(1:bins)
cpos <- cpos/bins*365
cpos <- cpos/365*2*pi
cpos <- cpos[1:length(series[,2])]
rlim <- c(0,max(series[,2],series[,3])*1.01)
rlim[1] <- -rlim[2]/6
mdelta <-(rlim[2]-rlim[1])/20
xmax <-array(data=NA,dim=length(series[,1]))
xmin <-array(data=NA,dim=length(series[,1]))
par(mfrow=c(1,1))
# set basic polar plot be plotting first series which will have a radial line for each interval in set
par(cex.lab=0.75)
plotrix::radial.plot(as.numeric(series[,2]),cpos, rp.type="ps",
line.col=lcol1[1], point.symbols=16, point.col=lcol2[1],
labels=NULL,label.pos=cpos,
radial.lim=rlim, show.grid.labels=1,
start=3*pi/2, clockwise=TRUE,
main=bmw$Station_lname)
par(cex.lab=1.0)
# add the polygons for periods of increase and decreases
# create the polygons for increases and decreases
ppolys <- data.frame(cpos,as.numeric(series[,2]),as.numeric(series[,3]))
# add a row to span back to the first element
ppolys[bins+1,] <- ppolys[1,]
ppolys[bins+1,1]<- ppolys[bins+1,1]+ppolys[bins,1] # adjust so radians continue
p1 <- approx(ppolys[,1], n=10*(bins+1))
p2 <- approx(ppolys[,2], n=10*(bins+1))
p3 <- approx(ppolys[,3], n=10*(bins+1))
pp <- data.frame(p1[1],p1[2],p2[2],p3[2])
pp[,5] <- pp[,3]>pp[,4] # add a column of p1 greater than p2
intersect.points <- which(diff(pp[,5])!=0)
ipoints <-c(1,intersect.points,length(pp[,1]))
for (j in 2:length(ipoints)) {
polyx <-c(pp[ipoints[j-1]:ipoints[j],2],
rev(pp[ipoints[j-1]:ipoints[j],2]))
polyy <-c(pp[ipoints[j-1]:ipoints[j],3],
rev(pp[ipoints[j-1]:ipoints[j],4]))
test <-ifelse (pp[ipoints[j],5],1,2)
plotrix::radial.plot(polyy,polyx, rp.type="p", poly.col=lfill[test],line.col=NA,
radial.lim=rlim, cex=0.5,
start=3*pi/2, clockwise=TRUE, add=TRUE)
}
# replot the first data set so it appreas on top of poylgons and then add the second
plotrix::radial.plot(as.numeric(series[,2]),cpos, rp.type="ps", line.col=lcol1[1],
point.symbols=16,point.col=lcol2[1],
radial.lim=rlim, cex=0.5,
start=3*pi/2, clockwise=TRUE, add=TRUE)
plotrix::radial.plot(as.numeric(series[,3]),cpos, rp.type="ps", line.col=lcol1[2],
point.symbols=16,point.col=lcol2[2],
radial.lim=rlim, cex=0.5,
start=3*pi/2, clockwise=TRUE, add=TRUE)
gp <-pretty(rlim)
plotrix::radial.grid(labels=dlabels, label.pos=dbreaks, radlab=FALSE,
radial.lim=rlim, clockwise=TRUE,
start=3*pi/2,grid.col="gray20", show.radial.grid=FALSE,
start.plot=FALSE, grid.pos=gp[length(gp)])
# get positions for significance symbols
for (k in 1:bins){
xmax[k] <- max(series[k,2],series[k,3]) + mdelta
xmin[k] <-min(series[k,2],series[k,3]) - mdelta
}
# set up for plotting the significance arrows
ssym <- data.frame(cpos,series[,6],xmax,xmin)
names(ssym) <- c("cpos","t", "xmax", "xmin")
spsym <- ssym[ssym$t==1,]
snsym <- ssym[ssym$t==-1,]
for (k in 1:length(snsym[,1])){
plotrix::radial.plot(snsym$xmin[k],snsym$cpos[k], rp.type="s", point.symbols=24, point.col=lsig[1],bg=lsig[1],
radial.lim=rlim, cex=0.85,
start=3*pi/2, clockwise=TRUE, add=TRUE)
}
for (k in 1:length(spsym[,1])){
plotrix::radial.plot(spsym$xmax[k],spsym$cpos[k], rp.type="s", point.symbols=25, point.col=lsig[2],bg=lsig[2],
radial.lim=rlim, cex=0.85,
start=3*pi/2, clockwise=TRUE, add=TRUE)
}
# add legend
ltext <- c(llines,paste("Decrease in",bmw$variable),paste("Increase in",bmw$variable),
"Significant Decrease", "Significant Increase"," ", paste("Method",bmw$test_method),
paste("p<=",bmw$p_used))
lcols <- c(lcol1,lfill,lsig,"black","black","black")
lcols1 <- c(NA,NA,NA,NA,lsig)
lsym <- c(19,19,15,15,25,24,NA,NA,NA)
lcex <-c(0.8, 0.8, 1.25,1.25,0.9,0.9,NA,NA,NA)
lln <- c(1,1,NA,NA,NA,NA,NA,NA,NA,NA,NA)
legend(rlim[2]*1.6,rlim[2]*1.5, ltext,pch=lsym, col=lcols,bty="n", lty=lln, pt.cex=lcex,
pt.bg=lcols1, cex=0.75)
}