In your first mini lesson, you are going to be creating a scatterplot using ggplot2, the finest plotting package across multiple languages. Let us start by creating a scatterplot of weight vs. mileage for cars in the mtcars
dataset which ships with R.
We first load ggplot2
and then use the quick plot function to specify the x variable, which is wt
, the y variable which is mpg
, the dataset mtcars
. If you hit the run button, you will see a nice scatterplot of weight vs. mileage.
Now, what if you wanted to enhance this scatterplot by color coding the points based on the number of cylinders the engine has. Well, you can do it easily by just adding the argument color and setting it to the variable cyl. Why don't you try it yourself? If you need a hint, press the button with the bookmark sign on it. This video will pause now so that you can try it out. When you are done, press the play button so that we can resume.