I'm trying to create a graph in R similar to what I have produced in Excel:
.
I am trying to group my bar plot by year and indices, separated by my treatment. I have tried to enter my data in a "long form" into R and came up with this:
Year Indices Means Treatment
1 2015 Shannon 3.062402 Drought
2 2015 Shannon 3.329283 Control
3 2015 Simpson 3.784725 Drought
4 2015 Simpson 4.346054 Control
5 2015 Species Richness 7.833333 Drought
6 2015 Species Richness 8.000000 Control
7 2016 Shannon 3.320377 Drought
8 2016 Shannon 3.496330 Control
9 2016 Simpson 3.877924 Drought
10 2016 Simpson 4.443414 Control
11 2016 Species Richness 9.666667 Drought
12 2016 Species Richness 10.333333 Control
13 2017 Shannon 4.421234 Drought
14 2017 Shannon 4.347877 Control
15 2017 Simpson 7.070072 Drought
16 2017 Simpson 6.340170 Control
17 2017 Species Richness 14.833333 Drought
18 2017 Species Richness 14.833333 Control
19 2018 Shannon 4.055784 Drought
20 2018 Shannon 3.939885 Control
21 2018 Simpson 5.796247 Drought
22 2018 Simpson 5.018261 Control
23 2018 Species Richness 13.500000 Drought
24 2018 Species Richness 14.000000 Control
Using GGplot I am able to create a bar graph with the Year or Indices grouped by not at the same time. Below is grouped by year. Here is what the graph looks like:
ggplot(data,aes(x=Year,y=Means, fill=Treatment),stat="identity",fill=factor(Treatment))+
geom_bar(stat="identity",position="dodge")
Here is the graph grouped by Indices
.
ggplot(data,aes(x=Indices,y=Means, fill=Treatment),stat="identity",fill=factor(Treatment))+
geom_bar(stat="identity",position="dodge")
My question is, how can I create a graph that is grouped by years and indices with different bars representing the treatments similar to my Excel graph? Should I re-enter my data in a different way? I've tried using group_by, but have had no luck.
Thanks!