-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
118 lines (91 loc) · 2.9 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "70%"
)
set.seed(1)
```
# ggquadrilateral
<!-- badges: start -->
<!-- badges: end -->
`ggquadrilateral` provides a [ggplot2](https://ggplot2.tidyverse.org/index.html) geom that can draw
arbitrary quadrilaterals in a convenient way.
## Installation
You can install the latest version of ggquadrilateral from [GitHub](https://github.com/const-ae/ggquadrilateral) with:
```{r eval=FALSE}
devtools::install_github("const-ae/ggquadrilateral")
```
If you don't already have devtools installed, you can get it from CRAN with `install.packages("devtools")`.
## Example
First load `ggplot2` and the `ggquadrilateral` package
```{r example}
library(ggplot2)
library(ggquadrilateral)
```
The simplest example is to just define the positions of the four corners manually
```{r}
kite_df <- data.frame(
left_tip_x = 2,
left_tip_y = 7,
top_tip_x = 3,
top_tip_y = 8,
right_tip_x = 4,
right_tip_y = 7,
bottom_tip_x = 3,
bottom_tip_y = 3
)
kite_df
```
```{r}
ggplot(kite_df) +
geom_quadrilateral(aes(x1=left_tip_x, y1 = left_tip_y,
x2 = top_tip_x, y2 = top_tip_y,
x3 = right_tip_x, y3 = right_tip_y,
x4 = bottom_tip_x, y4 = bottom_tip_y),
color = "black", fill = "purple", size=4) +
xlim(-2, 8) + ylim(0, 10)
```
It can also be used to visualize more complex data.
We will now use it to draw the triangle mesh for
10 random points.
```{r}
df <- data.frame(x=rnorm(n=10, mean=0, sd=1),
y=rnorm(n=10, mean=0, sd=1))
ggplot(df, aes(x=x, y=y)) +
geom_point()
```
We will use the [`tripack`](https://cran.r-project.org/web/packages/tripack/index.html) package
to calculate the Delauney triangulation.
```{r}
library(tripack)
triang <- as.data.frame(triangles(tri.mesh(df)))
triang_df <- data.frame(id = seq_len(nrow(triang)),
p1x = df$x[triang$node1],
p1y = df$y[triang$node1],
p2x = df$x[triang$node2],
p2y = df$y[triang$node2],
p3x = df$x[triang$node3],
p3y = df$y[triang$node3])
head(triang_df)
```
Using the `triang_df` that the coordinates for the 12 interpolating triangles we can make the plot:
```{r}
ggplot() +
geom_quadrilateral(data=triang_df,
mapping = aes(
x1 = p1x, y1 = p1y,
x2 = p2x, y2 = p2y,
x3 = p3x, y3 = p3y,
# We want triangles, so we make the
# fourth point identical to the third
x4 = p3x, y4 = p3y,
fill = as.factor(id)),
color = "black") +
geom_point(data= df, aes(x=x, y=y), size = 3)
```