rotatexy: Rotation Transformation

Description Usage Arguments Value Examples

View source: R/rotatexy.R

Description

A2 (output) is the result of rotating A1 (input) around a point. Note: the two shapes look the same (though with different angles) only when ggplot2::coord_fixed() is used.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
rotatexy(
  x,
  angle = pi/4,
  xmiddle = 0,
  ymiddle = 0,
  f = NULL,
  group = TRUE,
  todf = TRUE,
  checks = TRUE
)

Arguments

x

the input. It can be a data frame, matrix, tibble object, or a list of these kinds of objects. Each object must have exactly 2 columns and must be numeric without NA. If it has more than 2 columns, only the first 2 columns will be used.

angle

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

xmiddle

the x coordinates of rotation centers. Its length can be larger than 1.

ymiddle

the y coordinates of rotation centers. Its length can be larger than 1.

f

argument passed to split to divide a data frame into a list of data frames. It should be a vector whose length is equal to the number of rows of x (if x is a data frame).

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
 8
 9
10
11
library(ggplot2)
dat1=data.frame(x=c(0, 4, 4, 0), y=c(0, 0, 2, 2))
dat2=data.frame(x=c(5, 6, 6, 5), y=c(4, 4, 8, 8))
dat3=rotatexy(list(dat1, dat2), angle=c(pi, pi/4), 
	xmiddle=c(0, 5), ymiddle=c(0, 4), todf=TRUE)
ggplot()+
	coord_fixed()+
	geom_polygon(data=dat1, aes(x=x, y=y), fill="red", alpha=0.2)+
	geom_polygon(data=dat2, aes(x=x, y=y), fill="blue", alpha=0.2)+
	geom_polygon(show.legend=FALSE, data=dat3, 
		aes(x=x, y=y, group=g, fill=factor(g)), alpha=0.2)

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