当前位置:网站首页>R 椭圆随机点产生并画图
R 椭圆随机点产生并画图
2022-06-24 08:16:00 【qq_45759229】
产生数据集
首先我想到的怎么在R中产生数据集,我知道python中有make_blob函数,那么R中是否有对应的函数呢,虽然我可以在python产生数据再导入到R会很麻烦,因为我不是产生一次数据就完了,我得模拟很多次,最终找到一个比价满意的,
https://github.com/elbamos/clusteringdatasets
这个R包里面提供了和sklearn里面的make_blob一样的函数
安装方式如下
首先下载github的源文件
install.packages("/Volumes/MACPAN/Chrome下载/clusteringdatasets-master/",type="source",repos=NULL)
library(clusteringdatasets)
安装后就可以使用了
使用案例如下
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) # 设置cluster中心
##
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
# 因为我的结果最终就是非常圆润的,所以我想把一些点给删掉
# 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)
结果如下
但是我画出这个后,为了更好看一点,我想进一步画椭圆的图,这个怎么实现呢
https://stackoverflow.com/questions/41820683/how-to-plot-ellipse-given-a-general-equation-in-r
这个问题里有提到怎么画椭圆
其实这个椭圆是我想要的,但是这个只是画图,不提供表达式,因为我本质上要再要在一个椭圆内产生随机分布的点
pass
然后我又找到了一个类似的问题
https://stackoverflow.com/questions/64847597/how-do-i-generate-data-where-points-are-repelled-if-they-land-within-a-certain
这个里面阐述了怎么在一个圆内产生随机分布的点,但我需要的是椭圆,倒是可以借鉴,
然后我还找到一个更有意思的画图方案,直接画椭圆的置信区间
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))
这个其实也是可以拿来参考的,不过我后面发现了一个完美的解决方式
就是使用runifdisc函数
我实现的代码如下
################################# 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)) #创建一个3列的空对象
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)) #创建一个3列的空对象
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)) #创建一个3列的空对象
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)
最终的结果如下
左边那个椭圆的角度我怎么调都调不好,感觉旋转的角度不对,不过看起来是还是蛮漂亮的,达到了我的需求,嘿嘿
边栏推荐
- 深入解析 Apache BookKeeper 系列:第三篇——读取原理
- [ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)
- P6117-[joi 2019 final] greedy
- 解决:jmeter5.5在win11下界面上的字特别小
- Solution: the word of jmeter5.5 on the win11 lower interface is very small
- 每周推荐短视频:谈论“元宇宙”要有严肃认真的态度
- 关于thinkphp5 使用模型save()更新数据提示 method not exist:think\db\Query-&gt; 报错解决方案
- leetcode--字符串
- Depens:*** but it is not going to be installed
- 实战剖析:app扫码登陆实现原理(app+网页端详细逻辑)附源码
猜你喜欢

解决:jmeter5.5在win11下界面上的字特别小

NETRCA: AN EFFECTIVE NETWORK FAULT CAUSE LOCALIZATION之论文阅读

读CVPR 2022目标检测论文得到的亿点点启发

Alibaba Senior Software Testing Engineer recommends testers to learn -- Introduction to security testing

Epidemic situation, unemployment, 2022, we shouted to lie down!

Yolox backbone -- implementation of cspparknet

12、 Demonstration of all function realization effects

Ebanb B1 Bracelet brush firmware abnormal interrupt handling

e的lnx为什么等于x

实战剖析:app扫码登陆实现原理(app+网页端详细逻辑)附源码
随机推荐
【LeetCode】415. String addition
[redis realize Secondary killing Business ①] Overview of Secondary killing Process | Basic Business Realization
PhpStrom代码格式化设置
NETRCA: AN EFFECTIVE NETWORK FAULT CAUSE LOCALIZATION之论文阅读
软件系统依赖关系分析
Groovy通过withCredentials获取Jenkins凭据
Linux (centos7.9) installation and deployment of MySQL Cluster 7.6
RISC-V架构下 FPU Context 的动态保存和恢复
Cdga | how can we do well in data governance?
The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year
P6117-[JOI 2019 Final]コイン集め【贪心】
Zero foundation self-study SQL course | sub query
Applet cloud data, data request a method to collect data
Vidéo courte recommandée chaque semaine: Soyez sérieux en parlant de "métaunivers"
The ambition of JD instant retailing from 618
荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?
[e325: attention] VIM editing error
Threejs glow channel 01 (unrealbroompass & layers)
Recommendation - Secret of curiosity: how many dancing angels can stand on the tip of a needle?
Squid代理服务器应用