How to use Facet_Wrap to create multiple histograms in ggplot2

Hi all,

So I'm doing a Bayesian ecological project regarding Monarch Butterflies in given ecological regions. More specifically, my objective is to build a statistical model for the ecological niche of monarch butterflies and test for local adaptation by ecoregion.

The variables in my data set and of interest are

  1. Y -- number of Monarch Butterfly reports and goes from 0 to some N.
  2. N -- the number of surveys submitted
  3. forest -- the proportion of the region that is forest
  4. grassland -- the proportion of the region that is grassland
  5. cropland -- the proportion of the region that is cropland
  6. temp -- annual average temperature
  7. precip -- annual average precipitation
  8. humanPop -- human population density (number of humans per square mile)
  9. protected -- Indicator of whether the region includes protected lands
  10. ecoregion -- Either “MARINE WEST COAST FOREST”, “MEDITERRANEAN CALIFORNIA”,“NORTH AMERICAN DESERTS” or “NORTHWESTERN FORESTED MOUNTAINS” (this is imported as a character variable, but of course can easily be converted to a factor variable)

My goal is to create a set of histograms that look at for reach ecoregion, the number of Monarch Butterfly reports (Y) on both protected and not-protected parts of each ecoregion. Ideally, there would also be a kernel or density plot over these histograms too but I'm willing to forgo those if need be.

So specifically, I'd either have 8 total histograms or ideally 4 if I can overlay the protected and non-protected parts, but I'm absolutely willing to have 8 if it's cleaner and easier to do.

I think there is a way to do this in ggplot2 using the facet_wrap() or the facet_grid() command I just can't figure out how to get it working.

If it helps, here's what my data looks like:

Thanks!

Something like this?

library(tidyverse)

iris2 <- iris %>% 
  select(-Species) %>% 
  pivot_longer(everything(), names_to = "indicator", values_to = "value")

ggplot(iris2, aes(value)) +
  geom_histogram() +
  facet_wrap(~indicator)

Close! I ended up figuring it out, this is what I used:

ggplot(monarchs, aes(x=Y)) +
facet_grid(ecoregion~protected) +
geom_histogram()