-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdensity_and_contours.jl
More file actions
75 lines (63 loc) · 2.02 KB
/
Copy pathdensity_and_contours.jl
File metadata and controls
75 lines (63 loc) · 2.02 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
# generate values from a distribution
using Distributions, LinearAlgebra
using DataFrames
Σ = let X = randn(2,2); X * X' + I; end
d = MvNormal([1,1], Σ)
draws = rand(d, 100)
df = DataFrame(x = draws[1,:], y=draws[2,:])
# create Data object
dat = Data( values= df )
xscale = LinearScale(range="width", domain=dat.x, padding= 20)
yscale = LinearScale(range="height", domain=dat.y, padding= 20)
# create a Data object derived from 'dat' to calculate the density
density = Data(source=dat,
transform= [(
type= "kde2d",
x_expr = "scale('$xscale', datum.x)", # convert to pixel coordinates
y_expr = "scale('$yscale', datum.y)",
size= [(signal= "width",), (signal= "height",)], # output size
as= "grid"
)]
)
# create a Data object, derived from 'density', that will generate
# the iso contours
contours = Data(source=density,
transform= [( type= "isocontour",
field= "grid",
levels= 6
)]
)
# this mark shows a dot for each sample
pointmark = SymbolMark(
:x => xscale(dat.x),
:y => yscale(dat.y),
:size => 4,
)
# this mark plots an image, based on the density data
# the "heatmap" transform translates the density grid to an image
# (by default, it is the image opacity that varies with density, but
# color can be used as well)
densmark = ImageMark(from_data=density,
:x => 0,
:y => 0,
:width => (signal="width",),
:height => (signal="height",),
:aspect => false,
transform= [
(type="heatmap", field="datum.grid", color= :lightblue)
]
)
# this mark shows the density contours
contourmark = PathMark(from_data=contours,
clip= true,
:strokeWidth => 1,
:strokeOpacity => 0.5,
:stroke => :blue,
transform= [
(type="geopath", field="datum.contour")
]
)
vgspec = VG(width=400, height=400, background=:white, padding=10,
axes=[xscale(orient="bottom"), yscale(orient="left")],
marks= [pointmark, densmark, contourmark]
)