Plot mpg vs other factors
- We see a blank plot as seen below when we run ggplot(data=mpg)
- Mpg has 234 rows and 11 columns.
- The drv variable describes which wheel drive the vehicle is. f= front wheel drive, r= rear wheel drive, 4= 4 wheel drive.
Since the variable Class and drv are both categorical a scatter plot does not make any sense as the points overlap with one another.
- The mpg data frame has 234 rows and 11 variables. The variables are:
- Manufacturer model – Gives model name.
- Displ – Gives engine displacement, in litres
- Year –Gives year of manufacture
- Cyl-Gives number of cylinders
- Trans- Gives type of transmission
- Drv – Gives type of wheel drive where f = front-wheel drive, r = rear wheel drive, 4 = 4wd
- Cty- Gives city miles per gallon
- Hwy –Gives highway miles per gallon
- Fl – Gives fuel type
- Class- Gives “type” of car
- Categorical – manufacturer, model, trans, drv, fl, class
Continuous – displ, cyl, cty, hwy
Categorical variables are type chr, whereas continuous variables are type dbl or int. These are written on the second line of the table.
- The “str” function compactly displays the internal structure of an R It gives the type of the variable namely <int>, <chr>, <dbl>etc followed by the first few observations under this variable. Ideally, only one line for each ‘basic’ structure is displayed.
The ?mpg function on the other hand describes the dataset and what the variable names in the data set means.
- The points are not blue because the color argument was set within aes(), not geom_point(). Therefore when we run the code
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = “blue”))
we get the following graph
- The correct code to make the points appear blue is:
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = “blue”)
The outcome is the following graph: