forked from boefraty74/todelete_TutorialFunnelPlot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_RV_v2_00.r
362 lines (270 loc) · 11.5 KB
/
script_RV_v2_00.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
354
355
356
357
358
359
360
361
362
# Copyright (c) Microsoft Corporation. All rights reserved.
# Third Party Programs. This software enables you to obtain software applications from other sources.
# Those applications are offered and distributed by third parties under their own license terms.
# Microsoft is not developing, distributing or licensing those applications to you, but instead,
# as a convenience, enables you to use this software to obtain those applications directly from
# the application providers.
# By using the software, you acknowledge and agree that you are obtaining the applications directly
# from the third party providers and under separate license terms, and that it is your responsibility to locate,
# understand and comply with those license terms.
# Microsoft grants you no license rights for third-party software or applications that is obtained using this software.
# See also:
# http://stats.stackexchange.com/questions/5195/how-to-draw-funnel-plot-using-ggplot2-in-r/5210#5210
#RVIZ_IN_PBI_GUIDE:BEGIN:Removed to enable R-visual in Power BI
#dataset = read.csv(file = "dataset.csv", header = TRUE, sep = ",", stringsAsFactors = FALSE)
#RVIZ_IN_PBI_GUIDE:END:Removed to enable R-visual in Power BI
###############Library Declarations###############
libraryRequireInstall = function(packageName, ...)
{
if(!require(packageName, character.only = TRUE))
warning(paste("*** The package: '", packageName, "' was not installed ***", sep=""))
}
libraryRequireInstall("ggplot2")
libraryRequireInstall("scales")
###############Internal parameters definitions#################
# Set of parameters, which are not exported to GUI
##PBI_PARAM Color of scatterplot points
#Type:string, Default:"orange", Range:NA, PossibleValues:"orange","blue","green","black"
pointsCol = "orange"
#PBI_PARAM Transparency of scatterplot points
#Type:numeric, Default:0.4, Range:[0,1], PossibleValues:NA, Remarks: NA
transparency = 0.4
##PBI_PARAM Color of baseline
#Type:string, Default:"blue", Range:NA, PossibleValues:"orange","blue","green","black"
lineColor = "blue"
#PBI_PARAM Sparsification of scatterplot points
#Type:bool, Default:TRUE, Range:NA, PossibleValues:NA, Remarks: NA
sparsify = TRUE
#PBI_PARAM Size of points on the plot
#Type:numeric, Default: 1 , Range:[0.1,5], PossibleValues:NA, Remarks: NA
pointCex = 1
#PBI_PARAM Confidence level line
#Type:numeric, Default: 0.75 , Range:[0,1], PossibleValues:NA, Remarks: GUI input is predefined set of values
conf1 = 0.95
#PBI_PARAM Confidence level line #2
#Type:numeric, Default: 0.95 , Range:[0,1], PossibleValues:NA, Remarks: NA
conf2 = 0.99
##PBI_PARAM Precentage or Ratio ?
#Type:bool, Default:TRUE, Range:NA, PossibleValues:NA
axisXisPercentage = TRUE # ratio or percentage
##PBI_PARAM Axis X format
#Type:string, Default:"comma", Range:NA, PossibleValues:"none","comma","scientific","dollar"
scaleXformat = "comma"
##PBI_PARAM Axis Y format
#Type:string, Default:"none", Range:NA, PossibleValues:"none","comma","scientific"
scaleYformat = "none"
#PBI_PARAM Size of labels on axes
sizeLabel = 12
#PBI_PARAM Size of ticks on axes
sizeTicks = 6
#PBI_PARAM Size of labels on axes
colLabel = "gray"
#PBI_PARAM is vertical plot
verticalPlot = FALSE
#PBI_PARAM Minimal number of points for funnel plot
minPoints = 10
#PBI_PARAM Size of warnings font
sizeWarn = 11
###############Internal functions definitions#################
# Functions, used only for this visual
# Post-process text string to show on plot
# if very very long abbreviate
# if looooooong convert to lo...
# if shorter than maxChar remove
cutStr2Show = function(strText, strCex = 0.8, abbrTo = 100, isH = TRUE, maxChar = 3, partAvailable = 1)
{
# partAvailable, wich portion of window is available, in [0,1]
if(is.null(strText))
return (NULL)
SCL = 0.075*strCex/0.8
pardin = par()$din
gStand = partAvailable*(isH*pardin[1]+(1-isH)*pardin[2]) /SCL
# if very very long abbreviate
if(nchar(strText)>abbrTo && nchar(strText)> 1)
strText = abbreviate(strText, abbrTo)
# if looooooong convert to lo...
if(nchar(strText)>round(gStand) && nchar(strText)> 1)
strText = paste(substring(strText,1,floor(gStand)),"...",sep="")
# if shorter than maxChar remove
if(gStand<=maxChar)
strText = NULL
return(strText)
}
#randomly remove points from scatter if too many
SparsifyScatter = function (xyDataFrame, numXstrips = 9, numYstrips = 7, minMaxPoints = c(3000,9000), minmaxInStrip = c(900,9000), maxInCell = 300, remDuplicated = TRUE)
{
N_big = N = nrow(xyDataFrame)
usePoints = rep(TRUE,N)
if(N <= minMaxPoints[1]) # do nothing
return (usePoints)
if(remDuplicated) # remove duplicated
{
usePoints = usePoints & (!duplicated(xyDataFrame))
N = sum(usePoints)
}
if(N <= minMaxPoints[1]) # do nothing
return (usePoints)
rangeX = range(xyDataFrame[,1])
rangeY = range(xyDataFrame[,2])
gridX = seq(rangeX[1],rangeX[2], length.out = numXstrips + 1)
gridY = seq(rangeY[1],rangeY[2], length.out = numYstrips + 1)
#go cell by cell and sparsify
for (iX in seq(1,numXstrips))
{
smallRangeX = c(gridX[iX],gridX[iX+1])
inStrip = xyDataFrame[,1]>= smallRangeX[1] & xyDataFrame[,1]<= smallRangeX[2] & usePoints
if(sum(inStrip) > minmaxInStrip[1])
for (iY in seq(1,numYstrips))
{
smallRangeY = c(gridY[iY],gridY[iY+1])
inCell = xyDataFrame[,2]>= smallRangeY[1] & xyDataFrame[,2]<= smallRangeY[2] & inStrip
if(sum(inCell) > maxInCell)
{
inCellIndexes = seq(1,N_big)[inCell]
#randomly select maxInCell out of inCellIndexes
iii = sample(inCellIndexes,size = sum(inCell) - maxInCell, replace = FALSE)
usePoints[iii] = FALSE
}
}
}
N = sum(usePoints)
#if by the end still too many points --> go on whole set
if(N > minMaxPoints[2])
{
inIndexes = seq(1,N_big)[usePoints]
#randomly select minMaxPoints[2] out of inIndexes
iii = sample(inIndexes,size = minMaxPoints[2], replace = FALSE)
usePoints[-iii] = FALSE
}
return (usePoints)
}
# return FALSE if canvas is too small
goodPlotDimension = function(minWidthInch = 3,minHeightInch = 2.2)
{
re = (par()$din[1] > minWidthInch) & (par()$din[2] > minHeightInch)
return(re)
}
#tweak the limits of the axis
NiceLimitsAxis <- function(axisData, baseline =NULL, isPositive = TRUE)
{
limsA = c(min(axisData), max(axisData)) # default
if(is.null(baseline))
baseline = sum(limsA)/2
limsA = (limsA - mean(limsA))*1.3 + baseline # centralize
limsA[1] = min(limsA[1],min(axisData)) # include outliers
limsA[2] = max(limsA[2],max(axisData)) # include outliers
if(limsA[1] < 0 && isPositive) # don't include region far away from 0
{
temp = -0.02 * (limsA[2])
limsA[1] = max(temp, limsA[1])
}
return(limsA)
}
############# Input validation & initializations #############
if(conf2 < conf1)# swap
{ temp = conf1; conf1 = conf2; conf2 = temp}
validToPlot = TRUE
pbiWarning = ""
gpd = goodPlotDimension()
if(validToPlot && !gpd) # too small canvas
{
validToPlot = FALSE
pbiWarning1 = "Visual is "
pbiWarning1 = cutStr2Show(pbiWarning1, strCex = sizeWarn/6, partAvailable = 0.9)
pbiWarning2 = "too small "
pbiWarning2 = cutStr2Show(pbiWarning2, strCex = sizeWarn/6, partAvailable = 0.9)
pbiWarning<-paste(pbiWarning1, "<br>", pbiWarning2, sep="")
sizeWarn = 8 #smaller
}
if(validToPlot && (ncol(dataset)< 2)) # invalid input
{
validToPlot = FALSE
pbiWarning1 = "Both population and occurrence are required"
pbiWarning = cutStr2Show(pbiWarning1, strCex = sizeWarn/6, partAvailable = 0.9)
}
if(validToPlot)
validData = complete.cases(dataset) & (dataset[,1]>=dataset[,2])
if(validToPlot && (sum(validData) < minPoints)) # not enough data samples
{
validToPlot = FALSE
pbiWarning1 = "Not enough VALID data samples"
pbiWarning1 = cutStr2Show(pbiWarning1, strCex = sizeWarn/6, partAvailable = 0.9)
pbiWarning2 = "for funnel plot"
pbiWarning2 = cutStr2Show(pbiWarning2, strCex = sizeWarn/6, partAvailable = 0.9)
pbiWarning<-paste(pbiWarning1, "\n", pbiWarning2, sep="")
}
############# Main code #####################
if(validToPlot)
{
dataset = dataset[validData,]# keep only valid
namesDS = names(dataset)
countValue = dataset[,1]
p = dataset[,2]/dataset[,1]
p.se <- sqrt((p*(1-p)) / (countValue))
df <- data.frame(p, countValue, p.se)
## common effect (fixed effect model)
p.fem <- weighted.mean(p[p.se>0], 1/p.se[p.se>0]^2)
## lower and upper limits, based on FEM estimator
zLow = qnorm(conf1)
zUp = qnorm(conf2)
if(axisXisPercentage)
{
mult = 100
entryWordLabelY = "Percentage of "
}
else
{
mult = 1
entryWordLabelY = "Ratio of "
}
number.seq <- seq(min(countValue), max(countValue), 1000)
number.llconf1 <- (p.fem - zLow * sqrt((p.fem*(1-p.fem)) / (number.seq)))*mult
number.ulconf1 <- (p.fem + zLow * sqrt((p.fem*(1-p.fem)) / (number.seq)))*mult
number.llconf2 <- (p.fem - zUp * sqrt((p.fem*(1-p.fem)) / (number.seq)))*mult
number.ulconf2 <- (p.fem + zUp * sqrt((p.fem*(1-p.fem)) / (number.seq)))*mult
yAxis = p*mult
p.fem = p.fem*mult
dfCI <- data.frame(number.llconf1, number.ulconf1, number.llconf2, number.ulconf2, number.seq, p.fem)
#tweak the limits of the y-axis
limsY = NiceLimitsAxis(axisData = yAxis, baseline = p.fem)
xLabText = cutStr2Show( namesDS[1], strCex = sizeLabel/6, isH = TRUE, partAvailable = 0.85)
yLabText = cutStr2Show( paste(entryWordLabelY, namesDS[2], sep =""), strCex = sizeLabel/6, isH = FALSE, partAvailable = 0.85)
## draw plot
if(sparsify)
drawPoints = SparsifyScatter(dataset)# remove points from dense regions
else
drawPoints = SparsifyScatter(dataset,minMaxPoints = c(Inf,Inf))
fp <- ggplot(aes(x = countValue[drawPoints], y = yAxis[drawPoints]), data = df[drawPoints,])
fp <- fp + geom_point(shape = 19, colour = alpha(pointsCol,transparency), size = pointCex*2 )
fp <- fp + geom_line(aes(x = number.seq, y = number.llconf1),linetype = 1, colour = "green",data = dfCI)
fp <- fp + geom_line(aes(x = number.seq, y = number.ulconf1),linetype = 1, colour = "green", data = dfCI)
fp <- fp + geom_line(aes(x = number.seq, y = number.llconf2),linetype = 2, colour = "red",data = dfCI)
fp <- fp + geom_line(aes(x = number.seq, y = number.ulconf2), linetype = 2, colour = "red",data = dfCI)
fp <- fp + geom_hline(aes(yintercept = p.fem), data = dfCI, colour = lineColor, linetype = 4)
if(scaleYformat %in% c("none"))
fp <- fp + scale_y_continuous(limits =limsY)
if(scaleYformat %in% c("comma"))
fp <- fp + scale_y_continuous(limits =limsY, labels = comma)
if(scaleYformat %in% c("scientific"))
fp <- fp + scale_y_continuous(limits =limsY, labels = scientific)
if(scaleXformat %in% c("comma"))
fp <- fp + scale_x_continuous(labels = comma)
if(scaleXformat %in% c("dollar"))
fp <- fp + scale_x_continuous(labels = dollar)
if(scaleXformat %in% c("scientific"))
fp <- fp + scale_x_continuous(labels = scientific)
fp <- fp + xlab(xLabText) + ylab(yLabText) + theme_bw()
if(verticalPlot)
fp <- fp + coord_flip()
}else{# empty plot
fp <- ggplot()
}
#add warning as title
fp = fp + labs (title = pbiWarning, caption = NULL) + theme_bw() +
theme(plot.title = element_text(hjust = 0.5, size = sizeWarn),
axis.title=element_text(size = sizeLabel, colour = colLabel),
axis.text=element_text(size = sizeTicks),
panel.border = element_blank(), axis.line = element_line())
if(!validToPlot) # remove axes from empty plot
fp = fp + theme(axis.line = element_blank())
print(fp)