I am using this stacked bar plot example to create a stacked bar plot with a percentage axis. Here is some reproducible code:
using StatsPlots
measles = [38556, 24472, 14556, 18060, 19549, 8122, 28541, 7880, 3283, 4135, 7953, 1884]
mumps = [20178, 23536, 34561, 37395, 36072, 32237, 18597, 9408, 6005, 6268, 8963, 13882]
chickenPox = [37140, 32169, 37533, 39103, 33244, 23269, 16737, 5411, 3435, 6052, 12825, 23332]
ticklabel = string.(collect('A':'L'))
groupedbar([measles mumps chickenPox],
bar_position = :stack,
bar_width=0.7,
xticks=(1:12, ticklabel),
label=["measles" "mumps" "chickenPox"])
Output:
So now it is a nice stacked values barplot, but I would like to make the y-axis in percentage with range from 0 to 100%. Unfortunately there is no option like scale = :percent
. So I was wondering if anyone knows how to make this barplot with percentage y-axis in Julia
?
Mabe this can give you a idea:
foo = @. measles + mumps + chickenPox
my_range = LinRange(0, maximum(foo), 11)
groupedbar(
[measles mumps chickenPox],
bar_position = :stack,
bar_width=0.7,
xticks=(1:12, ticklabel),
yticks=(my_range, 0:0.1:1),
label=["measles" "mumps" "chickenPox"]
)
Explanation:
The idea is to map a linear range from zero up to the height of the tallest bar in the plot. So, first I should find the tallest bar in the plot; for this, I should achieve to values of the height of each bar:
julia> foo = @. measles + mumps + chickenPox
12-element Vector{Int64}:
95874
80177
86650
94558
88865
63628
63875
22699
12723
16455
29741
39098
Then, I use the maximum of foo
to create my linear range from zero to the maximum value:
julia> my_range = LinRange(0, maximum(foo), 11)
11-element LinRange{Float64, Int64}:
0.0,9587.4,19174.8,28762.2,38349.6,47937.0,57524.4,67111.8,76699.2,86286.6,95874.0
Now I use this range in the groupedbar
function for its keyword argument, named yticks
to scale the range 0:0.1:1
to the extremum value of the plot. If you want to change the range from zero to 100 (instead of percent interpretation), then just replace 0:0.1:1
with 0:10:100
in the yticks
.