简体   繁体   中英

How to generate points in R?

I need generate points in R. I want to get plot like in this example:

想要的情节

My main task: Generate a 2D dataset consisting of 3 clusters, each strongly stretched along one of the axes.

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: 方法 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: 方法 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: 方法 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: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM