结构图,其中每个成员组连接到中心,并在r中的所有集群大中心 [英] structure diagram where each members of group are connected to center and all cluster grand center in r

查看:150
本文介绍了结构图,其中每个成员组连接到中心,并在r中的所有集群大中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  mydf<  -  data.frame( (20,2,0.5),rnorm(20,2,0.5),$ = (20,9,0.5),rnorm(20,9,0.5),rnorm(20,5,0.5)),
y = c(rnorm(20,2,0.5),rnorm(20 ,9,0.5),rnorm(20,2,0.5),
rnorm(20,9,0.5),rnorm(20,2,0.5)))

表示<聚合(。〜group,data = mydf,mean)
gmx< -mean(mydf $ x)
gmy< - mean(mydf $ y)

library ggplot2)
ggplot(mydf,aes(x,y))+
geom_point(aes(color = factor(group),size = z))+ theme_bw()

我想将每个群集中的每个点连接到其中心,然后将群集中心连接到grad。这将产生一个如下图(只是粗略的草图,其中两个群集连接到中心,实际上所有群集都具有相同的):.........



(如果可能的话,我想使用与集群颜色相同的线段)

解决方案

这里是一个例子:

  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(color = factor(group),size = z))+
geom_segment(data = mydf2ms,mapping = aes(x = xx,y = yx,xend = x 。 y,yend = yy,color = factor(group)))+
geom_segment(data = ms2gm,mapping = aes(x = x,y = y,xend = x.1,yend = y.1)) +
geom_point(data = ms,color =black,size = 10,shape = 4)+
geom_point(data = gm,color =red,size = 10,shape = 4) +
geom_path(data = ci,mapping = aes(group = Var1),color =pink)


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),
                  x = c(rnorm (20, 2, 0.5), rnorm (20, 2, 0.5),
          rnorm (20, 9, 0.5), rnorm (20, 9, 0.5),rnorm (20, 5, 0.5)),
       y = c(rnorm (20, 2, 0.5), rnorm (20, 9, 0.5), rnorm (20, 2, 0.5),
      rnorm (20, 9, 0.5), rnorm (20, 2, 0.5)))

means <- aggregate(. ~ group, data = mydf, mean)
gmx <-mean (mydf$x)
gmy <- mean (mydf$y)

library(ggplot2)
ggplot(mydf, aes(x, y)) +
geom_point(aes(colour= factor (group), size=z)) + theme_bw()

I want make connect every points within each cluster to its center and then the cluster center to grad mean. This will be produce a plot like the following (just rough sketch where two cluster are connected to the center, in real all cluster have the same):.........

(I would like to use the line segments of same color as of cluster if possible)

解决方案

Here is an exmaple:

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")

这篇关于结构图,其中每个成员组连接到中心,并在r中的所有集群大中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆