ggplot2 - having trouble with geom_text with facet_wrap

48 views Asked by At

I created a decent-looking figure in ggplot...

    pollinators_1 <- ggplot(data = pollinator_counts_hybrids, aes(x = pollinator.species, y = n, fill = taxa.visited)) +
      geom_bar(stat = 'identity') +
      facet_wrap(~ site, ncol = 1) +
      theme_classic() +
      labs(title = "Pollinators of Gentian taxa at sites with hybrids present", x = "Pollinator Species", y = "Number of Visits", fill = "Taxa")
    
    pollinators_2 <- pollinators_1 + scale_fill_manual('Taxa', values=c('#a1d99b', '#9ebcda', '#8856a755'))

pollinators_3 <- pollinators_2 + theme(axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here

I would like to have some text in each facet_wrapped plot that is unique to that plot. I took this chunk of code:

dat_text <- data.frame(
  label = c("4 cylinders", "6 cylinders", "8 cylinders"),
  cyl   = c(4, 6, 8)
)
p + geom_text(
  data    = dat_text,
  mapping = aes(x = -Inf, y = -Inf, label = label),
  hjust   = -0.1,
  vjust   = -1
)

from a past StackOverflow question: Annotating text on individual facet in ggplot2 and made it to my own code:

text_pollinator_A <- data.frame(label = c("n = 11", "n = 15", "n = 10", "n = 10"),
                                site = c("BS", "LHF", "SHP", "SPN"))

pollinators_4 <- pollinators_3 + geom_text(data = text_pollinator_A, mapping = aes(x = -Inf, y = -Inf, label = label),
                                           hjust = -0.1,
                                           vjust = -1)

But I am receiving the error:

> pollinators_4
Error in `geom_text()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 2nd layer.
Caused by error:
! object 'taxa.visited' not found
Run `rlang::last_trace()` to see where the error occurred.

If anyone could help me fix this, that would be great! Thank you!

1

There are 1 answers

0
Nitesh Shriwash On BEST ANSWER

You want the fill to only work on the bar, not the text. So remove aes(fill) from the global ggplot() call and move it to the geom_bar() layer. Here is the corrected code:

pollinators_1 <- ggplot(data = pollinator_counts_hybrids, 
                          aes(x = pollinator.species, y = n)) +
                 geom_bar(stat = 'identity',aes(fill=taxa.visited)) +
                 facet_wrap(~ site, ncol = 1) +
                 theme_classic() +
                 labs(title = "Pollinators of Gentian taxa at sites with hybrids present",
                          x = "Pollinator Species", y = "Number of Visits", 
                         fill = "Taxa")

pollinators_2 <- pollinators_1 + scale_fill_manual('Taxa', 
                                            values=c('#a1d99b', '#9ebcda', '#8856a755'))

pollinators_3 <- pollinators_2 + theme(axis.text.x = element_text(angle = 45, hjust = 1))

text_pollinator_A <- data.frame(label = c("n = 11", "n = 15", "n = 10", "n = 10"),
                                 site = c("BS", "LHF", "SHP", "SPN"))

pollinators_4 <- pollinators_3 + geom_text(data = text_pollinator_A, 
                                       mapping = aes(x = -Inf, y = -Inf, label = label),
                                       hjust = -0.1,
                                       vjust = -1)