rggplot2

Bar graph trouble, error message is stumping me


I'm trying to make a barograph that looks like this BArgraphs

I'm running into an error message with my code:

Error in +.gg: ! Cannot add objects together. ℹ Did you forget to add this object to a object? Run rlang::last_trace() to see where the error occurred.

with my code which is:

library(ggplot2)

data<- data.frame(
  Year = c('2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024'),
  Class = c('Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta',"Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae"),
  Avg = c(0.733333,0,0,0,0,0,0,0,0, 17.8, 13.8, 35.2, 60.66667, 25, 81.53333, 82.26667, 38.2, 33.73333,0.2,0, 0, 0 , 0.2, 0,0.2, 0 , 0.2,1.3,0.6, 0.7,0,0,0,0,0,0,1.75,0.8,0.4,0,0.2 ,0,0,0,0.2,4.6, 6.066667  , 1.9, 0.666667, 1.3 , 0.4, 0.8, 1.533333 , 1.1,2.6 , 0.6,0, 0 ,0,0 ,0,0.2 ,0.2,11.9,3.133333, 4.933333 ,31.55556 ,24.13333, 60.2 , 62.8 ,33 ,4.7))
data$Class <- factor(data$Class, levels = c("Chironomidae","Isopoda","Amphipoda","Bivalvia","Gastropda","Hirudinea","Oligochaeta",
  "Platyhelminthes"))
p1 <- ggplot(data, aes(Avg, Year)) +
  geom_col(aes(fill = Class), width = 0.7, color = "grey30") +
  scale_fill_manual(values = c(
    "Platyhelminthes" = "#75150C","Oligochaeta"="#EF8634","Hirudinea"="#C0BE3D","Gastropda"= "#387D21","Bivalvia"="#258D93", "Amphipoda"="#5589D3",
    "Isopoda" = "#5549D1", "Chironomidae" = "#83350C"
  )) 
  scale_x_discrete(expand = expansion(add = c(1, 1))) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05)), limits = c(0, 110),
                     breaks = (0:5) * 20) +
  theme_classic(18) +
  theme(
    axis.line = element_line(color = "grey50"),
    axis.text.y = element_text(margin = margin(r = 10)),
    axis.text.x = element_text(margin = margin(t = 10)),
    axis.ticks.y = element_line(color = "grey50"),
    axis.ticks.x = element_line(color = "grey50"),
    axis.ticks.length = unit(7, "pt"),
    axis.title.y = element_text(margin = margin(r = 15)),
    legend.position = "inside",
    legend.position.inside = c(2, 0.7),
    legend.key.width = unit(30, "pt"),
    legend.key.height = unit(15, "pt"),
    legend.key.spacing.y = unit(5, "pt")
  ) +
  guides(fill = guide_legend(reverse = TRUE)) +
  labs(
    x = '',
    y = "Years",
    fill = NULL
  )

  data2<- data.frame(
    Year = c('2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024'),
    Class = c('Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta',"Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae"),
    Avg = c(0.6,0,0.2,0.333333 , 0.4 , 0 ,0.4, 0, 2.833333,47.06667 ,40.86667,142.3333 , 149.1111, 40.06667 ,94.66667 ,91.66667,38.2 , 82.4,0,0,0,0,0,0,0,0,0,1.866667, 0.6 ,0.2 ,0, 0.2, 0, 0.4, 0 ,0.2,2.2 ,2.9 ,8.133333,1.333333 , 2.1 ,1,2 , 2.4,1.766667,13.96667, 26.33333, 18.1 , 6.222222 , 5.2 ,6, 7.9,4.466667 ,16.9,8.266667 ,3.4, 5.1 ,1.833333, 1.7 , 0.2,1.566667 ,2.4 ,2.93334,2.5,1.4 ,2.933333,3.944444,3.233333 ,3.866667 ,3.733333 ,3.66666 ,2.266667))

data2$Class <- factor(data2$Class, levels = c(
  "Chironomidae","Isopoda","Amphipoda","Bivalvia","Gastropda","Hirudinea","Oligochaeta",
  "Platyhelminthes"))
p2 <- ggplot(data2, aes(Avg, Year)) +
  geom_col(aes(fill = Class), width = 0.7, color = "grey30") +
  scale_fill_manual(values = c(
    "Platyhelminthes" = "#75150C","Oligochaeta"="#EF8634","Hirudinea"="#C0BE3D","Gastropda"= "#387D21","Bivalvia"="#258D93", "Amphipoda"="#5589D3",
    "Isopoda" = "#5549D1", "Chironomidae" = "#83350C"
  )) +
  scale_x_discrete(expand = expansion(add = c(1, 1))) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.06)),limits = c(0, 110),
                     breaks = (0:5) * 20) +
  theme_classic(18) +
  theme(
    axis.line = element_line(color = "grey50"),
    axis.text.y = element_text(margin = margin(r = 10)),
    axis.text.x = element_text(margin = margin(t = 10)),
    axis.ticks.y = element_line(color = "grey50"),
    axis.ticks.x = element_line(color = "grey50"),
    axis.ticks.length = unit(7, "pt"),
    axis.title.y = element_text(margin = margin(r = 15)),
    legend.position = "right",
    legend.key.width = unit(30, "pt"),
    legend.key.height = unit(15, "pt"),
    legend.key.spacing.y = unit(5, "pt")
  ) +
  guides(fill = guide_legend(reverse = TRUE)) +
  labs(
    x = 'Average / Ponar',
    y = "Years",
    fill = NULL
  )
p <- cowplot::plot_grid(p1, p2, nrow = 1, rel_widths = c(1, 1.5))
png(width = 14, height = 6, units = "in", res = 300); p; dev.off()

I'd greatly appreciate it if someone could help me figure out what I've done wrong.


Solution

  • I see two errors in your code. You are missing a + sign after the first scale_fill_manual. That was causing the error. I also used scale_y_discrete() and scale_x_continuous() to match the data. Try this:

    library(ggplot2)
    
    data<- data.frame(
      Year = c('2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024'),
      Class = c('Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta',"Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae"),
      Avg = c(0.733333,0,0,0,0,0,0,0,0, 17.8, 13.8, 35.2, 60.66667, 25, 81.53333, 82.26667, 38.2, 33.73333,0.2,0, 0, 0 , 0.2, 0,0.2, 0 , 0.2,1.3,0.6, 0.7,0,0,0,0,0,0,1.75,0.8,0.4,0,0.2 ,0,0,0,0.2,4.6, 6.066667  , 1.9, 0.666667, 1.3 , 0.4, 0.8, 1.533333 , 1.1,2.6 , 0.6,0, 0 ,0,0 ,0,0.2 ,0.2,11.9,3.133333, 4.933333 ,31.55556 ,24.13333, 60.2 , 62.8 ,33 ,4.7))
    data$Class <- factor(data$Class, levels = c("Chironomidae","Isopoda","Amphipoda","Bivalvia","Gastropda","Hirudinea","Oligochaeta",
                                                "Platyhelminthes"))
    p1 <- ggplot(data, aes(Avg, Year)) +
      geom_col(aes(fill = Class), width = 0.7, color = "grey30") +
      scale_fill_manual(values = c(
        "Platyhelminthes" = "#75150C","Oligochaeta"="#EF8634","Hirudinea"="#C0BE3D","Gastropda"= "#387D21","Bivalvia"="#258D93", "Amphipoda"="#5589D3",
        "Isopoda" = "#5549D1", "Chironomidae" = "#83350C"
      )) + 
      scale_y_discrete(expand = expansion(add = c(1, 1))) +
      scale_x_continuous(expand = expansion(mult = c(0, 0.05)), limits = c(0, 110),
                         breaks = (0:5) * 20) +
      theme_classic(18) +
      theme(
        axis.line = element_line(color = "grey50"),
        axis.text.y = element_text(margin = margin(r = 10)),
        axis.text.x = element_text(margin = margin(t = 10)),
        axis.ticks.y = element_line(color = "grey50"),
        axis.ticks.x = element_line(color = "grey50"),
        axis.ticks.length = unit(7, "pt"),
        axis.title.y = element_text(margin = margin(r = 15)),
        legend.position = "inside",
        legend.position.inside = c(2, 0.7),
        legend.key.width = unit(30, "pt"),
        legend.key.height = unit(15, "pt"),
        legend.key.spacing.y = unit(5, "pt")
      ) +
      guides(fill = guide_legend(reverse = TRUE)) +
      labs(
        x = '',
        y = "Years",
        fill = NULL
      )
    
    data2<- data.frame(
      Year = c('2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024'),
      Class = c('Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta',"Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae"),
      Avg = c(0.6,0,0.2,0.333333 , 0.4 , 0 ,0.4, 0, 2.833333,47.06667 ,40.86667,142.3333 , 149.1111, 40.06667 ,94.66667 ,91.66667,38.2 , 82.4,0,0,0,0,0,0,0,0,0,1.866667, 0.6 ,0.2 ,0, 0.2, 0, 0.4, 0 ,0.2,2.2 ,2.9 ,8.133333,1.333333 , 2.1 ,1,2 , 2.4,1.766667,13.96667, 26.33333, 18.1 , 6.222222 , 5.2 ,6, 7.9,4.466667 ,16.9,8.266667 ,3.4, 5.1 ,1.833333, 1.7 , 0.2,1.566667 ,2.4 ,2.93334,2.5,1.4 ,2.933333,3.944444,3.233333 ,3.866667 ,3.733333 ,3.66666 ,2.266667))
    
    data2$Class <- factor(data2$Class, levels = c(
      "Chironomidae","Isopoda","Amphipoda","Bivalvia","Gastropda","Hirudinea","Oligochaeta",
      "Platyhelminthes"))
    p2 <- ggplot(data2, aes(Avg, Year)) +
      geom_col(aes(fill = Class), width = 0.7, color = "grey30") +
      scale_fill_manual(values = c(
        "Platyhelminthes" = "#75150C","Oligochaeta"="#EF8634","Hirudinea"="#C0BE3D","Gastropda"= "#387D21","Bivalvia"="#258D93", "Amphipoda"="#5589D3",
        "Isopoda" = "#5549D1", "Chironomidae" = "#83350C"
      )) +
      scale_y_discrete(expand = expansion(add = c(1, 1))) +
      scale_x_continuous(expand = expansion(mult = c(0, 0.06)),limits = c(0, 110),
                         breaks = (0:5) * 20) +
      theme_classic(18) +
      theme(
        axis.line = element_line(color = "grey50"),
        axis.text.y = element_text(margin = margin(r = 10)),
        axis.text.x = element_text(margin = margin(t = 10)),
        axis.ticks.y = element_line(color = "grey50"),
        axis.ticks.x = element_line(color = "grey50"),
        axis.ticks.length = unit(7, "pt"),
        axis.title.y = element_text(margin = margin(r = 15)),
        legend.position = "right",
        legend.key.width = unit(30, "pt"),
        legend.key.height = unit(15, "pt"),
        legend.key.spacing.y = unit(5, "pt")
      ) +
      guides(fill = guide_legend(reverse = TRUE)) +
      labs(
        x = 'Average / Ponar',
        y = "Years",
        fill = NULL
      )
    p <- cowplot::plot_grid(p1, p2, ncol = 1, rel_widths = c(1, 1.5))
    p