Skip to content

Commit

Permalink
Develop the operations vignette! (fix #13)
Browse files Browse the repository at this point in the history
  • Loading branch information
markromanmiller committed May 27, 2020
1 parent dcb8481 commit 344bafc
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 117 deletions.
11 changes: 11 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ spiral %>%
coord_equal()
```

# Installation

```
install.packages("devtools") # if you have not installed "devtools" package
devtools::install_github("MrMallIronmaker/dddr")
```

# Questions

If you have questions in how to use this library, what this library is useful for, or if it hase any particular features, it would be helpful to contact me directly at `mrmillr at stanford.edu`. This project is in its early development stages, and I want to know how you want to use the library / are using the library.

# Contributions

Contributions are welcome! The issue tracker in this repo can be used for bug reports, feature requests, and questions - whatever you might need.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ spiral %>%

![](README_files/figure-gfm/unnamed-chunk-3-1.png)<!-- -->

# Installation

install.packages("devtools") # if you have not installed "devtools" package
devtools::install_github("MrMallIronmaker/dddr")

# Questions

If you have questions in how to use this library, what this library is
useful for, or if it hase any particular features, it would be helpful
to contact me directly at `mrmillr at stanford.edu`. This project is in
its early development stages, and I want to know how you want to use the
library / are using the library.

# Contributions

Contributions are welcome\! The issue tracker in this repo can be used
Expand Down
117 changes: 0 additions & 117 deletions vignettes/dddr.Rmd

This file was deleted.

113 changes: 113 additions & 0 deletions vignettes/operations.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
---
title: "3D Operations using `dddr`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{operations}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.width=8,
fig.height=5
)
```

```{r setup, message=FALSE}
library(dplyr)
library(ggplot2)
library(dddr)
```

The R package `dddr` works well with three-diensional spatial data. It gives three-dimensional objects like points, velocities, and rotations first-class vector status in R, enabling usage as column types within `data.frame` and `tibble`.

# Vector Mathematics

Vectors have addition and subtraction operations, as well as scalar multiplication.

```{R}
spiral <- data.frame(i = seq(0, 10*pi, by=0.05)) %>%
mutate(
# vector3s are created using three numeric vector arguments,
# and thanks to dplyr, can refer to other columns in the dataframe
circular_part = vector3(x=cos(i), y=sin(i), z=0),
forward_part = vector3(x=0, y=0, z=i/15),
# vector3s can be added together and multiplied by numerics
spiral_part = circular_part * i / 30 + forward_part
)
ggplot(spiral, aes(x=spiral_part$x, y=spiral_part$y)) +
geom_point() +
geom_path() +
coord_equal()
```

```{R}
spiral %>%
mutate(addition_example = spiral_part + c(1, 1, 0)) %>%
ggplot() +
geom_point(aes(x=spiral_part$x, y=spiral_part$y), color="black") +
geom_path(aes(x=spiral_part$x, y=spiral_part$y), color="black") +
geom_point(aes(x=addition_example$x, y=addition_example$y), color="blue") +
geom_path(aes(x=addition_example$x, y=addition_example$y), color="blue") +
coord_equal()
```

```{R}
spiral %>%
mutate(multiplication_example = spiral_part * 1.5) %>%
ggplot() +
geom_point(aes(x=spiral_part$x, y=spiral_part$y), color="black") +
geom_path(aes(x=spiral_part$x, y=spiral_part$y), color="black") +
geom_point(aes(x=multiplication_example$x, y=multiplication_example$y), color="blue") +
geom_path(aes(x=multiplication_example$x, y=multiplication_example$y), color="blue") +
coord_equal()
```

# Rotations

Vectors can be rotated using the `rotate` function in many different ways. One approach is a very simple axis-angle.

```{R}
spiral %>%
mutate(axis_angle_rotation = rotate(spiral_part, axis=c(0, 1, 0), angle=pi/4)) %>%
ggplot() +
geom_point(aes(x=spiral_part$x, y=spiral_part$y), color="black") +
geom_path(aes(x=spiral_part$x, y=spiral_part$y), color="black") +
geom_point(aes(x=axis_angle_rotation$x, y=axis_angle_rotation$y), color="blue") +
geom_path(aes(x=axis_angle_rotation$x, y=axis_angle_rotation$y), color="blue") +
coord_equal()
```

Another approach uses from/to specific vectors.

```{R}
spiral %>%
mutate(from_to_rotation = rotate(spiral_part, from=c(0, 0, 1), to=c(-1, -1, -1))) %>%
ggplot() +
geom_point(aes(x=spiral_part$x, y=spiral_part$y), color="black") +
geom_path(aes(x=spiral_part$x, y=spiral_part$y), color="black") +
geom_point(aes(x=from_to_rotation$x, y=from_to_rotation$y), color="blue") +
geom_path(aes(x=from_to_rotation$x, y=from_to_rotation$y), color="blue") +
coord_equal()
```

# Spatial Functions

There are a handful of other useful functions, including `angle_between`, and vector projection and rejection.

```{R}
spiral %>%
mutate(angle = angle_between(spiral_part, c(0, 0, 0), origin=c(0, -2, 0))) %>%
ggplot() +
geom_point(aes(x=i, y=angle)) +
geom_line(aes(x=i, y=angle))
```





0 comments on commit 344bafc

Please sign in to comment.