0
votes

I have the raw totals for three values that I was looking to display over time in a stacked bar chart, but I don't know how to display this.
I have the percentage values (.22, et cetera), and the raw numbers.
How would I create a stacked bar chart using ggplot2 considering I have three proportions I am trying to graph. Do I need to melt the data?

I would like to do something like: ggplot(data, aes(fill=condition, y=value, x=specie)) + geom_bar( stat="identity", position="fill") But I do not know how to do this as my data isn't formatted right. Should I use dplyr?

Here is my df:

structure(list(date = structure(c(17405, 17406, 17407, 17408, 
17409, 17410, 17411, 17412, 17413, 17414), class = "Date"), total_membership = c(1, 
1, 1, 1, 1, 188, 284, 324, 354, 390), full_members = c(1, 1, 
1, 1, 1, 188, 284, 324, 354, 390), guests = c(0, 0, 0, 0, 0, 
0, 0, 0, 0, 0), daily_active_members = c(1, 1, 1, 1, 1, 169, 
225, 214, 203, 254), daily_members_posting_messages = c(1, 0, 
1, 0, 1, 111, 110, 96, 67, 70), weekly_active_members = c(1, 
1, 1, 1, 1, 169, 270, 309, 337, 378), weekly_members_posting_messages = c(1, 
1, 1, 1, 1, 111, 183, 218, 234, 255), messages_in_public_channels = c(4, 
0, 0, 0, 1, 252, 326, 204, 155, 135), messages_in_private_channels = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), messages_in_shared_channels = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), messages_in_d_ms = c(1, 0, 0, 0, 
0, 119, 46, 71, 70, 122), percent_of_messages_public_channels = c(0.8, 
0, 0, 0, 1, 0.6792, 0.8763, 0.7418, 0.6889, 0.5253), percent_of_messages_private_channels = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), percent_of_messages_d_ms = c(0.2, 
0, 0, 0, 0, 0.3208, 0.1237, 0.2582, 0.3111, 0.4747), percent_of_views_public_channels = c(0.2857, 
1, 1, 1, 1, 0.8809, 0.9607, 0.945, 0.9431, 0.9211), percent_of_views_private_channels = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), percent_of_views_d_ms = c(0.7143, 
0, 0, 0, 0, 0.1191, 0.0393, 0.055, 0.0569, 0.0789), name = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), public_channels_single_workspace = c(10, 
10, 11, 11, 12, 12, 12, 13, 13, 13), messages_posted = c(35, 
35, 37, 38, 66, 1101, 1797, 2265, 2631, 3055)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))
1
Please read this document. Without an example data (preferably included using dput) it is hard to say wht you should or should not use. - January
Can you update your post with what you have tried using your data? And what output you expect exactly? - kstew

1 Answers

1
votes

Here is an example using a toy data set, where the original data are first grouped and summarised to get the 'proportions', then piped to ggplot, which will automatically create a stacked bar plot

df <- data.frame(group=sample(letters[1:10],1000,T),
                 species=sample(1:4,1000,T),
                 amount=sample(10:30,1000,T))

df %>% group_by(group,species) %>% summarise(perc=mean(amount)) %>% 
  ggplot(aes(group,perc,fill=factor(species))) + 
  geom_bar(stat='identity')

enter image description here

UPDATE This will calculate the proportion that 'species' occurs within each 'group'.

df %>% group_by(group,species) %>% summarise(n=n()) %>% 
  group_by(group) %>% mutate(perc=n/sum(n)) %>% 
  ggplot(aes(group,perc,fill=factor(species))) + 
  geom_bar(stat='identity')