I'm trying to add the total count to a geom_bar graph where I have the command (stat = "identity") in ggplot2. I'm having issues with the stat = "count" function and the label = after_stat(count) function. I_m running R version 4.3.1 through R Studio.
The base code without any count:
library(ggplot2)
ggplot(gold, aes(x=Site, y=APIDAE, fill=Site, colours))+
geom_bar(stat = "identity")+
scale_fill_manual(name = "Sites", values = gold_cols)+
ylab(label = "Famille apidae")+
ylim(0, 50)+
ggtitle("Présence Apidae entre sites")
Gives me this:

I've tried adding the after_stat(count) function and here is the error message I get:
Error in
geom_text(): ! Problem while computing stat. ℹ Error occurred in the 2nd layer. Caused by error insetup_params(): !stat_bin()must only have an x or y aesthetic. Runrlang::last_trace()to see where the error occurred.
I've also tried this:
ggplot(gold, aes(x=Site, y=APIDAE, fill=Site, colours))+
geom_bar(stat = "identity")+
scale_fill_manual(name = "Sites", values = gold_cols)+
ylab(label = "Famille apidae")+
ylim(0, 50)+
geom_text(
aes(label = APIDAE),
colour = "white", size = 3,
vjust = 1.5, position = position_dodge(.9)
)+
ggtitle("Présence Apidae entre sites")

I don't want 4 values within each bin, I want the sum within the bin.
I tried stat = "count" without much luck:
ggplot(gold, aes(x=Site, y=APIDAE, fill=Site, colours))+
geom_bar(position = "dodge")+
geom_text(
stat = "count",
aes(
label = after_stat(count)
),
position = position_dodge(),
color = "black",
size = 8,
vjust = -0.2
)+
scale_fill_manual(name = "Sites", values = gold_cols)+
ylab(label = "Famille apidae")+
ylim(0, 50)+
ggtitle("Présence Apidae entre sites")
Gives me this error:
Error in
geom_bar(): ! Problem while computing stat. ℹ Error occurred in the 1st layer. Caused by error insetup_params(): !stat_count()must only have an x or y aesthetic. Runrlang::last_trace()to see where the error occurred.
I'm about to give up and just add the numbers manually in Powerpoint.
As I already mentioned in my comment, if you want to compute the sum of a variable use
stat_summary.stat="count"will compute the count aka the number of observations and maps the result ony, i.e. it does not work when a variable is mapped onyand will result in an error.Using some fake example data based on the
ggplot2::mpgdataset:However, perhaps the easiest approach would be to properly aggregate your dataset before passing it to ggplot2. This way you can add the labels without the need of
stat_summary():