如何在R中的矩阵中绘制随机点
我需要绘制一组点数是一个5x5网格。 基本上,我需要绘制一组2点(可以是水平或垂直的),而另外3个点是一个5x5网格。
这些点的位置应随机归因。
最后,它看起来应该像这样(我可以在“静态”代码中做,但我不能以随机的atributtion做到这一点)
par(xaxs = "i", yaxs = "i")
plot.new()
plot.window(xlim = c(0.5, 5.5), ylim = c(0.5, 5.5))
grid(nx = 5, ny = 5, col = "black", lty = "dashed")
box(lwd = 1)
# PLot points
points(1, 5, pch = 16, col = "blue", cex = 1.5)
points(2, 5, pch = 16, col = "blue", cex = 1.5)
points(1, 1, pch = 16, col = "pink", cex = 1.5)
points(1, 2, pch = 16, col = "pink", cex = 1.5)
points(1, 3, pch = 16, col = "pink", cex = 1.5)
I need to plot randonmly a set of points is a 5X5 grid.
basically I need to plot a set of 2 points (can be horizontaly or verticaly), and a set of other 3 points, is a 5x5 grid.
the position of the points should be randomly attributed.
at the end it should look like something like this (i can do in a "static" code, but i cannot do it as a random atributtion)
par(xaxs = "i", yaxs = "i")
plot.new()
plot.window(xlim = c(0.5, 5.5), ylim = c(0.5, 5.5))
grid(nx = 5, ny = 5, col = "black", lty = "dashed")
box(lwd = 1)
# PLot points
points(1, 5, pch = 16, col = "blue", cex = 1.5)
points(2, 5, pch = 16, col = "blue", cex = 1.5)
points(1, 1, pch = 16, col = "pink", cex = 1.5)
points(1, 2, pch = 16, col = "pink", cex = 1.5)
points(1, 3, pch = 16, col = "pink", cex = 1.5)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此功能将执行算法。通过列举所有可能有效的蓝点(只有40对)和有效的粉红色点(只有30个)来做到这一点。然后,它采样了一组有效的蓝点,然后反复采样随机的粉红色点,直到没有冲突为止。然后,它使用您的绘图代码绘制结果:
随机测试9次给我们:
This function will perform the algorithm. It does so by enumerating all possible valid pairs of blue dots (there are only 40) and valid triples of pink dots (there are only 30). It then samples a valid set of blue dots, then repeatedly samples random sets of pink dots until there are no clashes. It then uses your plotting code to draw the result:
Testing it out randomly 9 times gives us: