Home > Mobile >  Issue adding second variable to scatter plot in R
Issue adding second variable to scatter plot in R

Time:10-31

Been set this question for an assignment - but i've never used R before - any help is appreciated. Many thanks.
Question: Produce a scatter plot to compare CO2 emissions from Brazil and Argentina between 1950 and 2019.... I can get it for Brazil but cannot figure out how to add Argentina. I think i have to do something with geom_point and filter?

df%>%
  filter(Country=="Brazil", Year<=2019 & Year>=1950) %>%
  ggplot(aes(x = Year, y = CO2_annual_tonnes))   
    geom_point(na.rm =TRUE, shape=20, size=2, colour="green")   
    labs(x = "Year", y = "CO2Emmissions (tonnes)")

CodePudding user response:

The answer depends on what you're looking to do, but generally adding another dimension to a scatter plot where you already have clear x and y dimensions is done by applying an aesthetic (color, shape, etc) or via faceting.

In both approaches, you actually don't want to filter the data. You use either aesthetics or faceting to "filter" in a way and map the data appropriately based on the country column in the dataset. If your dataset contains more countries than Argentina and Brazil, you will want to filter to only include those, so:

your_filtered_df <- your_df %>%
    dplyr::filter(Country %in% c("Argentina", "Brazil"))

Faceting

Faceting is another way of saying you want to split up your one plot into two separate plots (one for Argentina, one for Brazil). Each plot will have the same aesthetics (look the same), but will have the appropriate "filtered" dataset.

In your case, you can try:

your_filtered_df %>%
ggplot(aes(x = Year, y = CO2_annual_tonnes))  
    geom_point(na.rm =TRUE, shape=20, size=2, colour="green")  
    facet_wrap(~Country)

Aesthetics

Here, you have a lot of options. The idea is that you tell ggplot2 to map the appearance of individual points in the point geom to the value specified in your_filtered_df$Country. You do this by placing one of the aesthetic arguments for geom_point() inside of aes(). If you use shape=, for example it might look like this:

your_filtered_df %>%
    ggplot(aes(x = Year, y = CO2_annual_tonnes))  
        geom_point(aes(shape=Country), na.rm =TRUE, size=2, colour="green")

This should show a plot that has a legend created to and two different shapes for the points that correspond to the country name. It's very important to remember that when you put an aesthetic like shape or color or size inside of aes(), you must not also have it outside. So, this will behave correctly:

geom_point(aes(colour=Country), ...)

But this will not:

geom_point(aes(colour=Country), colour="green", ...)

When one aesthetic is outside, it overrides the one in aes(). The second one will still show all points as green.

Don't Do this... but it works

OP posted a comment that indicated some additional hints from the professor, which was:

We were given the hint in the question "you can embed piped filter functions within geom_point objects"

I believe they are referring to a final... very bad way of generating the points. This method would require you to have two geom_point() objects, and send each one a different filtered dataset. You can do this by accessing the data= argument within each geom_point() object. There are many problems with this approach, including the lack of a legend being generated, but if you simply must do it this way... here it is:

# painful to write this.  it goes against all good practices with ggplot
your_filtered_df %>%
    ggplot(aes(x = Year, y = CO2_annual_tonnes))  
    geom_point(data=your_filtered_df %>% dplyr::filter(Country=="Argentina"),
        color="green", shape=20)  
    geom_point(data=your_filtered_df %>% dplyr::filter(Country=="Brazil"),
        color="red", shape=20)

You should probably see why this is not a good convention. Think about what you would do for representing 50 different countries... the above codes or methods would work, but with this method, you would have 50 individual geom_point() objects in your plot... ugh. Don't make a typo!

  • Related