简体   繁体   English

绘制给定百分比的圆/凸包

[英]plot a circle/convex hull arround a given percentage of points

I have 我有

x=rnorm(100)
y=rnorm(100)
plot(x,y)
abline(h=0); abline(v=0)

From point (0,0) and going outwards I would like to draw a contour/circle/ellipse/freehand convex hull that encloses any given percentage of points. 从点(0,0)到向外,我想绘制一个轮廓/圆形/椭圆/徒手的凸包,它包围点的任何给定百分比。

Is there any function or package that can automate this? 有没有可以自动执行此功能的功能或程序包? I have tried the following so far but I can only get a circle with some extrapolation and approximation. 到目前为止,我已经尝试了以下方法,但是只能得到带有一些外推和近似值的圆。

I have tried this so far: 到目前为止,我已经尝试过了:

#calculate radius
r<- sqrt(x^2+y^2)

df<-data.frame(radius=seq(0,3,0.1), percentage=NA)

#get the percentage of points that have a smaller radius than i
k<-1
for (i in seq(0,3,0.1)){
  df$percentage[k] <- sum(r<i)/length(r)
  k<-k+1
}

#extrapolation function
prox.function<- approxfun(df$percentage, df$radius)


#get the radius of the circle that encloses about 50% of
prox.function(.50)

#draw the circle
library(plotrix)
draw.circle(0,0,prox.function(.50))

在此处输入图片说明

The radius enclosing a fraction f of the points is: 包围点的分数f的半径是:

f <- 0.5 # use half for this example as in the question
sort(r)[ ceiling(f * length(r)) ]

Yes, we can create a new geom for ggplot that will draw a convex hull around any given percentage of all the points in the data. 是的,我们可以为ggplot创建一个新的几何图形,它将在数据中所有点的任何给定百分比周围绘制凸包。 This is similar to the bagplot , and uses some code from the bagplot function in the aplpack package (which is fixed at 50% of points). 这类似于bagplot ,并使用aplpack软件包中bagplot函数中的一些代码(固定为50%的点)。

Here is the definition of the new geom that allows you to chose what percentage of points to enclose: 这是新几何的定义,可让您选择要封闭的点数百分比:

library(ggplot2)

# Here's the stat_
StatBag <- ggproto("Statbag", Stat,
                   compute_group = function(data, scales, prop = 0.5) {

                     #################################
                     #################################
                     # originally from aplpack package, plotting functions removed
                     plothulls_ <- function(x, y, fraction, n.hull = 1,
                                            col.hull, lty.hull, lwd.hull, density=0, ...){
                       # function for data peeling:
                       # x,y : data
                       # fraction.in.inner.hull : max percentage of points within the hull to be drawn
                       # n.hull : number of hulls to be plotted (if there is no fractiion argument)
                       # col.hull, lty.hull, lwd.hull : style of hull line
                       # plotting bits have been removed, BM 160321
                       # pw 130524
                       if(ncol(x) == 2){ y <- x[,2]; x <- x[,1] }
                       n <- length(x)
                       if(!missing(fraction)) { # find special hull
                         n.hull <- 1
                         if(missing(col.hull)) col.hull <- 1
                         if(missing(lty.hull)) lty.hull <- 1
                         if(missing(lwd.hull)) lwd.hull <- 1
                         x.old <- x; y.old <- y
                         idx <- chull(x,y); x.hull <- x[idx]; y.hull <- y[idx]
                         for( i in 1:(length(x)/3)){
                           x <- x[-idx]; y <- y[-idx]
                           if( (length(x)/n) < fraction ){
                             return(cbind(x.hull,y.hull))
                           }
                           idx <- chull(x,y); x.hull <- x[idx]; y.hull <- y[idx];
                         }
                       }
                       if(missing(col.hull)) col.hull <- 1:n.hull
                       if(length(col.hull)) col.hull <- rep(col.hull,n.hull)
                       if(missing(lty.hull)) lty.hull <- 1:n.hull
                       if(length(lty.hull)) lty.hull <- rep(lty.hull,n.hull)
                       if(missing(lwd.hull)) lwd.hull <- 1
                       if(length(lwd.hull)) lwd.hull <- rep(lwd.hull,n.hull)
                       result <- NULL
                       for( i in 1:n.hull){
                         idx <- chull(x,y); x.hull <- x[idx]; y.hull <- y[idx]
                         result <- c(result, list( cbind(x.hull,y.hull) ))
                         x <- x[-idx]; y <- y[-idx]
                         if(0 == length(x)) return(result)
                       }
                       result
                     } # end of definition of plothulls
                     #################################


                     # prepare data to go into function below
                     the_matrix <- matrix(data = c(data$x, data$y), ncol = 2)

                     # get data out of function as df with names
                     setNames(data.frame(plothulls_(the_matrix, fraction = prop)), nm = c("x", "y"))
                     # how can we get the hull and loop vertices passed on also?
                   },

                   required_aes = c("x", "y")
)

# Here's the stat_ function
#' @inheritParams ggplot2::stat_identity
#' @param prop Proportion of all the points to be included in the bag (default is 0.5)
stat_bag <- function(mapping = NULL, data = NULL, geom = "polygon",
                     position = "identity", na.rm = FALSE, show.legend = NA, 
                     inherit.aes = TRUE, prop = 0.5, alpha = 0.3, ...) {
  layer(
    stat = StatBag, data = data, mapping = mapping, geom = geom, 
    position = position, show.legend = show.legend, inherit.aes = inherit.aes,
    params = list(na.rm = na.rm, prop = prop, alpha = alpha, ...)
  )
}

# here's the geom_
geom_bag <- function(mapping = NULL, data = NULL,
                     stat = "identity", position = "identity",
                     prop = 0.5, 
                     alpha = 0.3,
                     ...,
                     na.rm = FALSE,
                     show.legend = NA,
                     inherit.aes = TRUE) {
  layer(
    data = data,
    mapping = mapping,
    stat = StatBag,
    geom = GeomBag,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      na.rm = na.rm,
      alpha = alpha,
      prop = prop,
      ...
    )
  )
}

#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @export
GeomBag <- ggproto("GeomBag", Geom,
                   draw_group = function(data, panel_scales, coord) {
                     n <- nrow(data)
                     if (n == 1) return(zeroGrob())

                     munched <- coord_munch(coord, data, panel_scales)
                     # Sort by group to make sure that colors, fill, etc. come in same order
                     munched <- munched[order(munched$group), ]

                     # For gpar(), there is one entry per polygon (not one entry per point).
                     # We'll pull the first value from each group, and assume all these values
                     # are the same within each group.
                     first_idx <- !duplicated(munched$group)
                     first_rows <- munched[first_idx, ]

                     ggplot2:::ggname("geom_bag",
                                      grid:::polygonGrob(munched$x, munched$y, default.units = "native",
                                                         id = munched$group,
                                                         gp = grid::gpar(
                                                           col = first_rows$colour,
                                                           fill = alpha(first_rows$fill, first_rows$alpha),
                                                           lwd = first_rows$size * .pt,
                                                           lty = first_rows$linetype
                                                         )
                                      )
                     )


                   },

                   default_aes = aes(colour = "NA", fill = "grey20", size = 0.5, linetype = 1,
                                     alpha = NA, prop = 0.5),

                   handle_na = function(data, params) {
                     data
                   },

                   required_aes = c("x", "y"),

                   draw_key = draw_key_polygon
)

Here are some examples. 这里有些例子。 We can stack three convex hulls together with different alpha levels to show where the centre of the data is, and its spread: 我们可以将三个凸包与不同的alpha级别堆叠在一起,以显示数据的中心位置及其散布:

ggplot(mpg, aes(displ, hwy, fill = drv, colour = drv)) + 
  geom_point() + 
  geom_bag(prop = 0.95) + # enclose 95% of points
  geom_bag(prop = 0.5,  alpha = 0.5) + # enclose 50% of points
  geom_bag(prop = 0.1, alpha = 0.8) # enclose 5% of points

在此处输入图片说明

ggplot(iris, aes(Sepal.Length,  Petal.Length, colour = Species, fill = Species)) + 
  geom_point() + 
  stat_bag(prop = 0.95) +  # enclose 95% of points
  stat_bag(prop = 0.5, alpha = 0.5) + # enclose 50% of points
  stat_bag(prop = 0.05, alpha = 0.9) # enclose 5% of points

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM