Sum of barplots ggplott2

Hi,

i recently started using Rstudios, because I need it for my research.
I want to show my data using Bar plots, but the bars won't sum. The data consists of catched flies (big or small) using different colored containers in different forests.

my code is the following:

Data_vliegen%>%
ggplot(aes(x = Kleur, fill = as.character(Grote_vliegen))) +
geom_bar(alpha = 0.7) +
theme_bw(base_size = 12)+
labs(fill = "Kleur") +
ylab("Aantal")

But this plot does not consider that multiple flies can be caught within 1 sample (it won't sum). How can i write it that it will take the sum of all the flies (about 3000 in each color)? I think I have to change my dataset from a wide to a long conformation.

Here is a picture of the created plot:

Eventually I have to consider the different forests and want to implement this as a subcategorie in each barplot.

Thanks!

translated: (kleur=color / Grote_vliegen= Big_flies/ Kleine_vliegen=small_flies/ aantal=total)

here is the plot I want to create:
image

And a small part of the dataset:

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? We can't reproduce your code from a screenshot, please have a look at this guide, to see how to create a proper reprex:

Is this a good dataset?

flies <- tibble::tribble(
~Ophaling, ~Boomtype, ~Bestand_nummer, ~Val_nummer, ~Kleur, ~Kolom1, ~Grote_vliegen, ~Kleine_vliegen, ~opmerkingen,
1L, "B", 2L, 1L, "B", NA, 2L, 6L, NA,
1L, "B", 2L, 1L, "W", NA, 6L, 3L, NA,
1L, "B", 2L, 1L, "Y", NA, 7L, 6L, NA,
1L, "B", 2L, 2L, "B", NA, 3L, 2L, NA,
1L, "B", 2L, 2L, "W", NA, 8L, 7L, NA,
1L, "B", 2L, 2L, "Y", NA, 5L, 15L, NA,
1L, "B", 3L, 1L, "B", NA, 4L, 14L, NA,
1L, "B", 3L, 1L, "W", NA, 15L, 29L, NA,
1L, "B", 3L, 1L, "Y", NA, 12L, 15L, NA,
1L, "B", 3L, 2L, "B", NA, 34L, 43L, NA,
1L, "B", 3L, 2L, "W", NA, 168L, 47L, NA,
1L, "B", 3L, 2L, "Y", NA, 52L, 32L, NA,
1L, "B", 4L, 1L, "B", NA, 2L, 2L, NA,
1L, "B", 4L, 1L, "W", NA, 17L, 7L, NA,
1L, "B", 4L, 1L, "Y", NA, 46L, 6L, NA,
1L, "B", 4L, 2L, "B", NA, 2L, 5L, NA,
1L, "B", 4L, 2L, "W", NA, 12L, 4L, NA,
1L, "B", 4L, 2L, "Y", NA, 24L, 14L, NA
)
head(flies)

This is not elegant but I think it does what you want.

library(tidyverse)
dat1 <-    flies %>% 
  group_by(Kleur) %>% 
    summarise(grosse = sum( Grote_vliegen)
     
              
 ggplot(dat1, aes(Kleur, grosse, fill = Kleur)) + geom_bar(stat = "identity") +
   theme(legend.position="none")

Is this what you want to do?

library(tidyverse)

# Sample data in a copy/paste friendly format, replace this with your own data frame
flies <- tibble::tribble(
    ~Ophaling, ~Boomtype, ~Bestand_nummer, ~Val_nummer, ~Kleur, ~Kolom1, ~Grote_vliegen, ~Kleine_vliegen, ~opmerkingen,
    1L, "B", 2L, 1L, "B", NA, 2L, 6L, NA,
    1L, "B", 2L, 1L, "W", NA, 6L, 3L, NA,
    1L, "B", 2L, 1L, "Y", NA, 7L, 6L, NA,
    1L, "B", 2L, 2L, "B", NA, 3L, 2L, NA,
    1L, "B", 2L, 2L, "W", NA, 8L, 7L, NA,
    1L, "B", 2L, 2L, "Y", NA, 5L, 15L, NA,
    1L, "B", 3L, 1L, "B", NA, 4L, 14L, NA,
    1L, "B", 3L, 1L, "W", NA, 15L, 29L, NA,
    1L, "B", 3L, 1L, "Y", NA, 12L, 15L, NA,
    1L, "B", 3L, 2L, "B", NA, 34L, 43L, NA,
    1L, "B", 3L, 2L, "W", NA, 168L, 47L, NA,
    1L, "B", 3L, 2L, "Y", NA, 52L, 32L, NA,
    1L, "B", 4L, 1L, "B", NA, 2L, 2L, NA,
    1L, "B", 4L, 1L, "W", NA, 17L, 7L, NA,
    1L, "B", 4L, 1L, "Y", NA, 46L, 6L, NA,
    1L, "B", 4L, 2L, "B", NA, 2L, 5L, NA,
    1L, "B", 4L, 2L, "W", NA, 12L, 4L, NA,
    1L, "B", 4L, 2L, "Y", NA, 24L, 14L, NA
)

# Relevant code
flies %>%
    group_by(Kleur) %>% 
    summarise(Aantal = sum(Grote_vliegen)) %>% 
    ggplot(aes(x = Kleur, y = Aantal, fill = Kleur)) +
    geom_col(alpha = 0.7) +
    theme_bw(base_size = 12)

Created on 2021-12-27 by the reprex package (v2.0.1)

Thank you very much, I managed to create a good ggplot.

This is the plot i wanted:
image

first I changed my dataset from wide to al long dataset and after that i used "weight" in my ggplot line.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.