Learn R Programming

artpack (version 0.1.0)

rotator: Rotate Points in a Data Frame Based on an Anchor Point

Description

Rotates the x and y points in a given data frame by a given angle based on a designated anchor point.

Usage

rotator(data, x, y, angle = 5, anchor = "center", drop = FALSE)

Value

A data frame

Arguments

data

A data frame or tibble with at least x and y variables

x

A numeric variable in data. The variable intended to be plotted on the x axis in a ggplot.

y

A numeric variable in data. The variable intended to be plotted on the y axis in a ggplot.

angle

The angle (in degrees) the points in data will be rotated around it's anchor

anchor

The anchor point for the rotation. Default is "center". Options include:"center", "bottom", "top", "left", and "right"

drop

Logical TRUE or FALSE that determines if all other variables that are not being rotated are removed from the final output. Default is FALSE.

Examples

Run this code
library(ggplot2)
original_square <- data.frame(
  x = c(0, 3, 3, 0, 0),
  y = c(0, 0, 3, 3, 0)
)
rotated_square <- rotator(data = original_square,
                          x = x,
                          y = y,
                          angle = 45,
                          anchor = "center")

ggplot()+
  geom_path(data = original_square,
                     aes(x,y),
                     color = "red")+
  geom_polygon(data = rotated_square,
                        aes(x,y),
                        fill = "purple")+
  coord_equal()


Run the code above in your browser using DataLab