This repository has been archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
recipes.jl
130 lines (112 loc) · 4.25 KB
/
recipes.jl
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
@userplot AbundancePlot
@recipe function f(plt::AbundancePlot; topabund=10, srt=collect(1:nsites(plt.args[1])))
abun = plt.args[1]
typeof(abun) <: AbstractComMatrix || error("AbundancePlot not defined for $(typeof(abun))")
topabund = min(topabund, nfeatures(abun))
2 <= topabund < 12 || error("n must be between 2 and 12")
top = filterabund(abun, topabund)
rows = featurenames(top)
yflip := true
bar_position := :stack
label := rows
StatsPlots.GroupedBar((1:nsamples(top), Matrix(occurrences(top)[:,srt]')))
end
"""
AnnotationBar(::Array{<:Color,1})
Array of colors for plotting a grid. Use `plot(::AnnotationBar; kwargs...)`
"""
struct AnnotationBar
colors::Array{<:Color,1}
end
"""
annotationbar(colors::AbstractArray{<:Color,1})
annotationbar(labels::AbstractArray{<:AbstractString,1})
annotationbar(labels::AbstractArray{<:AbstractString,1}, colormap::Dict{<:AbstractString,<:Color})
annotationbar(labels::AbstractArray{<:AbstractString,1}, colors::AbstractArray{<:Color,1})
Create an `AnnotationBar` from a vector of colors or a vector of `labels` (`String`s).
- A vector of `Color`s will be used directly to create an `AnnotationBar`
- For an array of `String`s, each unique value will be assigned a random color from
`Colors.color_names`.
- A vector of labels may be passed with a `Dict` with keys for each `label` mapping
to a `Color` value.
- A vector of `labels` and `colors` may be used.
- If the lengths of these vectors are the same,
the `colors` will be used to create an AnnotationBar
- If the length of `labels` is longer than the length of `colors`,
each unique label will be assigned to one of the `colors`
"""
annotationbar(colors::AbstractArray{<:Color,1}) = AnnotationBar(colors)
function annotationbar(labels::AbstractArray{<:AbstractString,1}, colormap::Dict{<:AbstractString,<:Color})
colorkeys = keys(colormap)
all(label-> in(label, colorkeys), labels) || throw(ArgumentError("Colormap missing keys $(setdiff(labels, colorkeys))"))
AnnotationBar([colormap[label] for label in labels])
end
function annotationbar(labels::AbstractArray{<:AbstractString,1}, colors::AbstractArray{<:Color,1})
if length(labels) == length(colors)
return annotationbar(colors)
end
ulabels = unique(labels)
length(colors) < length(ulabels) && throw(ArgumentError("Must have at least 1 color for each unique label"))
colormap = Dict(l => colors[i] for (i, l) in enumerate(ulabels))
annotationbar(labels, colormap)
end
# if no colors are passed, give a unique random color to each label
function annotationbar(labels::AbstractArray{<:AbstractString,1})
ulabels = unique(labels)
cnames = collect(keys(Colors.color_names))
colors = cnames[randperm(length(cnames))[1:length(ulabels)]]
colormap = Dict(l => parse(Colorant, colors[i]) for (i, l) in enumerate(ulabels))
annotationbar(labels, colormap)
end
@recipe function f(bar::AnnotationBar)
xs = Int[]
for i in 1:length(bar.colors)
append!(xs, [0,0,1,1,0] .+ (i-1))
end
xs = reshape(xs, 5, length(bar.colors))
ys = hcat([[0,1,1,0,0] for _ in bar.colors]...)
fc = reshape(bar.colors, 1, length(bar.colors))
seriestype := :path
fill := (0,1)
fillcolor := fc
legend := false
color --> :black
ticks := false
framestyle := false
xaxis --> false
yaxis --> false
xs, ys
end
# From Michael K. Borregaard (posted to slack 2018-05-18)
# Usage:
#
# x = randn(100)
# y = randn(100) + 2
# y[y.<0] = 0
#
# zeroyplot(x,y, yscale = :log10, color = :red, markersize = 8)
# @userplot ZeroYPlot
# @recipe function f(h::ZeroYPlot)
# length(h.args) != 2 && error("zeroyplot only defined for x and y input arguments")
# x, y = h.args
# val0 = y .== 0
#
# layout := @layout([a
# b{0.05h}])
#
# markeralpha --> 0.4
# seriestype --> :scatter
#
# @series begin
# primary := false
# subplot := 2
# yscale := :identity
# ylim := (-1,1)
# yticks := ([0],[0])
# grid := false
# x[val0], y[val0]
# end
#
# subplot := 1
# x[.!val0], y[.!val0]
# end