Created
July 28, 2018 17:18
-
-
Save bbolker/c2a8cff3c040f59195d3d5df5182cb5d to your computer and use it in GitHub Desktop.
a ggplot geom that connects points to the centroid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##' | |
##' @examples | |
##' dd <- data.frame(x=rnorm(10),y=rnorm(10)) | |
##' ggplot(dd,aes(x,y))+ | |
##' geom_point() + | |
##' stat_centseg() | |
##' ggplot(dd,aes(x,y))+ | |
##' geom_point() + | |
##' stat_centseg(cfun=median) | |
##' data("Gasoline",package="nlme") | |
##' ggplot(Gasoline,aes(yield,endpoint,colour=Sample))+ | |
##' geom_point()+ | |
##' ggalt::geom_encircle() | |
##' stat_centseg() | |
library(ggplot2) | |
StatCentSeg <- ggplot2::ggproto("StatCentSeg", Stat, | |
compute_group = function(data, scales, params, | |
cfun=median) { | |
data$xend <- cfun(data$x) | |
data$yend <- cfun(data$y) | |
return(data) | |
}, | |
required_aes = c("x", "y") | |
) | |
stat_centseg <- function(mapping = NULL, data = NULL, geom = "segment", | |
position = "identity", na.rm = FALSE, show.legend = NA, | |
inherit.aes = TRUE, cfun=median, ...) { | |
layer( | |
stat = StatCentSeg, data = data, mapping = mapping, geom = geom, | |
position = position, show.legend = show.legend, inherit.aes = inherit.aes, | |
params = list(na.rm = na.rm, cfun = cfun, ...) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment