0

I am trying to make a plot of GDP vs CO2 emissions globally. I have found that I have two countries that have data that is a lot larger than the rest of the data so I am trying to separate it with facet_wrap so I have one graph of the two outlier countries and one graph with the rest of the data.

My code thus far is

ggplot(CO2_GDP, aes(x= GDP, y=value)) +
  geom_point(size=1)+
  labs(title = "GDP and CO2 Emissions", y= "CO2 Emissions in Tons", x= "GDP in Billions of USD") +
  facet_wrap(~country_name==c("China", "United States")) 

This gives me one graph with all of the countries including China and the United States and another graph of just China and United States. I need to find a way to remove China and United States from the first graph but have just that data on the second graph.

enter image description hereI thought by adding the comma between China and United States in the last row would remove them from the first graph and just show it on the second but thats not the case as you can see in this image the data on the "True" graph is still on the false graph and its not supposed to be.

Phil
  • 7,287
  • 3
  • 36
  • 66
Alix
  • 1
  • 4
    Try `facet_wrap(~country_name %in% c("China", "United States")) ` – Allan Cameron Nov 02 '22 at 19:57
  • 6
    Two things: (1) your use of `==` is wrong, see https://stackoverflow.com/q/15358006/3358272 and https://stackoverflow.com/q/42637099/3358272. (2) `facet_wrap` does not always filter the data the way we want, I suggest you add an indicator variable in your data that makes this demarcation before you even get to `ggplot`. Perhaps: `transform(CO2_GDP, us_china = country_name %in% c("China", "United States")) |> ggplot(...) ... + facet_wrap(~ us_china)`. – r2evans Nov 02 '22 at 19:59

0 Answers0