Adding total count labels to geom_bar(stat = "identity") in ggplot2

189 Views Asked by At

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:

enter image description here

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 in setup_params(): ! stat_bin() must only have an x or y aesthetic. Run rlang::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")

enter image description here

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 in setup_params(): ! stat_count() must only have an x or y aesthetic. Run rlang::last_trace() to see where the error occurred.

I'm about to give up and just add the numbers manually in Powerpoint.

1

There are 1 best solutions below

1
stefan On

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 on y, i.e. it does not work when a variable is mapped on y and will result in an error.

Using some fake example data based on the ggplot2::mpg dataset:

library(ggplot2)

# Make example data
gold <- mpg |>
  dplyr::count(class, trans) |>
  dplyr::rename(Site = class, APIDAE = n)

ggplot(gold, aes(x = Site, y = APIDAE, fill = Site)) +
  geom_bar(stat = "identity") +
  stat_summary(
    aes(label = after_stat(y)),
    fun = "sum", geom = "text", vjust = 0,
    position = position_nudge(y = 1)
  ) +
  # scale_fill_manual(name = "Sites", values = gold_cols) +
  # ylim(0, 50) +
  ylab(label = "Famille apidae") +
  ggtitle("Présence Apidae entre sites")

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():

gold <- gold |>
  dplyr::summarise(APIDAE = sum(APIDAE), .by = Site)

ggplot(gold, aes(x = Site, y = APIDAE, fill = Site)) +
  geom_col() +
  geom_text(aes(label = APIDAE),
    vjust = 0,
    position = position_nudge(y = 1)
  ) +
  # scale_fill_manual(name = "Sites", values = gold_cols) +
  # ylim(0, 50) +
  ylab(label = "Famille apidae") +
  ggtitle("Présence Apidae entre sites")