0

I'm using geom_text from ggplot to annotate each of my plots in a facet_wrap. My code is shown below. num_zeros_text is just a dataframe with a single column, label, with a number in each row. I'm trying to follow the answer posted by Kamil Slowikowski in this post. For some reason, my labels are ending up "overlapping" each other (see picture below), where essentially all numbers from the dataframe num_zeros_text are being displayed on each plot (I believe). I can't figure out why.

  ggplot(normalized_counts_subsampled, aes(value)) +
    geom_histogram() +
    facet_wrap(~bins, 100) +
    geom_text(data = num_zeros_text,
              aes(x = 0.75, y=50, label=label))

enter image description here

An Ignorant Wanderer
  • 1,322
  • 1
  • 10
  • 23
  • 1
    If you want great answers quickly, it's best to make your question reproducible. This includes sample data like the output from `dput()`. Check it out: [making R reproducible questions](https://stackoverflow.com/q/5963269). What do you have in `num_zeros_test$label`? Is it one label for each graph? Is it a label for some graphs? – Kat Aug 19 '22 at 04:44

1 Answers1

0

The issue is that you labels dataset does not contain the facetting variable. Hence, how should ggplot2 know that you want only one number per facet and in which order? As a consequence all numbers are plotted in each facet.

Here is a minimal reproducible example of your issue based on iris:

library(ggplot2)

num_zeros_text <- data.frame(
  label = c(1, 2, 3)
)

ggplot(iris, aes(Sepal.Length)) +
  geom_histogram() +
  facet_wrap(~Species) +
  geom_text(data = num_zeros_text,
            aes(x = 0.75, y=50, label=label))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

To fix your issue add a second column with the facetting variable to your labels dataset, i.e. for my reprex a Species column:

num_zeros_text$Species <- unique(iris$Species)

ggplot(iris, aes(Sepal.Length)) +
  geom_histogram() +
  facet_wrap(~Species) +
  geom_text(data = num_zeros_text,
            aes(x = 0.75, y=50, label=label))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

stefan
  • 90,330
  • 6
  • 25
  • 51