-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutility.function.R
1334 lines (1257 loc) · 44 KB
/
utility.function.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
### Find the point on each curve which maximizes the distance to the
### plot border or to another curve.
far.from.others.borders <- function(all.groups,...,debug=FALSE){
group.data <- split(all.groups, all.groups$group)
group.list <- list()
for(groups in names(group.data)){
## Run linear interpolation to get a set of points on which we
## could place the label (this is useful for e.g. the lasso path
## where there are only a few points plotted).
one.group <- group.data[[groups]]
approx.list <- with(one.group, approx(x, y))
if(debug){
with(approx.list, grid.points(x, y, default.units="cm"))
}
group.list[[groups]] <- data.frame(
approx.list,
groups,
label=one.group$label[1])
}
output.list <- list()
for(group.i in seq_along(group.list)){
one.group <- group.list[[group.i]]
## From Mark Schmidt: "For the location of the boxes, I found the
## data point on the line that has the maximum distance (in the
## image coordinates) to the nearest data point on another line or
## to the image boundary."
dist.mat <- matrix(NA, length(one.group$x), 3)
colnames(dist.mat) <- c("x","y","other")
## dist.mat has 3 columns: the first two are the shortest distance
## to the nearest x and y border, and the third is the shortest
## distance to another data point.
for(xy in c("x", "y")){
xy.vec <- one.group[,xy]
xy.mat <- rbind(xy.vec, xy.vec)
lim.fun <- get(sprintf("%slimits", xy))
diff.mat <- xy.mat - lim.fun()
dist.mat[,xy] <- apply(abs(diff.mat), 2, min)
}
other.groups <- group.list[-group.i]
other.df <- do.call(rbind, other.groups)
for(row.i in 1:nrow(dist.mat)){
r <- one.group[row.i,]
other.dist <- with(other.df, (x-r$x)^2 + (y-r$y)^2)
dist.mat[row.i,"other"] <- sqrt(min(other.dist))
}
shortest.dist <- apply(dist.mat, 1, min)
picked <- calc.boxes(one.group[which.max(shortest.dist),])
## Mark's label rotation: "For the angle, I computed the slope
## between neighboring data points (which isn't ideal for noisy
## data, it should probably be based on a smoothed estimate)."
left <- max(picked$left, min(one.group$x))
right <- min(picked$right, max(one.group$x))
neighbors <- approx(one.group$x, one.group$y, c(left, right))
slope <- with(neighbors, (y[2]-y[1])/(x[2]-x[1]))
picked$rot <- 180*atan(slope)/pi
output.list[[group.i]] <- picked
}
output <- do.call(rbind, output.list)
##browser()
output
}
label.endpoints <- function
### Make a Positioning Method that labels a certain x value.
(FUN,
### FUN(d$x) should return an index of which point to label. for
### example you can use which.min or which.max.
HJUST
### hjust of the labels.
){
stopifnot(is.function(FUN))
stopifnot(length(HJUST)==1)
stopifnot(is.numeric(HJUST))
stopifnot(is.finite(HJUST))
function(d,...)gapply(d,function(d,...){
i <- FUN(d$x)==d$x
if(length(i)==0){
data.frame()
}else{
sub.df <- d[i,]
if(nrow(sub.df) > 1){
y.target <- mean(range(sub.df$y))
sub.df <- sub.df[1,]
sub.df$y <- y.target
}
sub.df$hjust <- HJUST
sub.df$vjust <- 0.5
sub.df
}
})
### A Positioning Method like first.points or last.points.
}
dl.combine <- structure(function # Combine output of several methods
### Apply several Positioning methods to the original data frame.
(...
### Several Positioning Methods.
){
FUNS <- list(...)
pf <- function(d,...){
dfs <- lapply(FUNS,apply.method,d,...)
res <- data.frame()
for(df in dfs){
## if cex is undefined, we will get NAs which will not be
## plotted.
if(!"cex"%in%names(df)){
df$cex <- 1
}
## we need to do merge to keep all the columns around.
if(nrow(res))res <- merge(df,res,all=TRUE)
else res <- df
}
res
}
pf
### A Positioning Method that returns the combined data frame after
### applying each specified Positioning Method.
},ex=function(){
## Simple example: label the start and endpoints
if(require(nlme) && require(lattice)){
ratplot <- xyplot(
weight~Time|Diet,BodyWeight,groups=Rat,type='l',layout=c(3,1))
both <- dl.combine("first.points","last.points")
rat.both <- direct.label(ratplot,"both")
print(rat.both)
## same as repeated call to direct.label:
rat.repeated <-
direct.label(direct.label(ratplot,"last.points"),"first.points")
print(rat.repeated)
}
## same with ggplot2:
if(require(nlme) && require(ggplot2)){
rp2 <- qplot(
Time,weight,data=BodyWeight,geom="line",facets=.~Diet,colour=Rat)
print(direct.label(direct.label(rp2,"last.points"),"first.points"))
print(direct.label(rp2,"both"))
}
## more complex example: first here is a function for computing the
## lasso path.
mylars <- function
## Least angle regression algorithm for calculating lasso solutions.
(x,
## Matrix of predictor variables.
y,
## Vector of responses.
epsilon=1e-6
## If correlation < epsilon, we are done.
){
xscale <- scale(x) # need to work with standardized variables
b <- rep(0,ncol(x))# coef vector starts at 0
names(b) <- colnames(x)
ycor <- apply(xscale,2,function(xj)sum(xj*y))
j <- which.max(ycor) # variables in active set, starts with most correlated
alpha.total <- 0
out <- data.frame()
while(1){## lar loop
xak <- xscale[,j] # current variables
r <- y-xscale%*%b # current residual
## direction of parameter evolution
delta <- solve(t(xak)%*%xak)%*%t(xak)%*%r
## Current correlations (actually dot product)
intercept <- apply(xscale,2,function(xk)sum(r*xk))
## current rate of change of correlations
z <- xak%*%delta
slope <- apply(xscale,2,function(xk)-sum(z*xk))
## store current values of parameters and correlation
out <- rbind(out,data.frame(variable=colnames(x),
coef=b,
corr=abs(intercept),
alpha=alpha.total,
arclength=sum(abs(b)),
coef.unscaled=b/attr(xscale,"scaled:scale")))
if(sum(abs(intercept)) < epsilon)#corr==0 so we are done
return(transform(out,s=arclength/max(arclength)))
## If there are more variables we can enter into the regression,
## then see which one will cross the highest correlation line
## first, and record the alpha value of where the lines cross.
d <- data.frame(slope,intercept)
d[d$intercept<0,] <- d[d$intercept<0,]*-1
d0 <- data.frame(d[j[1],])# highest correlation line
d2 <- data.frame(rbind(d,-d),variable=names(slope))#reflected lines
## Calculation of alpha for where lines cross for each variable
d2$alpha <- (d0$intercept-d2$intercept)/(d2$slope-d0$slope)
subd <- d2[(!d2$variable%in%colnames(x)[j])&d2$alpha>epsilon,]
subd <- subd[which.min(subd$alpha),]
nextvar <- subd$variable
alpha <- if(nrow(subd))subd$alpha else 1
## If one of the coefficients would hit 0 at a smaller alpha
## value, take it out of the regression and continue.
hit0 <- xor(b[j]>0,delta>0)&b[j]!=0
alpha0 <- -b[j][hit0]/delta[hit0]
takeout <- length(alpha0)&&min(alpha0) < alpha
if(takeout){
i <- which.min(alpha0)
alpha <- alpha0[i]
}
b[j] <- b[j]+alpha*delta ## evolve parameters
alpha.total <- alpha.total+alpha
## add or remove a variable from the active set
j <- if(takeout)j[j!=which(names(i)==colnames(x))]
else c(j,which(nextvar==colnames(x)))
}
}
## Calculate lasso path, plot labels at two points: (1) where the
## variable enters the path, and (2) at the end of the path.
if(require(lars) && require(lattice)){
data(diabetes,envir=environment())
dres <- with(diabetes,mylars(x,y))
P <- xyplot(coef~arclength,dres,groups=variable,type="l")
mylasso <- dl.combine("lasso.labels", "last.qp")
plot(direct.label(P,"mylasso"))
}
})
gapply.fun <- structure(function # Direct label groups independently
### Makes a function you can use to specify the location of each group
### independently.
(expr
### Expression that takes a subset of the d data frame, with data from
### only a single group, and returns the direct label position.
){
foo <- substitute(expr)
f <- function(d,...)eval(foo)
src <- paste("gapply.fun(",paste(deparse(foo),collapse="\n"),")",sep="")
pf <- structure(function(d,...)gapply(d,f,...),"source"=src)
pf
### A Positioning Function.
},ex=function(){
complicated <- list(dl.trans(x=x+10),
gapply.fun(d[-2,]),
rot=c(30,180))
if(require(lattice)){
direct.label(dotplot(VADeaths,type="o"),complicated,TRUE)
}
})
dl.trans <- structure(function # Direct label data transform
### Make a function that transforms the data. This is for conveniently
### making a function that calls transform on the data frame, with the
### arguments provided. See examples.
(...
### Arguments to pass to transform.
){
L <- as.list(match.call())[-1]
pf <- function(d,...)do.call("transform",c(list(d),L))
pf
### A Positioning Function.
},ex=function(){
complicated <- list(dl.trans(x=x+10),
gapply.fun(d[-2,]),
rot=c(30,180))
if(require(lattice)){
direct.label(dotplot(VADeaths,type="o"),complicated,TRUE)
}
})
dl.move <- structure(function # Manually move a direct label
### Sometimes there is 1 label that is placed oddly by another
### Positioning Function. This function can be used to manually place
### that label in a good spot.
(group,
### Group to change.
x,
### Horizontal position of the new label.
y,
### Vertical position of the new label. If missing(y) and !missing(x)
### then we will calculate a new y value using linear interpolation.
...
### Variables to change for the specified group
){
L <- list(...)
pos <- list()
if(!missing(x))pos$x <- x
if(!missing(y))pos$y <- y
pf <- function(d,...,axes2native){
native <- axes2native(do.call(data.frame,pos))
## first convert user-specified axes units to cm
for(var in names(pos)){
u <- unit(native[[var]],"native")
L[[var]] <- convertUnit(u,"cm",var,"location",var,"location")
}
v <- d$groups==group
for(N in names(L))
d[v,N] <- L[[N]]
## maybe generalize this to be symmetric on x and y one day?
if("x" %in% names(L) && (!"y" %in% names(L))){
orig <- attr(d,"orig.data")
orig <- orig[orig$label==group,]
## do linear interpolation to find a good y-value
f <- with(orig,approxfun(x,y))
d[v,"y"] <- f(L$x)
}
d
}
pf
### A Positioning Function that moves a label into a good spot.
},ex=function(){
if(require(ggplot2) && require(lattice)){
scatter <- xyplot(jitter(cty)~jitter(hwy),mpg,groups=class,aspect=1)
dlcompare(list(scatter),
list("extreme.grid",
`+dl.move`=list(extreme.grid,dl.move("suv",15,15))))
p <- qplot(log10(gamma),rate,data=svmtrain,group=data,colour=data,
geom="line",facets=replicate~nu)
adjust.kif <- dl.move("KIF11",-0.9,hjust=1,vjust=1)
dlcompare(list(p+xlim(-8,7)),
list("last.points",
`+dl.move`=list(last.points,adjust.kif)))
}
})
### Jitter the label positions.
dl.jitter <- dl.trans(x=jitter(x),y=jitter(y))
calc.boxes <- function
### Calculate boxes around labels, for collision detection.
(d,
debug=FALSE,
...
){
vp <- current.viewport()
convert <- function(str.prop, worh=str.prop){
conv <- get(paste("convert",worh,sep=""))
stri <- get(paste("string", str.prop, sep=""))
as.numeric(sapply(seq_along(d$groups),function(i){
if("cex"%in%names(d))vp$gp <- gpar(cex=d$cex[i])
pushViewport(vp)
if(debug)grid.rect() ##highlight current viewport
cm <- conv(stri(as.character(d$label[i])),"cm")
popViewport()
cm
}))
}
## abs since we have a weird bug with ggplot2 sometimes
d$w <- abs(convert("Width"))
d$h <- abs(convert("Height"))
d$descent <- abs(convert("Descent", "Height"))
calc.borders(d)
}
### Calculate big boxes around the means of each cluster.
big.boxes <- list("get.means","calc.boxes","enlarge.box")
### Point halfway between the min and max
midrange <- function(x){
r <- range(x)
(r[2]-r[1])/2+r[1]
}
### Point in the middle of the min and max for each group.
visualcenter <- gapply.fun(dl.summarize(d,x=midrange(x),y=midrange(y)))
### Positioning Function for the mean of each cluster of points.
get.means <-
gapply.fun(dl.summarize(d,x=mean(x),y=mean(y)))
calc.borders <- function
### Calculate bounding box based on newly calculated width and height.
(d,
### Data frame of point labels, with new widths and heights in the w
### and h columns.
...
### ignored.
){
for(just in c("hjust","vjust")){
if(!just %in% names(d)){
d[,just] <- 0.5
}
}
if(!"descent" %in% names(d)){
d$descent <- 0
}
d$top <- d$y+(1-d$vjust)*d$h
d$bottom <- d$y-d$vjust*d$h - d$descent
d$right <- d$x+(1-d$hjust)*d$w
d$left <- d$x-d$hjust*d$w
d
}
polygon.method <- function
### Make a Positioning Method that places non-overlapping speech
### polygons at the first or last points.
(top.bottom.left.right,
### Character string indicating what side of the plot to label.
offset.cm=0.1,
### Offset from the polygon to the most extreme data point.
padding.cm=0.05,
### Padding inside the polygon.
custom.colors=NULL
### Positioning method applied just before draw.polygons, can set
### box.color and text.color for custom colors.
){
if(is.null(custom.colors)){
custom.colors <- gapply.fun({
rgb.mat <- col2rgb(d[["colour"]])
d$text.color <- with(data.frame(t(rgb.mat)), {
gray <- 0.3*red + 0.59*green + 0.11*blue
ifelse(gray/255 < 0.5, "white", "black")
})
d
})
}
opposite.side <- c(
left="right",
right="left",
top="bottom",
bottom="top")[[top.bottom.left.right]]
direction <- if(
top.bottom.left.right %in% c("bottom", "left")
) -1 else 1
min.or.max <- if(
top.bottom.left.right %in% c("top", "right")
) max else min
if(top.bottom.left.right %in% c("left", "right")){
min.or.max.xy <- "x"
qp.target <- "y"
qp.max <- "top"
qp.min <- "bottom"
padding.h.factor <- 2
padding.w.factor <- 1
limits.fun <- ylimits
reduce.method <- "reduce.cex.lr"
}else{
min.or.max.xy <- "y"
qp.target <- "x"
qp.max <- "right"
qp.min <- "left"
padding.h.factor <- 1
padding.w.factor <- 2
limits.fun <- xlimits
reduce.method <- "reduce.cex.tb"
}
list(
paste0(top.bottom.left.right, ".points"),
function(d,...){
## set the end of the speech polygon to the original data point.
for(xy in c("x", "y")){
extra.coord <- sprintf(# e.g. left.x
"%s.%s", opposite.side, xy)
d[[extra.coord]] <- d[[xy]]
}
## set the speech polygon position to the min or max of all
## label positions. e.g. max
d[[min.or.max.xy]] <- min.or.max(d[[min.or.max.xy]]) + offset.cm*direction
d
},
"calc.boxes",
reduce.method,
function(d, ...){
d$h <- d$h + padding.cm * padding.h.factor
d$w <- d$w + padding.cm * padding.w.factor
d
},
"calc.borders",
qp.labels(
qp.target,
qp.min,
qp.max,
make.tiebreaker(min.or.max.xy, qp.target),
limits.fun),
"calc.borders",
custom.colors,
"draw.polygons")
}
### Draw polygons around label positions.
draw.polygons <- function(d,...){
for(side in c("left", "right", "top", "bottom")){
for(xy in c("x", "y")){
col.name <- paste0(side, ".", xy)
if(!col.name %in% names(d)){
d[[col.name]] <- NA
}
}
}
if(! "box.color" %in% names(d)){
d$box.color <- "black"
}
if(! "text.color" %in% names(d)){
d$text.color <- "white"
}
for(i in 1:nrow(d))with(d[i,], {
L <- list(
x=c(left.x, left, top.x, right, right.x, right, bottom.x, left),
y=c(left.y, top, top.y, top, right.y, bottom, bottom.y, bottom))
for(xy.name in names(L)){
xy <- L[[xy.name]]
L[[xy.name]] <- xy[!is.na(xy)]
}
grid::grid.polygon(
L$x, L$y,
default.units="cm",
gp=grid::gpar(col=box.color, fill=colour),
name="directlabels.draw.polygon"
)
})
d$colour <- d$text.color
d
}
### Positioning Function that draws boxes around label positions. Need
### to have previously called calc.boxes. Does not edit the data
### frame.
draw.rects <- function(d,...){
if(is.null(d$box.color))d$box.color <- "black"
if(is.null(d$fill))d$fill <- "white"
for(i in 1:nrow(d)){
with(d[i,], grid.rect(
gp = gpar(col = box.color, fill = fill),
vp = viewport(x, y, w, h, "cm", c(hjust, vjust), angle=rot),
name="directlabels.draw.rects"
))
}
d
}
### Sequentially bump labels up, starting from the bottom, if they
### collide with the label underneath.
bumpup <- function(d,...){
## If there is only 1, then there is no collision detection to do.
if(nrow(d) == 1)return(d)
d <- calc.boxes(d)[order(d$y),]
"%between%" <- function(v,lims)lims[1]<v&v<lims[2]
obox <- function(x,y){
tocheck <- with(x,c(left,(right-left)/2+left,right))
tocheck %between% with(y,c(left,right))
}
for(i in 2:nrow(d)){
dif <- d$bottom[i]-d$top[i-1]
## here we are trying to test if box i can possibly collide with
## the box below it! Originally we checked if the bottom points of
## this box fall in the box below it, but this causes problems
## since we are reassigning box positions. If all boxes start at
## the same place, 2 will get moved up, 3 will not since its
## bottom points are no longer inside box 2. Solution: Look at box
## left and right limits and see if they collide!
## GOTCHA: If all the boxes are exactly the same size, on top of
## each other, then if we only examine left and right points of
## each box, none of the boxes will be detected as
## overlapping. One way to fix this is change > to >= in %between%
## but this is a bad idea since you can have boxes right next to
## each other that we don't want to move, that would be detected
## as overlapping. Solution: use the midpoint of the box as well!
overlap <- c(obox(d[i,],d[i-1,]),obox(d[i-1,],d[i,]))
if(dif < 0 && any(overlap)){
d$bottom[i] <- d$bottom[i]-dif
d$top[i] <- d$top[i]-dif
d$y[i] <- d$y[i]-dif
}
}
d
}
### Remove rows for which either x or y is NA
ignore.na <- function(d,...){
not.na <- is.finite(d$x)
if("y"%in% names(d)){
not.na <- not.na & is.finite(d$y)
}
d[not.na,]
}
qp.labels <- structure(function# Make a Positioning Method for non-overlapping lineplot labels
### Use a QP solver to find the best places to put the points on a
### line, subject to the constraint that they should not overlap.
(target.var,
### Variable name of the label target.
lower.var,
### Variable name of the lower limit of each label bounding box.
upper.var,
### Variable name of the upper limit of each label bounding box.
order.labels=function(d)order(d[,target.var]),
### Function that takes the data.frame of labels and returns an
### ordering, like from the order function. That ordering will be used
### to reorder the rows. This is useful to e.g. break ties when two
### groups have exactly the same value at the endpoint near the label.
limits=NULL
### Function that takes the data.frame of labels an returns a numeric
### vector of length 2. If finite, these values will be used to add
### constraints to the QP: limits[1] is the lower limit for the first
### label's lower.var, and limits[2] is the upper limit for the last
### labels's upper.var. Or NULL for no limits.
){
## Reality checks. These also have the side effect of forcing
## evaluation of all the arguments in the returned closure.
stopifnot(is.function(order.labels))
essential <- list(target.var,upper.var,lower.var)
for(v in essential){
stopifnot(is.character(v))
stopifnot(length(v)==1)
}
stopifnot(is.function(limits)||is.null(limits))
function(d,...){
## If there is only 1 label, there is no collision detection to
## do, so just return it.
if(nrow(d)==1)return(d)
##browser()
## Reality checks.
for(v in essential){
if(! v %in% names(d)){
stop("need to have calculated ",v)
}
}
## sorts data so that target_1 <= target_2 <= ... <= target_n.
d <- d[order.labels(d),]
## check limits to see if there is enough space, given specified
## cex.
if(is.function(limits)){
l <- limits(d)
stopifnot(is.numeric(l))
stopifnot(length(l)==2)
stopifnot(l[1]<l[2])
h.available <- l[2] - l[1]
h <- d[,upper.var]-d[,lower.var]
h.occupied <- sum(h)
if(h.occupied > h.available){ ## then the feasible set is empty.
## total hack:
cex <- h.available / h.occupied * 0.9
if("cex" %in% names(d)){
d$cex <- d$cex * cex
}else{
d$cex <- cex
}
d <- calc.boxes(d)
}
}
## These are the standard form matrices described in the
## directlabels poster.
target <- d[,target.var]
k <- nrow(d)
D <- diag(rep(1,k))
Ik <- diag(rep(1,k-1))
A <- rbind(0,Ik)-rbind(Ik,0)
y.up <- d[,upper.var]
y.lo <- d[,lower.var]
b0 <- (y.up-target)[-k] + (target-y.lo)[-1]
## limit constraints.
if(is.function(limits)){
if(is.finite(l[1])){
c.vec <- rep(0,k)
c.vec[1] <- 1
A <- cbind(A,c.vec)
b0 <- c(b0,l[1]+target[1]-y.lo[1])
}
if(is.finite(l[2])){
c.vec <- rep(0,k)
c.vec[k] <- -1
A <- cbind(A,c.vec)
b0 <- c(b0,y.up[k]-target[k]-l[2])
}
}
##print(A)
##print(b0)
##browser()
sol <- solve.QP(D,target,A,b0)
d[,target.var] <- sol$solution
d
}
### Positioning Method that adjusts target.var so there is no overlap
### of the label bounding boxes, as specified by upper.var and
### lower.var.
},ex=function(){
SegCost$error <- factor(SegCost$error,c("FP","FN","E","I"))
if(require(ggplot2)){
fp.fn.colors <- c(FP="skyblue",FN="#E41A1C",I="black",E="black")
fp.fn.sizes <- c(FP=2.5,FN=2.5,I=1,E=1)
fp.fn.linetypes <- c(FP="solid",FN="solid",I="dashed",E="solid")
err.df <- subset(SegCost,type!="Signal")
kplot <- ggplot(err.df,aes(segments,cost))+
geom_line(aes(colour=error,size=error,linetype=error))+
facet_grid(type~bases.per.probe)+
scale_linetype_manual(values=fp.fn.linetypes)+
scale_colour_manual(values=fp.fn.colors)+
scale_size_manual(values=fp.fn.sizes)+
scale_x_continuous(limits=c(0,20),breaks=c(1,7,20),minor_breaks=NULL)+
theme_bw()+theme(panel.margin=grid::unit(0,"lines"))
## The usual ggplot without direct labels.
print(kplot)
## Get rid of legend for direct labels.
no.leg <- kplot+guides(colour="none",linetype="none",size="none")
## Default direct labels.
direct.label(no.leg)
## Explore several options for tiebreaking and limits. First let's
## make a qp.labels Positioning Method that does not tiebreak.
no.tiebreak <- list("first.points",
"calc.boxes",
qp.labels("y","bottom","top"))
direct.label(no.leg, no.tiebreak)
## Look at the weird labels in the upper left panel. The E curve is
## above the FN curve, but the labels are the opposite! This is
## because they have the same y value on the first points, which are
## the targets for qp.labels. We need to tiebreak.
qp.break <- qp.labels("y","bottom","top",make.tiebreaker("x","y"))
tiebreak <- list("first.points",
"calc.boxes",
"qp.break")
direct.label(no.leg, tiebreak)
## Enlarge the text size and spacing.
tiebreak.big <- list("first.points",
cex=2,
"calc.boxes",
dl.trans(h=1.25*h),
"calc.borders",
"qp.break")
direct.label(no.leg, tiebreak.big)
## Even on my big monitor, the FP runs off the bottom of the screen
## in the top panels. To avoid that you can specify a limits
## function.
## Below, the ylimits function uses the limits of each panel, so
## labels appear inside the plot region. Also, if you resize your
## window so that it is small, you can see that the text size of the
## labels is decreased until they all fit in the plotting region.
qp.limited <- qp.labels("y","bottom","top",make.tiebreaker("x","y"),ylimits)
tiebreak.lim <- list("first.points",
cex=2,
"calc.boxes",
dl.trans(h=1.25*h),
"calc.borders",
"qp.limited")
direct.label(no.leg, tiebreak.lim)
}
})
### Make text bounding box larger by some amount.
enlarge.box <- function(d,...){
if(!"h"%in%names(d))stop("need to have already calculated height and width.")
calc.borders(within(d,{
w <- w+h
h <- h+h
}))
}
in1which <- function
### Calculate which points fall in a box.
(p,
### data frame of points with columns x and y and many rows.
box
### data frame of 1 row with columns left right top bottom.
){
p$x>=box$left & p$x<=box$right & p$y<=box$top & p$y>=box$bottom
}
### Calculate how many points fall in a box.
in1box <- function(p,box)sum(in1which(p,box))
### Make a Positioning Method that will, for every piece, select
### points and assign a vjust value.
label.pieces <- function(FUN,VJUST){
function(d,...){
processed <- gapply(d,function(d,...)d[FUN(d$y),],groups="piece")
transform(processed,hjust=0.5,vjust=VJUST)
}
}
inside <- function
### Calculate for each box how many points are inside.
(boxes,
### Data frame of box descriptions, each row is 1 box, need columns
### left right top bottom.
points
### Data frame of points, each row is 1 point, need columns x y.
){
sapply(1:nrow(boxes),function(i)in1box(points,boxes[i,]))
### Vector of point counts for each box.
}
dl.summarize <- function
### summarize which preserves important columns for direct labels.
(OLD,
### data frame
...
){
rownames(OLD) <- NULL
NEW <- unique(transform(OLD,...))
to.copy <- names(OLD)[!names(OLD)%in%names(NEW)]
for(N in to.copy)
NEW[,N] <- OLD[,N]
NEW
}
gapply <- function
### apply a Positioning Method to every group. works like ddply from
### plyr package, but the grouping column is always called groups, and
### the Positioning Method is not necessarily a function (but can be).
(d,
### data frame with column groups.
method,
### Positioning Method to apply to every group separately.
...,
### additional arguments, passed to Positioning Methods.
groups="groups"
### can also be useful for piece column.
){
stopifnot(is.data.frame(d))
dfs <- split(d,as.character(d[[groups]]))
f <- function(d,...){
res <- apply.method(method,d,columns.to.check=c("x","y"),...)
if(nrow(res)){
res[[groups]] <- d[[groups]][1]
res[["label"]] <- d[["label"]][1]
}
res
}
results <- lapply(dfs,f,...)
if(any(!sapply(results,is.data.frame))){
print(results)
stop("Positioning Method did not return data.frame")
}
do.call(rbind,results)
### data frame of results after applying FUN to each group in d.
}
### Label the points furthest from the middle for each group.
extreme.points <- function(d,...){
d$dist.from.center <- sqrt((d$x-midrange(d$x))^2+(d$y-midrange(d$y))^2)
gapply(d,function(d,...)d[which.max(d$dist.from.center),])
}
edges.to.outside <- function
### Given a list of edges from the convex or alpha hull, and a list of
### cluster centers, calculate a point near to each cluster on the
### outside of the hull.
(edges,centers,debug=FALSE,...){
if(debug){
with(centers,grid.points(
x,y,pch="+",default.units="cm",
name="directlabels.points.edges.to.outside"
))
with(edges,grid.segments(
x1,y1,x2,y2,default.units="cm",
name="directlabels.segments.edges.to.outside"
))
}
closepts <- gapply(centers,project.onto.segments,edges,debug=debug,...)
closepts$vjust <- ifelse(closepts$y-centers$y>0,0,1)
closepts$hjust <- ifelse(closepts$x-centers$x>0,0,1)
r <- apply.method("big.boxes",closepts)
r$x <- (r$right-r$left)/2+r$left
r$y <- (r$top-r$bottom)/2+r$bottom
r$hjust <- 0.5
r$vjust <- 0.5
r
}
### Calculate closest point on the alpha hull with size of the boxes,
### and put it outside that point.
outside.ahull <- function(d,...){
edges.to.outside(ahull.points(d),visualcenter(d),...)
}
### Calculate closest point on the convex hull and put it outside that
### point. Assume d is the center for each point cloud and then use
### orig.data to calculate hull.
outside.chull <- function(d,...){
edges.to.outside(chull.points(d),visualcenter(d),...)
}
project.onto.segments <- function
### Given a point and a set of line segments representing a convex or
### alpha hull, calculate the closest point on the segments.
(m,
### m is 1 row, a center of a point cloud, we need to find the
### distance to the closest point on each segment of the convex
### hull.
h,
### Data frame describing the line segments of the convex or alpha
### hull.
debug=FALSE,
...
### ignored
){
h$s <- (h$y2-h$y1)/(h$x2-h$x1)
## the closest point on the line formed by expanding this line
## segment (this expression is calculated by finding the minimum
## of the distance function).
h$xstar <- (m$x + m$y*h$s + h$x1*h$s^2 - h$s*h$y1)/(h$s^2+1)
h$minval <- apply(cbind(h$x1,h$x2),1,min)
h$maxval <- apply(cbind(h$x1,h$x2),1,max)
## xopt is the closest point on the line segment
h$xopt <- ifelse(h$xstar<h$minval,h$minval,
ifelse(h$xstar>h$maxval,h$maxval,h$xstar))
h$yopt <- h$s*(h$xopt-h$x1)+h$y1
## distance to each point on line segment from the center
h$d <- (m$x-h$xopt)^2+(m$y-h$yopt)^2
i <- which.min(h$d)
result <- with(h[i,],data.frame(x=xopt,y=yopt))
if(debug){
grid.segments(
m$x,m$y,result$x,result$y,default.units="cm",
name="directlabels.segments.project.onto.segments"
)
}
result
}
### Make a Positioning Function from a set of points on a vertical
### line that will be spaced out using qp.labels.
vertical.qp <- function(M){
avoid.collisions <-
qp.labels("y","bottom","top",make.tiebreaker("x","y"),ylimits)
list(M,"reduce.cex.lr",avoid.collisions)
}
### Make a tiebreaker function that can be used with qp.labels.
make.tiebreaker <- function(x.var,tiebreak.var){
force(x.var)
force(tiebreak.var)
function(d,...){
orig <- attr(d,"orig.data")
xvals <- unique(orig[,x.var])
x <- unique(d[,x.var])
if(length(x)>1){
stop("labels are not aligned")
}
xvals <- xvals[order(abs(xvals-x))]
group.dfs <- split(orig,orig$groups)
glist <- lapply(d$groups,function(g){
df <- group.dfs[[as.character(g)]]
group.x <- df[,x.var]
group.y <- df[,tiebreak.var]
all.unique <- length(unique(group.x)) == length(group.x)
## approx gives the following error if we only have one point:
## need at least two non-NA values to interpolate
x.not.missing <- !is.na(group.x)
y.not.missing <- !is.na(group.y)
not.missing <- sum(x.not.missing & y.not.missing)
if(all.unique && 1 < not.missing){
## rule=2 means to use the most extreme value instead of the
## default NA, for any points that are outside the range -
## this is required to get a good ordering in some cases.
approx(group.x, group.y, xvals, rule=2)$y
}else{
iord <- order(abs(group.x-x))
closest <- iord[1]
rep(group.y[closest], length(xvals))
}
})
m <- do.call(cbind,glist)
## useful for debugging:
##print(m)
L <- lapply(1:nrow(m),function(i)m[i,])
do.call(order,L)
}
}
### Calculate the default alpha parameter for ashape based on the
### average size of label boxes.
default.ahull <- function(d,...){
labels <- apply.method("big.boxes",d,...)
mean(unlist(labels[,c("w","h")]))
}
### Calculate the points on the ashape.
ahull.points <- function(d,...,ahull=default.ahull(d)){
xy <- unique(d[,c("x","y")])
as <- alphahull::ashape(xy,alpha = ahull)
as.data.frame(as$edges)
}
### Calculate the points on the convex hull.
chull.points <- function(d,...){
bpts <- d[with(d,chull(x,y)),]
r <- data.frame(i1=1:nrow(bpts),i2=c(2:nrow(bpts),1))
r$x1 <- bpts$x[r$i1]
r$y1 <- bpts$y[r$i1]
r$x2 <- bpts$x[r$i2]
r$y2 <- bpts$y[r$i2]
r
}
check.for.columns <- function
### Stop if a data.frame does not have some columns.
(d,