adding filters to gg plot

hi im doing coursera google data analytic course and i had a question. how would i add a filter to this existing code?

ggplot(data = penguins) + geom_point(mapping = aes(x = bill_length_mm, y = bill_depth_mm))

i would like to add a filter of the bill length to be more or equal to 45

thanks in advanced

Hi @Josh_Rivera , you could try something like that:

names(penguins) # For check the correct columns names 
penguins |>  filter(bill_length_mm  >=45)  |>  
ggplot() +
 geom_point(mapping = aes(x = bill_length_mm, y = bill_depth_mm))
1 Like

thank you so much for your reply. this may be a dumb question but do you know why that chunk of code comes first? is it just the way r reads filters?

The filtering is placed first because @M_AcostaCH is using the pipe |>. The pipe sends the thing on its left as the first argument of the function on its right. That leads to code you can read from left to right. You can get the same plot with

ggplot(data = filter(penguins, bill_length_mm  >=45)) +
 geom_point(mapping = aes(x = bill_length_mm, y = bill_depth_mm))

To understand that, you read from the inside out; the penguins data set is nested inside two functions, filter() and ggplot().
Using the pipe is easier to read, I think, but it's just a matter of preference.

Remember that it is not a bad thing to ask and there is no such thing as a dumb question, there is always something new to learn, even the most expert in any subject has something to learn.

In this example, the pipe |> symbols can be understood as follows:

With the script what I am trying to say is, take the database penguins then apply a filter a in the bill_length_mm columns where you select the values greater than or equal to 45 and then make the graph.

thank you so much for the explanation and support best of luck!!

This topic was automatically closed 21 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.