structure diagram where each members of group are connected to center and all cluster grand center in r

前端 未结 1 1445
时光取名叫无心
时光取名叫无心 2021-02-01 09:12

I am trying to create a structure diagram from the data like the following:

mydf <- data.frame ( group = rep (1:5, each = 20), z = rnorm (20, 10, 1),
                 


        
相关标签:
1条回答
  • 2021-02-01 09:44

    Here is an example:

    library(plyr)
    ms <- ddply(mydf, .(group), colwise(mean))
    mydf2ms <- merge(mydf, ms, by = "group")
    gm <- ddply(mydf, NULL, colwise(mean))
    ms2gm <- data.frame(ms, gm)
    
    ci <- expand.grid(1:3*2, seq(0, 2*pi, length = 180))
    ci <- transform(ci, x = cos(Var2) * Var1 + gm$x, y = sin(Var2) * Var1 + gm$y)
    
    library(ggplot2)
    ggplot(mydf, aes(x, y)) +
      geom_point(aes(colour= factor (group), size=z)) +
      geom_segment(data = mydf2ms, mapping = aes(x = x.x, y = y.x, xend = x.y, yend = y.y, colour = factor(group))) +
      geom_segment(data = ms2gm, mapping = aes(x = x, y = y, xend = x.1, yend = y.1)) +
      geom_point(data = ms, colour = "black", size = 10, shape = 4) +
      geom_point(data = gm, colour = "red", size = 10, shape = 4) +
      geom_path(data = ci, mapping = aes(group = Var1), colour = "pink")
    

    enter image description here

    0 讨论(0)
提交回复
热议问题