Creating a stacked barplot or histogram

Hey guys, I'm trying to do a stacked barplot / histogram which has the observation dates on the x axis and the total of sighted pods (of whales) on the y axis. The amount of pods inside the experimental area should be part of the total bars, so that you can directly see how many pods per day were inside the experiment. Ideally even with numbers on top. The code I got so far:

library(ggplot2)
PodsBoth <- read.table("PodsBoth.txt", header = TRUE)
colnames(PodsBoth) <- c("Date", "Total", "Inside")
ggplot(PodsBoth, aes(x = Date, y=Total, color = Inside, fill=Inside)) + 
  geom_histogram(position = "stack", stat = "identity", color="white")

What I get out of it:

Somehow the color coding is like that and I really don't know why ( R beginner, sorry :smiley: )
Maybe you guys have an idea or solution.

Date		Pods	Inside
2021-06-23	3	0
2021-06-24	11	0
2021-06-25	5	1
2021-06-26	14	1
2021-06-28	6	2
2021-06-29	1	0
2021-07-03	2	0
2021-07-04	6	1
2021-07-08	5	0
2021-07-09	6	0
2021-07-11	16	2
2021-07-12	4	0
2021-07-13	13	3
2021-07-14	6	0
2021-07-15	19	3
2021-07-16	23	1
2021-07-19	10	2
2021-07-21	13	4
2021-07-22	8	2
2021-07-23	27	9
2021-07-26	14	6
2021-07-27	8	7
2021-08-04	45	20
2021-08-05	20	12
2021-08-06	2	1
2021-08-07	19	14
2021-08-10	4	2
2021-08-11	6	6
2021-08-12	20	13
2021-08-13	0	0
2021-08-21	34	16
2021-08-22	9	9
2021-08-23	16	15

Thats what my data frame looks like. Sorry I'm not that good in reprex etc. Hope you can work with what I gave you. Thanks in advance. :slight_smile:

Is this what you mean? I think you want a column plot instead of a histogram (since you are using stat = identity)

library(tidyverse)

PodsBoth <- data.frame(
        Date = c("2021-06-23","2021-06-24","2021-06-25",
                 "2021-06-26","2021-06-28","2021-06-29","2021-07-03",
                 "2021-07-04","2021-07-08","2021-07-09","2021-07-11","2021-07-12",
                 "2021-07-13","2021-07-14","2021-07-15","2021-07-16",
                 "2021-07-19","2021-07-21","2021-07-22","2021-07-23","2021-07-26",
                 "2021-07-27","2021-08-04","2021-08-05","2021-08-06",
                 "2021-08-07","2021-08-10","2021-08-11","2021-08-12","2021-08-13",
                 "2021-08-21","2021-08-22","2021-08-23"),
       Pods = c(3,11,5,14,6,1,2,6,5,6,16,4,13,
                 6,19,23,10,13,8,27,14,8,45,20,2,19,4,6,20,0,34,
                 9,16),
      Inside = c(0,0,1,1,2,0,0,1,0,0,2,0,3,0,3,
                 1,2,4,2,9,6,7,20,12,1,14,2,6,13,0,16,9,15)
)

PodsBoth %>%
    mutate(Pods = Pods - Inside) %>% 
    pivot_longer(cols = c("Pods", "Inside"), names_to = "type", values_to = "Count") %>% 
    ggplot(aes(x = Date, y=Count, fill = type)) + 
    geom_col(color="white")

Created on 2021-08-30 by the reprex package (v2.0.1)

1 Like

Wow, amazing. Thanks so much.
Do you also know how to add the actual numbers on top of each bar? Ideally with the total number of pods for one day and the pods inside the experiment in brackets, e.g. for the highest day "45 (20)" to say that 20 out of 45 total pods were inside the experiment. Not sure tho if it fits in the graphics.

You can use geom_text()

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.