简体   繁体   English

如何在 R 中生成点?

[英]How to generate points in R?

I need generate points in R.我需要在 R 中生成点。 I want to get plot like in this example:我想得到 plot 就像在这个例子中:

想要的情节

My main task: Generate a 2D dataset consisting of 3 clusters, each strongly stretched along one of the axes.我的主要任务:生成一个由 3 个集群组成的 2D 数据集,每个集群都沿着其中一个轴强烈拉伸。

I also have code example:我也有代码示例:

x <- rbind(matrix(rnorm(100, mean=0.2, sd=0.1), ncol=2), matrix(rnorm(100, mean=0.2, sd=0.1), ncol=2))
plot(x)

I guess I should change the params but I don't know how.我想我应该更改参数,但我不知道如何。

I tried with this kind of approach, have look at it, if it is helpful.我尝试过这种方法,看看它,如果它有帮助。

# Creating 3 uniform data distributions 
a = matrix(runif(100, 0, 5), ncol=2)   
b = matrix(runif(100, 10, 15), ncol=2) 
c = matrix(runif(100, 10, 15), ncol=2) 

b[,1] = b[,1] - 10 # removing 10 units from x aixs
c[,2] = c[,2] - 10 # removing 10 units from y aixs

x <- rbind(a,b,c)  # binding rowise
plot(x)            # plot

Output: Output: 方法 1 的输出

# Creating 3 uniform data distributions 
a = matrix(runif(100, 2, 4), ncol=2)   
b = matrix(runif(100, 1, 5), ncol=2) 
c = matrix(runif(100, 1, 5), ncol=2) 

b[,1] = b[,1] /5 # scaling x axis by 5 units
c[,2] = c[,2] /5 # scaling y axis by 5 units

x <- rbind(a,b,c)  # binding rowise
plot(x)            # plot

Output: Output: 方法 2 的输出

Another approach另一种方法

# Creating normal distribution over X axis and uniform distribution over Y axis
a = cbind(rnorm(100, mean=3, sd=1),
        runif(100, 0, 1))

# Creating uniform distribution over X axis and normal distribution over Y axis
b = cbind(runif(100, 0, 1),
        rnorm(100, mean=3, sd=1))

# Creating normal distribution over both axis ranging from 2-4
c = matrix(runif(100, 2, 4), ncol=2)   

# binding row wise 300 samples 100 from each cluster and plotting
plot(rbind(a, b, c))

Output: Output: 方法 3 的输出

Hope it helps!希望能帮助到你!

Your code is working: Try to:您的代码正在运行:尝试:

dev.off()
x <- rbind(matrix(rnorm(100, mean=0.2, sd=0.1), ncol=2), matrix(rnorm(100, mean=0.2, sd=0.1), ncol=2))
plot(x)

output: output: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写函数以在R中生成点序列? - How to write a function to generate a sequence of points in R? 如何干净地使用点之间的插值来生成R中的均值 - How to cleanly use interpolation between points to generate a mean in R 如何根据值生成密度的地图,而不是R中的点数? - How to generate a map with density based on value, not amount of points in R? 为点 R 生成圆形边界 - generate circular boundary for points R R 如何在给定起点和终点的小标题中生成序列 - R How to Generate Sequences in a Tibble Given Start and End Points 如何生成椭圆柱,用随机分布的点填充它,并测量 R 中这些点之间的重叠实例 - How to generate an elliptical cylinder, populate it with randomly distributed points, and measure instances of overlap between those points in R R 使用 runifpoint function 生成具有条件的点 - R generate points with condition using runifpoint function 如何使用模式生成空间点 - How to generate spatial points with a pattern 如何在python或R中统一生成半圆内的一组随机点? - how to generate a set of random points within a half-circle uniformly in python or R? R:如何用点“填充”网格 - R: How to “fill” a grid with points
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM