0

I have a set of data that looks basically like this (not real data):

 Group        Color    RelFreq1    
Asparagopsis    1       30       
Asparagopsis    2       30         
Asparagopsis    3       40       
Cyano           1      100        
Dichtomaria     1       20       
Dichtomaria     2       40        

Group        Tissue    RelFreq2
Asparagopsis    5        20
Asparagopsis    6        50
Asparagopsis    7        30
Cyano           0        100
Dichtomaria     5        60
Dicthomaria     8        40

With Several groups and two sets of variables. (color ranges from 1-3, Tissue ranges from 0,5-8). Both sets of variables have calculated relative frequencies for each value. I have made stacked bar graphs for each separately (real data here): graph 1 (Color):

CbAg4_stacked <- ggplot(ColorbyAg4, aes(x=AlgaeGenus, y=RelFreqC, fill=Color))+geom_bar(stat="identity")
CbAg4_stacked <- CbAg4_stacked + ylab("Relative Frequency")

graph 2 (Tissue):

TbAg_stacked <- ggplot(TissuebyAg, aes(x=AlgaeGenus, y=RelFreqT, fill=Tissue))+geom_bar(stat="identity")
TbAg_stacked <- TbAg_stacked + ylab("Relative Frequency")

Stacked bar graph for variable 1

Stacked bar graph for variable 2

(graphs are not perfect of course because they're not how I want the finished product yet)

I would like to have them merged into one plot, with the two stacked bars for each group next to each other. (ex: for Asparagopsis, two stacked bars: one for color, one for tissue). I'll make them different colors too, but what I need help with is getting the two sets of data into the same plot. I can merge the data into one dataframe, but how do I do the graphing?

I've been using ggplot2, but I'm open to using something else if it provides a better solution. ggplot(dfm, aes(x=AlgaeGenus, y=?? How can I specify both RelFreq1 and RelFreq2? I tried c(RelFreq1, RelFreq2) and it didn't like that. I'll also need to give it multiple things for "fill=", both Color and Tissue.

Any advice? Is this possible? Both y axes are the same scale (relative frequencies out of 100), so it seems like there should be a way, but I can't figure it out.

  • I don't see the names on x-axis in your dataset example. try to make it reproducible. You also include the code you used to produce your plot – MLavoie Mar 15 '16 at 22:04
  • Sorry, I added the x axis names from the plots to the dataset example and added in the graph codes. – Tori Sindorf Mar 15 '16 at 22:20
  • do you want one legend (Tissue + color) or one for each? – MLavoie Mar 15 '16 at 22:32
  • @MLavoie One legend would be okay. I might prefer two, but either would be fine. – Tori Sindorf Mar 15 '16 at 22:55
  • @MLavoie why did you delete your answer? – Tori Sindorf Mar 15 '16 at 23:52
  • if this is not what you want there is no reason to keep it there :-) – MLavoie Mar 15 '16 at 23:54
  • 1
    also possible duplicate of: http://stackoverflow.com/questions/21409850/generate-paired-stacked-bar-charts-in-ggplot-using-position-dodge-only-on-some and http://stackoverflow.com/questions/11604070/issue-with-ggplot2-geom-bar-and-position-dodge-stacked-has-correct-y-values – erc Mar 16 '16 at 07:07

0 Answers0