如何在三角形的每一侧制作正方形,而又不用硬编码正方形?
我有一个data.frame
包含4个点,使用GEOM_PATH
连接时会创建一个三角形:
library(ggplot2)
triangle = data.frame(x = c(0, 0.5, 1, 0),
y = c(0, 0.5, 0, 0))
ggplot(triangle, aes(x, y)) +
geom_path()
现在,我想创建一个新的< code> data.frame (基于三角形
),有4分(例如xmin
,xmax
,ymin
,ymax
)从三角形的侧面创建正方形(因此,此data.frame
将有3行(对于每个方形)和4列(每个点
)
。 /renzd.png“ alt =”在此处输入图像描述“>
是否可以在不硬编码正方形的侧面进行操作?
I have a data.frame
containing 4 points, which creates a triangle when connected using geom_path
:
library(ggplot2)
triangle = data.frame(x = c(0, 0.5, 1, 0),
y = c(0, 0.5, 0, 0))
ggplot(triangle, aes(x, y)) +
geom_path()
Now, I want to create a new data.frame
(based on triangle
), that has 4 points (e.g. xmin
, xmax
, ymin
, ymax
) that creates squares from the sides of the triangle (hence, this data.frame
will have 3 rows (for each square) and 4 columns (for each point).
Here is an example:
Is it possible to do it without hard-coding the sides of the squares?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于正方形的角度将以角度为单位,因此您可能需要以顶点的x,y坐标为单位的输出。这只需要一点触发。以下函数采用x,y点的向量表示封闭的三角形,并返回两侧正方形顶点的数据框架:
输出看起来像这样:
您可以在图中使用它:
Since the squares will be at an angle, you probably need the output to be in terms of x, y co-ordinates of the vertices. This just requires a bit of trig. The following function takes a vector of x, y points representing a closed triangle and returns a data frame of the vertices of the squares on each side:
The output looks like this:
And you can use it in your plot like this: