r - How to calculate share per category within a column? -
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 16 2 2 b 14 3 1 c 23 4 2 d 15 5 1 e 12 6 2 15 7 1 b 23 8 2 c 22 9 1 d 22 10 2 e 26
i'd calculate each name's count share per week. @ first going use 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 making each column merge, put label on bar plot, seemed inefficient. there must type of quick solution dont know of yet.
basically follows add share% may have been calculated above 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 reorder function fails upon me. if have tips sort order in desc, please share.
the data provided has been used:
# loading required data 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))
using plyr
package function, percentage , relative positions labelling have been calculated.
#loading required packages library(plyr) library(ggplot2) # calculating percentages df = ddply(df, .(week), transform, percent = round(count/sum(count) * 100)) # calculating position plotting df = ddply(df, .(week), transform, pos = cumsum(percent) - (0.5 * percent))
using information calculated above, plotting has been done.
# basic graph p10 <- ggplot() + geom_bar(aes(y = percent, x = week, fill = name), data = df, stat="identity") # adding data labels p10 <- p10 + geom_text(data=df, aes(x = week, y = pos, label = paste0(percent,"%")), size=4) p10
is have been looking ?
Comments
Post a Comment