ggplot faceted scatterplot - why are all of my plots identical?

Hey everyone,

I'm attempting to produce a faceted grid of scatter plots for species presence and absence (indicated by a 1 for present and a 0 for absent) against coordinates. So far I've been able to produce a faceted scatter plots for 70 sites and 12 species however they all appear identical.

I wonder if I am missing something to make any 0 instances invisible / excluded from the figures? What I would like to see is a scatter plot for each species showing a point for where that species was present.

The script below replicates the issue for 5 sites and still gives the same issue of identical scatter plots.

Thanks for any help in advance!

#creating example dataframe
df <- data.frame(Site = c("S01", "S02", "S03", "S04", "S05"),
                 Easting = c(634571, 635625, 635985, 636208, 638082),
                 Northing = c(6306325, 6306939, 6304130,6301157, 6299408),
                 Species1 = c(1,1,0,0,1),
                 Species2 = c(0,0,1,0,0),
                 Species3 = c(1,0,1,0,1),
                 Species4 = c(0,0,1,1,1))

#pivoting dataframe for ggplot
df.piv <- pivot_longer(df, cols = c(4:7), names_to = "Species", values_to = "presence/absence")

#plotting scatterplots
ggplot(df.piv) +
  geom_point(aes(x=Easting,y=Northing)) +
  facet_wrap(~Species) +
  theme_bw() +
  ylab("Northing")+ xlab("Easting")

Because your pivot table gives identical Easting/Northing values to all species, and because the presence/absence data that distinguishes species is missing in your ggplot code.

Does this give you what you're looking for?

#plotting scatterplots
ggplot(
  df.piv |> 
    # include only species present at each site
    filter(`presence/absence` == 1)
  ) +
  geom_point(aes(x=Easting,y=Northing)) +
  facet_wrap(~Species) +
  theme_bw() +
  ylab("Northing")+ xlab("Easting")

Created on 2024-01-19 with reprex v2.0.2

Yes that works! Thank you for the quick solution I was beginning to go mad googling the wrong words.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.