美文网首页
ggplot2-geom_convexhull()函数绘制点集凸

ggplot2-geom_convexhull()函数绘制点集凸

作者: 倪桦 | 来源:发表于2023-01-08 11:45 被阅读0次

    1.Prepare data

    library(ggplot2);library(dplyr)
    dat <- rbind(data.frame(x = rnorm(50,1,sd = .8),y = rnorm(50,1,sd = .5),groups = "a"),
                 data.frame(x = rnorm(60,3,sd = .5),y = rnorm(60,5,sd = .3),groups = "b"),
                 data.frame(x = rnorm(90,4,sd = 1.),y = rnorm(90,1,sd = .6),groups = "c"))
    -------------------------------------------------------
                  x           y group
    1   -0.91868184  0.67446740     a
    2    0.28066967  0.97873870     a
    52   3.22960282  4.75726027     b
    53   2.49711939  5.78070916     b
    54   2.99912717  5.33394315     b
    141  3.64351920  1.56362945     c
    142  4.67163828  0.86267089     c
    ...
    

    2.Draw HULL

    ggplot(dat , aes(x = x,y = y)) + geom_point(aes(color = groups),show.legend = F) + 
        geom_label(data = dat %>% group_by(groups) %>% summarise(x= mean(x),y = mean(y)),
                   aes(x = x,y = y,label = groups,fill = groups),
                   fontface = "bold",colour = "white",size = 8,show.legend = F) +
        coord_equal(xlim = c(-1,6),ylim = c(-1,6)) +
        ggConvexHull::geom_convexhull(aes(color = groups),fill = NA,show.legend = F) +
        theme_bw() + theme(panel.grid = element_blank())
    -------------------------------------------------------
    

    相关文章

      网友评论

          本文标题:ggplot2-geom_convexhull()函数绘制点集凸

          本文链接:https://www.haomeiwen.com/subject/qgqzqdtx.html