简体   繁体   中英

Percentage stacked barplot in Julia

I would like to create a percentage stacked barplot in Julia. In R we may do the following:

set.seed(7)
data <- matrix(sample(1:30,6), nrow=3)
colnames(data) <- c("A","B")
rownames(data) <- c("V1","V2","V3")
library(RColorBrewer)
cols <- brewer.pal(3, "Pastel1") 
df_percentage <- apply(data, 2, function(x){x*100/sum(x,na.rm=T)})
barplot(df_percentage, col=cols, border="white", xlab="group")

Created on 2022-12-29 with reprex v2.0.2

I am now able to create the axis in percentages, but not to make it stacked and percentage for each stacked bar like above. Here is some reproducible code:

using StatsPlots

measles = [38556, 24472]
mumps = [20178, 23536]
chickenPox = [37140, 32169]
ticklabel = ["A", "B"]
foo = @. measles + mumps + chickenPox
my_range = LinRange(0, maximum(foo), 11)

groupedbar(
  [measles mumps chickenPox],
  bar_position = :stack,
  bar_width=0.7,
  xticks=(1:2, ticklabel),
  yticks=(my_range, 0:10:100),
  label=["measles" "mumps" "chickenPox"]
)

Output:

在此处输入图像描述

This is almost what I want. So I was wondering if anyone knows how to make a stacked percentage barplot like above in Julia ?

Are you looking for this(?):

my_range = LinRange(0, 1, 11)
foo = @. measles + mumps + chickenPox
groupedbar(
  [measles./foo mumps./foo chickenPox./foo],
  bar_position = :stack,
  bar_width=0.7,
  xticks=(1:2, ["A", "B"]),
  yticks=(my_range, 0:10:100),
  label=["measles" "mumps" "chickenPox"]
)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM