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:
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:
Then, I use the maximum of
foo
to create my linear range from zero to the maximum value:Now I use this range in the
groupedbar
function for its keyword argument, namedyticks
to scale the range0: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 replace0:0.1:1
with0:10:100
in theyticks
.