Can´t understand the difference using: |, c() and %in% in ggplot (3 examples)

Hi I am learning how to use ggplot2 and I find these results a bit confusing .
Example 1:

  filter(country ==  c("Colombia", "Peru", "Chile", "Mexico")) %>%
  ggplot( aes(x = year, y =gdpPercap, color = country)) +
  geom_line()  

I get this graph and it is wrong

Example2 & 3 (both correct):

gapminder %>%
  filter(country %in% c("Colombia", "Peru", "Chile", "Mexico"))%>%
  ggplot( aes(x = year, y =gdpPercap, color = country)) +
  geom_line()

gapminder %>%
  filter(country == "Colombia" | country == "Peru" | country == "Chile" | country == "Mexico")%>%
  ggplot( aes(x = year, y =gdpPercap, color = country)) +
  geom_line()

Don't understand why with example one I don´t get the same result than with examples 2 and 3. Why filter(country == c("Colombia", "Peru", "Chile", "Mexico")) doesn't work? And what it is doing?

In example 1 you're asking whether the country equals a vector. It doesn't. In the 2nd example you're asking if the country is a member of the set of countries, which it is. In the 3rd, you're asking if any of the conditions of the country equaling one particular names is true, and one of them is.

1 Like

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.