comparison stat_bag.r @ 2:c29354d16967 draft

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