0

I need to create a scatterplot of count vs. depth of 12 species using ggplot.

This is what I have so far:

library(ggplot2)
ggplot(data = ReefFish, mapping = aes(count, depth))

However, how do I use geom_point(), geom_smooth(), and facet_wrap() to include a smoother as well as include just the 12 species I want from the data (ReefFish)? Since I believe what I have right now includes all species from the data.

Here is an example of part of my data:

enter image description here

4
  • 1
    filter() your data frame to include only the 12 species you want before calling ggplot(). As for how to use geom_xxxx(), that's impossible to say without sight of your data. Commented Sep 15, 2021 at 12:58
  • Error in ggplot(data = ReefFish, mapping = aes(count, depth)) : object 'ReefFish' not found. Where can we find this data set? Commented Sep 15, 2021 at 12:58
  • It is a huge dataset, what is the best way for me to share it for you? I edited the question and included an example of a piece of the data. Commented Sep 15, 2021 at 13:01
  • How do I include all 12 species in the filter() function? Would it be something like: filter("species 1" "species 2" etc.)? Commented Sep 15, 2021 at 13:04

1 Answer 1

1

Since I don't have access to the ReefFish data set, here's an example using the built-in mpg data set about cars. To make it work with your data set, just edit this code to replace manufacturers with species.

Filter the data

First we filter the data so that it only includes the species/manufacturers we're interested in.

# load our packages
library(ggplot2)
library(magrittr)
library(dplyr)

# set up a character vector of the manufacturers we're interested in
manufacturers <- c("audi", "nissan", "toyota")

# filter our data set to only include the manufacturers we care about
mpg_filtered <- mpg %>%
  filter(manufacturer %in% manufacturers)

Plot the data

Now we plot. Your code was just about there! You just needed to add the plot elements, you wanted, like so:

mpg_filtered %>%
  ggplot(mapping = aes(x = cty, 
                       y = hwy)) +
  geom_point() +
  geom_smooth() +
  facet_wrap(~manufacturer)

Hope that helps, and let me know if you have any issues.

Sign up to request clarification or add additional context in comments.

10 Comments

This is very helpful! However, I'm a little confused on the mpg_filtered part. This is what I have so far:
species <- c("Vanderbilt's chromis", "Agile chromis", "Brown surgeonfish", "Spotted surgeonfish", "Yellow tang", "Common bluestripe snapper", "Arc-eye hawkfish", "Hawaiian bicolor chromis", "Whitebar surgeonfish", "Saddle wrasse", "Striated wrasse", "Pebbled butterflyfish") mpg_filtered <- mpg %>% filter(species %in% ReefFish) mpg_filtered %>% ggplot(mapping = aes(count, depth)) + geom_point() + geom_smooth() + facet_wrap(~species)
In "manufacturer %in% manufacturers", is "manufacturers" the dataset?
Or is "mpg" the dataset?
Hi Alison, my bad for skipping a few steps. Is your data public? It's hard to give answers without it, but I assume there is a column in ReefFish with the species names, and I'll call it SPECIES. So here species is the vector we set up of interesting fish, and SPECIES (all caps) is the name of the column in ReefFish. You'll need to edit the code to reflect the actual column name. Then you could try: fish_filtered <- ReefFish %>% filter(SPECIES %in% species), and then fish_filtered %>% ggplot(mapping = aes(count, depth)) + geom_point() + geom_smooth() + facet_wrap(~SPECIES)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.