如何使用ggplot在圆内随机散布点,而不会围绕中心聚集? [英] How to randomly scatter points inside a circle with ggplot, without clustering around the center?
问题描述
我想用 ggplot
画一个圆,然后在里面散布点.我有代码( (v2.0.0) 于 2021 年 8 月 2 日创建上>
应该很容易注意到围绕每个圆中心的集群.如何在圆内随机排列点,避开中心的簇?
我对 sample()
我想解决方案涉及修改df_circular_points_scatter
数据.但是,我不知道如何.我试图用 sample()
包裹 df_circular_points_scatter
的每一列,但后来得到了超过圆周的点,并且总体上是一个交叉".安排.
也就是说,如果我们像这样用 sample()
包裹:
r <- runif(n)th <- runif(n)df_circular_points_scatter <-data.frame(x = 样本(r*cos(2*pi*th)),y = 样本(r*sin(2*pi*th)))
那么结果是:
知道如何避免聚类吗?我不一定想要完全均匀的分散.只是在圈内随机.
期望的输出
采用 问题)>
结果:
I want to use ggplot
to draw a circle, then scatter points inside it. I have code (adopted from this answer) that gets me pretty close to what I want. However, I want the points to scatter inside the circle randomly, but right now I get an undesired cluster around the center.
I saw a similar SO question and answer, but it's in c# and I don't understand how to adapt it to R
code.
My code so far
The following code defines a custom visualization function vis_points_inside_circle()
, and then calls it 4 times to give 4 examples of visualizing using my current method.
library(ggplot2)
library(ggforce)
## set up function
vis_points_inside_circle <- function(n) {
# part 1 -- set up empty circle
df_empty_circle <-
data.frame(x = 0,
y = 0,
r = 1)
p_empty_circle <-
ggplot(df_empty_circle) +
geom_circle(aes(x0 = x, y0 = y, r = r)) +
coord_fixed() +
theme_void()
# part 2 -- set up points scatter
r <- runif(n)
th <- runif(n)
df_circular_points_scatter <-
data.frame(x = r*cos(2*pi*th), ## from @Ben's answer: https://stackoverflow.com/a/68606605/6105259
y = r*sin(2*pi*th))
# part 3 -- combine circle and points
p_empty_circle +
geom_point(data = df_circular_points_scatter,
aes(x = x, y = y))
}
## visualize
library(gridExtra)
set.seed(2021)
p1000_1 <- vis_points_inside_circle(n = 1000)
p1000_2 <- vis_points_inside_circle(n = 1000)
p2000_1 <- vis_points_inside_circle(n = 2000)
p2000_2 <- vis_points_inside_circle(n = 2000)
gridExtra::grid.arrange(p1000_1, p1000_2, p2000_1, p2000_2, nrow = 2)
Created on 2021-08-02 by the reprex package (v2.0.0)
It should be easy to notice the cluster around the center of each circle. How can we randomly arrange the points within the circle, to avoid the cluster at the center?
My attempt with sample()
I guess the solution involves the modification of df_circular_points_scatter
data. However, I don't know how. I've tried to wrap each of df_circular_points_scatter
's columns with sample()
, but then got points that exceed the circumference, and overall a "cross" arrangement.
That is, if we wrap with sample()
like this:
r <- runif(n)
th <- runif(n)
df_circular_points_scatter <-
data.frame(x = sample(r*cos(2*pi*th)),
y = sample(r*sin(2*pi*th)))
Then the result is:
Any idea how to avoid clustering? I don't necessarily want a perfectly even scatter. Just random within the circle.
Desired output
Adopted from this answer, the desired output should look something like:
You are almost there. The sampling needs to be done as follows:
r <- runif(n)
th <- runif(n, 0, 2 * pi)
df_circular_points_scatter <-
data.frame(x = sqrt(r) * cos(th),
y = sqrt(r) * sin(th)
)
(see this question on crossvalidated)
Result:
这篇关于如何使用ggplot在圆内随机散布点,而不会围绕中心聚集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!