Unstacking GGplot Bar Graph

I am trying to create a bar graph in ggplot for multiple variables, but I can only manage to create a stacked bar graph. However, I want the variables side by side with their own bars.

My dataset:

Hm# A tibble: 30 x 5
   Year  MackerelMean Mackerelse SardineMean Sardinese
   <fct>        <dbl>      <dbl>       <dbl>     <dbl>
 1 1988         0.291     0.0702       0.285    0.0848
 2 1989         0.403     0.0974       0.296    0.105 
 3 1990         0.249     0.0586       0.400    0.106 
 4 1991         0.723     0.108        0.405    0.0914
 5 1992         0.627     0.106        0.743    0.142 
 6 1993         0.370     0.0696       0.214    0.0673
 7 1994         0.641     0.108        0.490    0.0966
 8 1995         0.933     0.121        0.532    0.106 
 9 1996         0.809     0.136        0.898    0.179 
10 1997         0.565     0.0966       0.195    0.0460

This is the code I've tried that creates a stacked bar graph:

ggplot(Hm) +
  geom_bar(aes(x=Year, y=MackerelMean), stat="identity", 
           fill="forestgreen", alpha=0.7) +
  geom_bar(aes(x=Year, y=SardineMean), stat="identity", 
           fill = "grey2") +
  scale_x_discrete(breaks=seq(1988, 2017, by = 3)) 

But I want MackerelMean and SardineMean to have their own independent bars relative to Year. Also, I want to include geom_errorbars based on the standard error values (Mackerelse and Sardinese) but I'm not sure how to incorporate separate geom error bars.

So that it's
geom_errorbar( aes(x=Year, ymin=MackerelMean-Mackerelse, ymax=MackerelMean+Mackerelse)

and

geom_errorbar( aes(x=Year, ymin=SardineMean-Sardinese, ymax=SardineMean+Sardinese)

Thanks.

The plotting is easier if you reshape the data. It might be possible to reshape the data in a single step but I used two steps.
Also, it would be easier for others to work with your data if you posted the output of

dput(Hm)

so we can copy and paste directly into the code.

DF <- read.csv("~/R/Play/Dummy.csv")
library(ggplot2)
library(tidyr)
tmp <- pivot_longer(data = DF, cols = -Year, names_to = c("Fish", "Stat"), 
             names_pattern = "(.+)(se|Mean)", values_to = "Value")
DFnew <- pivot_wider(data = tmp, names_from = Stat, values_from = Value)

DFnew
#> # A tibble: 20 x 4
#>     Year Fish      Mean     se
#>    <int> <chr>    <dbl>  <dbl>
#>  1  1988 Mackerel 0.291 0.0702
#>  2  1988 Sardine  0.285 0.0848
#>  3  1989 Mackerel 0.403 0.0974
#>  4  1989 Sardine  0.296 0.105 
#>  5  1990 Mackerel 0.249 0.0586
#>  6  1990 Sardine  0.4   0.106 
#>  7  1991 Mackerel 0.723 0.108 
#>  8  1991 Sardine  0.405 0.0914
#>  9  1992 Mackerel 0.627 0.106 
#> 10  1992 Sardine  0.743 0.142 
#> 11  1993 Mackerel 0.37  0.0696
#> 12  1993 Sardine  0.214 0.0673
#> 13  1994 Mackerel 0.641 0.108 
#> 14  1994 Sardine  0.49  0.0966
#> 15  1995 Mackerel 0.933 0.121 
#> 16  1995 Sardine  0.532 0.106 
#> 17  1996 Mackerel 0.809 0.136 
#> 18  1996 Sardine  0.898 0.179 
#> 19  1997 Mackerel 0.565 0.0966
#> 20  1997 Sardine  0.195 0.046
ggplot(DFnew, aes(factor(Year), y = Mean, fill = Fish)) +
  geom_col(position = "dodge") +
  geom_errorbar(aes(ymin = Mean - se, ymax = Mean + se), position = "dodge")

Created on 2020-08-21 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.