annotate stat_bag.r @ 6:a3260d4f7868 draft

Uploaded
author mnhn65mo
date Mon, 13 Aug 2018 09:38:57 -0400
parents c29354d16967
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
1 library(ggplot2)
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
2 StatBag <- ggproto("Statbag", Stat,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
3 compute_group = function(data, scales, prop = 0.5) {
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
4
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
5 #################################
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
6 #################################
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
7 # originally from aplpack package, plotting functions removed
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
8 plothulls_ <- function(x, y, fraction, n.hull = 1,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
9 col.hull, lty.hull, lwd.hull, density=0, ...){
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
10 # function for data peeling:
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
11 # x,y : data
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
12 # fraction.in.inner.hull : max percentage of points within the hull to be drawn
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
13 # n.hull : number of hulls to be plotted (if there is no fractiion argument)
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
14 # col.hull, lty.hull, lwd.hull : style of hull line
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
15 # plotting bits have been removed, BM 160321
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
16 # pw 130524
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
17 if(ncol(x) == 2){ y <- x[,2]; x <- x[,1] }
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
18 n <- length(x)
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
19 if(!missing(fraction)) { # find special hull
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
20 n.hull <- 1
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
21 if(missing(col.hull)) col.hull <- 1
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
22 if(missing(lty.hull)) lty.hull <- 1
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
23 if(missing(lwd.hull)) lwd.hull <- 1
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
24 x.old <- x; y.old <- y
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
25 idx <- chull(x,y); x.hull <- x[idx]; y.hull <- y[idx]
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
26 for( i in 1:(length(x)/3)){
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
27 x <- x[-idx]; y <- y[-idx]
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
28 if( (length(x)/n) < fraction ){
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
29 return(cbind(x.hull,y.hull))
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
30 }
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
31 idx <- chull(x,y); x.hull <- x[idx]; y.hull <- y[idx];
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
32 }
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
33 }
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
34 if(missing(col.hull)) col.hull <- 1:n.hull
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
35 if(length(col.hull)) col.hull <- rep(col.hull,n.hull)
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
36 if(missing(lty.hull)) lty.hull <- 1:n.hull
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
37 if(length(lty.hull)) lty.hull <- rep(lty.hull,n.hull)
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
38 if(missing(lwd.hull)) lwd.hull <- 1
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
39 if(length(lwd.hull)) lwd.hull <- rep(lwd.hull,n.hull)
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
40 result <- NULL
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
41 for( i in 1:n.hull){
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
42 idx <- chull(x,y); x.hull <- x[idx]; y.hull <- y[idx]
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
43 result <- c(result, list( cbind(x.hull,y.hull) ))
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
44 x <- x[-idx]; y <- y[-idx]
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
45 if(0 == length(x)) return(result)
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
46 }
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
47 result
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
48 } # end of definition of plothulls
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
49 #################################
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
50
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
51
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
52 # prepare data to go into function below
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
53 the_matrix <- matrix(data = c(data$x, data$y), ncol = 2)
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
54
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
55 # get data out of function as df with names
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
56 setNames(data.frame(plothulls_(the_matrix, fraction = prop)), nm = c("x", "y"))
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
57 # how can we get the hull and loop vertices passed on also?
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
58 },
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
59
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
60 required_aes = c("x", "y")
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
61 )
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
62
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
63 #' @inheritParams ggplot2::stat_identity
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
64 #' @param prop Proportion of all the points to be included in the bag (default is 0.5)
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
65 stat_bag <- function(mapping = NULL, data = NULL, geom = "polygon",
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
66 position = "identity", na.rm = FALSE, show.legend = NA,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
67 inherit.aes = TRUE, prop = 0.5, alpha = 0.3, ...) {
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
68 layer(
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
69 stat = StatBag, data = data, mapping = mapping, geom = geom,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
70 position = position, show.legend = show.legend, inherit.aes = inherit.aes,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
71 params = list(na.rm = na.rm, prop = prop, alpha = alpha, ...)
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
72 )
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
73 }
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
74
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
75
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
76 geom_bag <- function(mapping = NULL, data = NULL,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
77 stat = "identity", position = "identity",
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
78 prop = 0.5,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
79 alpha = 0.3,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
80 ...,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
81 na.rm = FALSE,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
82 show.legend = NA,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
83 inherit.aes = TRUE) {
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
84 layer(
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
85 data = data,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
86 mapping = mapping,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
87 stat = StatBag,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
88 geom = GeomBag,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
89 position = position,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
90 show.legend = show.legend,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
91 inherit.aes = inherit.aes,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
92 params = list(
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
93 na.rm = na.rm,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
94 alpha = alpha,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
95 prop = prop,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
96 ...
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
97 )
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
98 )
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
99 }
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
100
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
101 #' @rdname ggplot2-ggproto
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
102 #' @format NULL
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
103 #' @usage NULL
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
104 #' @export
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
105 GeomBag <- ggproto("GeomBag", Geom,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
106 draw_group = function(data, panel_scales, coord) {
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
107 n <- nrow(data)
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
108 if (n == 1) return(zeroGrob())
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
109
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
110 munched <- coord_munch(coord, data, panel_scales)
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
111 # Sort by group to make sure that colors, fill, etc. come in same order
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
112 munched <- munched[order(munched$group), ]
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
113
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
114 # For gpar(), there is one entry per polygon (not one entry per point).
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
115 # We'll pull the first value from each group, and assume all these values
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
116 # are the same within each group.
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
117 first_idx <- !duplicated(munched$group)
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
118 first_rows <- munched[first_idx, ]
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
119
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
120 ggplot2:::ggname("geom_bag",
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
121 grid:::polygonGrob(munched$x, munched$y, default.units = "native",
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
122 id = munched$group,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
123 gp = grid::gpar(
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
124 col = first_rows$colour,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
125 fill = alpha(first_rows$fill, first_rows$alpha),
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
126 lwd = first_rows$size * .pt,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
127 lty = first_rows$linetype
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
128 )
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
129 )
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
130 )
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
131
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
132
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
133 },
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
134
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
135 default_aes = aes(colour = "NA", fill = "grey20", size = 0.5, linetype = 1,
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
136 alpha = NA, prop = 0.5),
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
137
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
138 handle_na = function(data, params) {
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
139 data
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
140 },
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
141
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
142 required_aes = c("x", "y"),
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
143
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
144 draw_key = draw_key_polygon
c29354d16967 Uploaded
mnhn65mo
parents:
diff changeset
145 )