df = data.frame(week = as.factor(rep(c(1, 2), times = 5)),
name = as.factor(rep(LETTERS[1:5], times = 2)),
count = rpois(n = 10, lambda = 20))
> df
week name count
1 1 A 16
2 2 B 14
3 1 C 23
4 2 D 15
5 1 E 12
6 2 A 15
7 1 B 23
8 2 C 22
9 1 D 22
10 2 E 26
I'd like to calculate each name's count share per week. At first I was going to use the following method:
transform(df, week1_share = ifelse(week == "1", round((df$count / sum(df$count) * 100),2), NA))
transform(df, week2_share = ifelse(week == "2", round((df$count / sum(df$count) * 100),2), NA))
but then making each column to merge, to eventually put it as label on the bar plot, seemed too inefficient. There must be some type of quick solution for this that I dont know of yet.
Basically what I would like to do is as follows but add the share% that may have been calculated as above to match within each box.
ggplot(df, aes(reorder(week, -count),count, color = "white", group = name, fill = name))+
geom_bar(position = "stack", stat = "identity") +
scale_y_continuous(labels=comma)+
ggthemes::scale_color_tableau()
I don't know why the reorder function often fails upon me. If you have any tips to sort the order in desc, please share.