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?
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: