当前位置:网站首页>R ellipse random point generation and drawing

R ellipse random point generation and drawing

2022-06-24 09:32:00 qq_ forty-five million seven hundred and fifty-nine thousand tw

Generating data sets

The first thing I thought about was R Generate dataset in , That's true. python There is make_blob function , that R Is there a corresponding function in , Although I can be in python Generate data and then import it to R It will be very trouble , Because I don't just generate data once , I have to simulate many times , Finally find a satisfactory price comparison ,
https://github.com/elbamos/clusteringdatasets
This R And... Are provided in the package sklearn Inside make_blob Same function
The installation method is as follows
First download github The source file

install.packages("/Volumes/MACPAN/Chrome download /clusteringdatasets-master/",type="source",repos=NULL)
library(clusteringdatasets)

After installation, it can be used
Use cases are as follows

rm(list=ls())
library(ggplot2)
library(clusteringdatasets)
set.seed(1)
n_sample=20000
center_metrix=matrix(c(0,3,-2,0,2,0),nrow=3,byrow = T) #  Set up cluster center 
##
blobs <- make_blobs(n_samples = n_sample,centers=center_metrix,cluster_std = 0.2)
xx=sample(c(1,2,3),n_sample,replace=T)
blobs$labels=xx

#  Because my final result is very smooth , So I want to delete some points 
# radis=0.3
# r1=center_metrix[1,]
# r2=center_metrix[2,]
# r3=center_metrix[3,]

df=as.data.frame(blobs)
colnames(df)=c("x","y","cluster")
df$cluster=as.factor(df$cluster)

#plot(blobs$samples, col=rainbow(3)[blobs$labels])
p=ggplot(data=df,aes(x=x,y=y,colour=cluster))+geom_point()
print(p)

give the result as follows
 Insert picture description here
But when I draw this , For a better look , I want to draw the ellipse further , How to achieve this
https://stackoverflow.com/questions/41820683/how-to-plot-ellipse-given-a-general-equation-in-r
This question mentioned how to draw an ellipse
 Insert picture description here Actually, this ellipse is what I want , But this is just a drawing , No expression is provided , Because I essentially want to generate random points in an ellipse
pass

Then I found a similar problem
https://stackoverflow.com/questions/64847597/how-do-i-generate-data-where-points-are-repelled-if-they-land-within-a-certain
This explains how to generate randomly distributed points in a circle , But what I need is an ellipse , It can be used for reference ,

Then I found a more interesting drawing scheme , Draw the confidence interval of the ellipse directly
https://blog.csdn.net/maryyu8873/article/details/78491869

library(car)
dataEllipse(Duncan$income, Duncan$education, levels=0.95,xlim=c(-30,120),ylim=c(-30,160))

 Insert picture description here This can also be used for reference , But I found a perfect solution later
Is the use of runifdisc function
The code I implemented is as follows

################################# plot cluster1 #########################
############################################################################
############################################################################
############################################################################
n_sample=2000
center=c(0,3)
X <- runifdisc(n_sample)
Y <- affine(X, mat=diag(c(1.5,0.8)))
Z = Y
#Z = rotate(Y,45)
df = as.data.frame(matrix(nrow=n_sample,ncol=3)) # Create a 3 Empty object for column 
colnames(df)=c("x","y","cluster")
df$x=Z$x
df$y=Z$y
df$x=df$x+center[1]
df$y=df$y+center[2]
df$cluster=sample(c("Batch1","Batch2","Batch3"),n_sample,replace=T)
df$cluster=as.factor(df$cluster)
df1=df

########################### plot cluster 2 ################################
############################################################################
############################################################################
############################################################################

center=c(-4,0)
X <- runifdisc(n_sample)
Y <- affine(X, mat=diag(c(1,1.5)))
#Z=Y
Z = rotate(Y,90)
#Z = rotate(Z,45)

df = as.data.frame(matrix(nrow=n_sample,ncol=3)) # Create a 3 Empty object for column 
colnames(df)=c("x","y","cluster")
df$x=Z$x
df$y=Z$y
df$x=df$x+center[1]
df$y=df$y+center[2]
df$cluster=sample(c("Batch1","Batch2","Batch3"),n_sample,replace=T)
df$cluster=as.factor(df$cluster)
df2=df

########################### plot cluster 3 ################################
############################################################################
############################################################################
############################################################################

center=c(4,0)
X <- runifdisc(n_sample)
Y <- affine(X, mat=diag(c(2,1)))
Z = rotate(Y,135)
df = as.data.frame(matrix(nrow=n_sample,ncol=3)) # Create a 3 Empty object for column 
colnames(df)=c("x","y","cluster")
df$x=Z$x
df$y=Z$y
df$x=df$x+center[1]
df$y=df$y+center[2]
df$cluster=sample(c("Batch1","Batch2","Batch3"),n_sample,replace=T)
df$cluster=as.factor(df$cluster)
df3=df


df=rbind(df1,df2,df3)
colnames(df)=c("x","y","BATCH")

p=ggplot(data=df,aes(x=x,y=y,colour=BATCH))+geom_point(size=1)+
  theme(aspect.ratio=1)+theme_void()+
  guides(colour = guide_legend(override.aes = list(size = 20,alpha=1,shape=16)))+
  theme(legend.position = c(0.8, 0.7),legend.title=element_text(size=20))+
  theme(legend.text=element_text(size=20))+
  scale_color_manual(values=RColorBrewer::brewer.pal(3,"Set1"))
print(p)

ggsave("./simulation_plot.png",plot=p)

The final result is as follows
 Insert picture description here I can't adjust the angle of the ellipse on the left properly , I feel the rotation angle is wrong , But it looks pretty , It meets my needs , Hey

原网站

版权声明
本文为[qq_ forty-five million seven hundred and fifty-nine thousand tw]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240816224648.html