0

I have created a scatter plot for CO2 emissions for Argentina and Brazil. How would i make them different colours? at the moment it's all green. Thanks Lois

df%>%
  dplyr:: filter(Country %in% c("Argentina", "Brazil")) %>%   
  filter(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)")
MrFlick
  • 195,160
  • 17
  • 277
  • 295
LF123
  • 23
  • 3
  • 3
    OP, you seem to be asking a lot here today on this question... what type of reference material or instruction was provided in your class? [The answer I provided you here](https://stackoverflow.com/a/69772304/9664796) on your previous question should get you there. You need to put color into the `aes()` function. It can either go inside `ggplot(aes(...), ...)` or `geom_point(aes(...),...)`. Map color to `Country` and you should be all good. If you need to specify colors, do that with a `scale_color_*` function - just chck that documentation. – chemdork123 Oct 29 '21 at 19:18

1 Answers1

1

Try this:

library(ggplot2)
library(dplyr)
df%>% 
  dplyr:: filter(Country %in% c("Argentina", "Brazil")) %>%    
  filter(Year<=2019 & Year>=1950) %>%    
  ggplot(aes(x = Year, y = CO2_annual_tonnes, colour=factor(country))) + 
  geom_point(na.rm =TRUE, shape=20, size=2) + 
  labs (x = "Year", y = "CO2Emmissions (tonnes)")

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Thank you TarJae - yes I know I'm asking a lot - this the the very last step of the assignment. The only information we were given was for a single plot and not how to colour 2 plots independantly. I will give it another go. Thanks for your help – LF123 Oct 29 '21 at 19:51
  • @LoisFleming. If this is an exercise than be careful. The trick is if you use `fill` in this case it won't work because of shape=20. If you use shape=21 then fill will work. Just a nostice. And please consider to upvote and accept if the answer solved your problem! – TarJae Oct 29 '21 at 19:55
  • 1
    Ok - they're different colours now :) - i think i've done it technically the "wrong" way as you have mentioned with the geom_point but if it's what they want there we go. Thanks for showing m the right way as well for future. I only saw my first line of code ever on tuesday and have really struggled with it so thanks again. – LF123 Oct 29 '21 at 20:03