rectxy: Generating Coordinates of Multiple Rectangles

Description Usage Arguments Value Examples

View source: R/rectxy.R

Description

Note: the shapes are correct only when ggplot2::coord_fixed() is used.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
rectxy(
  x = 0,
  y = 0,
  a = 1,
  b = 1,
  angle = 0,
  xytype = "middle",
  group = TRUE,
  todf = TRUE,
  checks = TRUE
)

Arguments

x

the x coordinates of relative points. Its length can be larger than 1. See xytype.

y

the y coordinates of relative points. Its length can be larger than 1. See xytype.

a

the side that is parallel to x-axis before rotation. Its length can be larger than 1.

b

the side that is parallel to y-axis before rotation. Its length can be larger than 1.

angle

default is 0. The rotation angle in radian. Note: "radian = degree * pi / 180". Its length can be larger than 1. The rotation direction is anti-clockwise.

xytype

should be one of "middle/center" (default), "bottomleft", "middleleft/centerleft/left". It indicates the type of argument of the middle point of an shape. If it is "middleleft", x and y are the middle-left coordinates before rotation. If it is "bottomleft", x and y are the coordinates of the bottom-left corner.

group

default is TRUE. It indicates whether to add a 3rd column named "g" to label the group number of each group of points. It is useful when using aes(...group=g) with 'ggplot2'.

todf

default is TRUE. It indicates whether to combine the output (a list) into a data frame.

checks

default is TRUE. It indicates whether to check input validity. Do not turn it off unless you are sure that the input is OK.

Value

if todf = TRUE, the output will be a data frame with coordinates of possibly several polygons, otherwise, it will be a list of data frames. Data frames have 2 columns named "x" and "y", and if group = TRUE, a third column named "g" is added indicating group numbers.

Examples

1
2
3
4
5
6
7
library(ggplot2)
dat1=rectxy(x=4, y=3, a=2, b=1, angle=0, xytype="bottomleft", todf=TRUE) 
dat2=rectxy(x=4, y=3, a=2, b=1, angle=pi/6, xytype="bottomleft", todf=TRUE)
ggplot()+
	geom_polygon(data=dat1, aes(x=x, y=y), fill="red", alpha=0.3)+
	geom_polygon(data=dat2, aes(x=x, y=y), fill="blue", alpha=0.3)+	
	coord_fixed()

plothelper documentation built on July 2, 2020, 4:03 a.m.