-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoordinates.Rmd
More file actions
156 lines (120 loc) · 3.48 KB
/
coordinates.Rmd
File metadata and controls
156 lines (120 loc) · 3.48 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
---
title: Coordinates
---
```{r}
library(gglite)
```
Coordinate systems control how the positional channels (x and y) are
interpreted. Use `coordinate()` to set the type and options.
## Cartesian (default)
The default coordinate system. Typically you don't need to specify it
explicitly, but you can do so for clarity.
```{r}
df = data.frame(x = c('A', 'B', 'C', 'D'), y = c(3, 7, 2, 5))
g2(df, x = 'x', y = 'y') |>
mark_interval() |>
coordinate('cartesian')
```
## Polar
Maps x to angle and y to radius. Useful for rose charts and radar-like
displays.
```{r}
g2(df, x = 'x', y = 'y') |>
mark_interval() |>
coordinate('polar')
```
### Polar with innerRadius (donut rose)
```{r}
g2(df, x = 'x', y = 'y', color = 'x') |>
mark_interval() |>
coordinate('polar', innerRadius = 0.4)
```
### Polar with custom start/end angles
```{r}
g2(df, x = 'x', y = 'y', color = 'x') |>
mark_interval() |>
coordinate('polar', startAngle = -pi / 2, endAngle = pi / 2)
```
## Theta
Maps data values to angular extent. Used for pie and donut charts.
```{r}
g2(df, x = 'x', y = 'y', color = 'x') |>
mark_interval() |>
transform_of('stackY') |>
coordinate('theta')
```
### Donut chart (theta with innerRadius)
```{r}
g2(df, x = 'x', y = 'y', color = 'x') |>
mark_interval() |>
transform_of('stackY') |>
coordinate('theta', innerRadius = 0.5)
```
## Radial
Maps y to the radial direction. Suitable for radial bar charts.
```{r}
g2(df, x = 'x', y = 'y', color = 'x') |>
mark_interval() |>
coordinate('radial')
```
### Radial with innerRadius
```{r}
g2(df, x = 'x', y = 'y', color = 'x') |>
mark_interval() |>
coordinate('radial', innerRadius = 0.3)
```
## Parallel
Maps multiple numeric variables to parallel axes. Use a `position`
encoding (a character vector of column names) instead of `x`/`y`.
```{r}
g2(iris, position = c('Sepal.Length', 'Sepal.Width',
'Petal.Length', 'Petal.Width'), color = 'Species') |>
mark_line() |>
coordinate('parallel') |>
legend_of('color', position = 'bottom') |>
padding_of(top = 30)
```
## Radar
Displays data on radial axes emanating from a center point. A radar chart
is a line or area chart in polar coordinates. Use long-format data with
`x` (category), `y` (value), and `color` (series) encodings. All values
should be on the same scale (e.g., 0--100).
```{r}
df_radar = data.frame(
item = rep(c('Design', 'Dev', 'Marketing', 'Sales', 'Support'), 2),
score = c(80, 90, 65, 75, 85, 60, 70, 85, 80, 70),
team = rep(c('A', 'B'), each = 5)
)
g2(df_radar, x = 'item', y = 'score', color = 'team') |>
mark_area(style = list(fillOpacity = 0.5)) |>
mark_line(style = list(lineWidth = 2)) |>
mark_point() |>
coordinate('polar') |>
scale_of('x', padding = 0.5, align = 0) |>
scale_of('y', domainMin = 0, domainMax = 100) |>
axis_of('y', grid = TRUE, title = FALSE)
```
## Helix
Arranges data along a helix spiral. Works best with `mark_interval()`.
```{r}
df_helix = data.frame(x = paste0('D', 1:50), y = abs(sin(1:50 / 5)))
g2(df_helix, x = 'x', y = 'y', color = 'y') |>
mark_interval() |>
coordinate('helix')
```
## Transpose (coord_flip)
Swap x and y axes, equivalent to ggplot2's `coord_flip()`. This is a
coordinate transform, not a type.
```{r}
df = data.frame(x = c('A', 'B', 'C', 'D'), y = c(3, 7, 2, 5))
g2(df, x = 'x', y = 'y') |>
mark_interval() |>
coord_transpose()
```
### Transpose with parallel
```{r}
g2(iris, position = names(iris)[-5], color = 'Species') |>
mark_line() |>
coordinate('parallel') |>
coord_transpose()
```