September 2018
Beginner
206 pages
4h 27m
English
To make these charts better, we're going to convert the cyl and gear variables in mtcars to factor variables using the following code:
mtcars$cylfactor <- as.factor(mtcars$cyl)mtcars$gearfactor <- as.factor(mtcars$gear)
Use of the factor variables will help the data display properly.
We previously saw how to both automatically change the color of a bar chart (when we made them light blue) and also how to fill a bar chart with another variable. We did this using the following code:
ggplot(mtcars, aes(cyl, fill = gearfactor)) + geom_bar()
The fill indicates the count of each car with a particular type of cylinder and gear. There are a few other ways to display the fill that we can use.
If we want the bars to be next to each other ...
Read now
Unlock full access