-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.R
More file actions
158 lines (127 loc) · 4.36 KB
/
demo.R
File metadata and controls
158 lines (127 loc) · 4.36 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
157
158
########################################################################
# Introduce the concept of scatter plots using bee swarm through ML Hub
#
# Copyright 2018 Graham.Williams@togaware.com
library(mlhub)
mlcat("Introducing Bee Swarm Plots",
"The beeswarm package provides jittered scatter plots that avoid
the typical overplotting of traditional scatter plots.
The demonstration here is motivated by the examples published on
github: https://github.com/eclarke/ggbeeswarm/blob/master/README.md")
#-----------------------------------------------------------------------
# Load required packages from local library into the R session.
#-----------------------------------------------------------------------
suppressMessages(
{
library(magrittr) # Data pipelines: %>% %<>% %T>% equals().
library(rattle) # Support: normVarNames(), weatherAUS.
library(ggbeeswarm) #
library(ggplot2) # Visualise data.
library(dplyr) # Wrangling: sample_n()
library(randomForest) # Missing data: na.roughfix().
})
#-----------------------------------------------------------------------
# Identify the data source and refence using template variables.
#-----------------------------------------------------------------------
dsname <- "weatherAUS"
ds <- get(dsname) %>% sample_n(1000)
names(ds) %<>% normVarNames()
xv <- "rain_tomorrow"
yv <- "humidity_3pm"
vars <- c(xv, yv)
ds[vars] %<>% na.roughfix() # Removing missing values.
#-----------------------------------------------------------------------
# Start with a basic scatter plot.
#-----------------------------------------------------------------------
mlask()
mlcat("Basic Scatter Plot",
"A basic scatter plot displays points on the plot without regard for
over-plotting of the points. Consequently if there are multiple observations
with the same x and y values only one will be visible. A scatter plot adds a
points geomtery to the canvas.
ds %>%
ggplot(aes(x=rain_tomorrow, y=humidity_3pm)) +
geom_point()")
# Plot a basic jitter plot.
fname <- "plot_scatter.pdf"
pdf(file=fname, width=8)
ds %>%
ggplot(aes_string(x=xv, y=yv)) +
geom_point()
invisible(dev.off())
mlask()
mlpreview(fname)
mlask()
#-----------------------------------------------------------------------
# Compare that to a basic jitter plot
#-----------------------------------------------------------------------
mlcat("Jitter the Points",
"The jitter geometry can be used in place of points to jitter the
over-plotted points.")
mlask()
# Plot a basic jitter plot.
fname <- "plot_jitter.pdf"
pdf(file=fname, width=8)
ds %>%
ggplot(aes_string(x=xv, y=yv)) +
geom_jitter()
invisible(dev.off())
mlask()
mlpreview(fname)
mlask()
#-----------------------------------------------------------------------
# Compare it to a quasirandom layout
#-----------------------------------------------------------------------
mlcat("Quasi Random Points",
"Compare the jittered plot to the more informative geom_quasirandom()")
fname <- "plot_jitter_quasirandom.pdf"
pdf(file=fname, width=8)
ds %>%
ggplot(aes_string(x=xv, y=yv)) +
geom_quasirandom()
invisible(dev.off())
mlask()
mlpreview(fname)
mlask()
#-----------------------------------------------------------------------
# Compare it to a deeswarm layout
#-----------------------------------------------------------------------
mlcat("Bee SwarmTITLE",
"Use geom_beeswarm()")
fname <- "plot_jitter_beeswarm.pdf"
pdf(file=fname, width=8)
ds %>%
ggplot(aes_string(x=xv, y=yv)) +
geom_beeswarm()
invisible(dev.off())
mlask()
mlpreview(fname)
mlask()
#-----------------------------------------------------------------------
# A tukey plot.
#-----------------------------------------------------------------------
mlcat("Tukey Jitters",
"Use Tukey's method for jittering.")
fname <- "plot_quasirandom_tukey.pdf"
pdf(file=fname, width=8)
ds %>%
ggplot(aes_string(x=xv, y=yv)) +
geom_quasirandom(method="tukey")
invisible(dev.off())
mlask()
mlpreview(fname)
mlask()
#-----------------------------------------------------------------------
# A smiley plot.
#-----------------------------------------------------------------------
mlcat("Smiley",
"How about a smiley plot?")
fname <- "plot_quasirandom_smiley.pdf"
pdf(file=fname, width=8)
ds %>%
ggplot(aes_string(x=xv, y=yv)) +
geom_quasirandom(method="smiley")
invisible(dev.off())
mlask()
mlpreview(fname)
mlask()