-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScatterPlot.Rmd
197 lines (177 loc) · 3.84 KB
/
ScatterPlot.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
---
title: "Scatter Plot"
output: html_document
---
<!--
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{Scatter Plot}
-->
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r setting}
library(nivor)
library(reactable)
data <- data.frame(
index = rep(0:13, 6),
x = rep(0:13, 6),
y = c(
2.0, 2.1, 2.3, 2.5, 2.7, 2.9, 3.0, 3.2, 3.3, 3.5, 3.6, 3.8, 3.9, 4.0,
3.2, 3.3, 3.6, 3.8, 4.1, 4.3, 4.6, 4.8, 5.0, 5.2, 5.4, 5.5, 5.7, 5.8,
4.8, 5.1, 5.4, 5.7, 6.1, 6.5, 6.8, 7.1, 7.3, 7.6, 7.8, 8.1, 8.3, 8.5,
2.5, 2.6, 2.8, 3.1, 3.3, 3.5, 3.8, 4.0, 4.2, 4.4, 4.5, 4.7, 4.9, 5.0,
3.3, 3.5, 3.8, 4.1, 4.4, 4.7, 4.9, 5.2, 5.4, 5.6, 5.8, 6.0, 6.2, 6.4,
5.0, 5.3, 5.6, 6.0, 6.4, 6.8, 7.2, 7.5, 7.8, 8.0, 8.3, 8.5, 8.8, 9.0
),
z = c(
1, 2, 3, 1, 3, 0, 1, 3, 2, 3, 0, 0, 4, 1,
1, 3, 4, 0, 1, 1, 2, 2, 3, 0, 1, 1, 2, 4,
4, 3, 4, 1, 0, 1, 2, 3, 3, 1, 1, 0, 4, 1,
3, 2, 0, 0, 1, 4, 4, 4, 3, 2, 1, 0, 0, 3,
0, 4, 3, 3, 2, 2, 2, 1, 1, 0, 3, 4, 2, 1,
4, 3, 2, 2, 1, 2, 1, 3, 4, 1, 0, 2, 1, 4
),
group = c(
rep("girls low", 14),
rep("girls med", 14),
rep("girls high", 14),
rep("boys low", 14),
rep("boys med", 14),
rep("boys high", 14)
)
)
reactable(data)
common_settings <- list(
width = 800,
height = 400,
margin = list(
top = 30,
right = 30,
bottom = 60,
left = 80
),
nodeSize = 10,
blendMode = "multiply",
xFormat = htmlwidgets::JS("d => `week ${d}`"),
yFormat = htmlwidgets::JS("d => `${d} kg`"),
axisBottom = list(
format = htmlwidgets::JS("d => `week ${d}`")
),
axisLeft = list(
format = htmlwidgets::JS("d => `${d} kg`")
),
legends = list(
list(
anchor = "bottom-left",
direction = "row",
translateY = 60,
itemWidth = 130,
itemHeight = 12,
symbolSize = 12,
symbolShape = "circle"
)
)
)
```
## Default
In R, we use `do.call` to constructs and executes a function call from a name or a function and a list of arguments to be passed to it.
```{r default}
do.call(
n_scatter,
n_options(
default = common_settings,
data = data[data$group == "girls low", ],
x = "x",
y = "y",
group = "group" # use group column for series name
)
)
```
## Multiple Series
```{r multiple series}
do.call(
n_scatter,
n_options(
default = common_settings,
data = data,
x = "x",
y = "y",
group = "group"
)
)
```
## Alternative colors
```{r alternative colors}
do.call(
n_scatter,
n_options(
default = common_settings,
data = data,
x = "x",
y = "y",
group = "group",
colors = list(
scheme = "category10"
)
)
)
```
## Using Time Scales
```{r using time scales}
data <- data.frame(
group = c(rep("apple", 8), rep("oranges", 8)),
x = rep(seq.Date(as.Date("2018-01-01"), by = "days", length.out = 8), 2),
y = c(7, 5, 11, 9, 12, 16, 13, 13, 14, 14, 15, 11, 10, 12, 9, 7)
)
reactable(data)
settings <- n_options(
default = common_settings,
data = data,
x = "x",
y = "y",
group = "group",
xScale = list(
type = "time",
format = "%Y-%m-%d",
precision = "day"
),
xFormat = "time:%Y-%m-%d",
axisBottom = list(
format = "%b %d",
tickValues = "every 2 days"
)
)
do.call(n_scatter, settings)
```
## Using Logarithmic Scales
```{r using logarithmic scales}
data <- data.frame(
name = "apples",
x = c(10, 100, 1000, 10000, 100000, 1000000),
y = c(2, 4, 8, 16, 32, 64)
)
settings <- n_options(
default = common_settings,
data = data,
x = "x",
y = "y",
group = "name",
xScale = list(
type = "log",
base = 10
),
xFormat = NA,
yScale = list(
type = "log",
base = 2
),
yFormat = NA,
axisBottom = list(
tickValues = c(10, 100, 1000, 1000, 10000, 100000, 1000000, 10000000)
),
axisLeft = list(
tickValues = c(2, 4, 8, 16, 32, 64)
)
)
do.call(n_scatter, settings)
```