Plot types
In Chapter 3, when studying basic data visualization, we used the mtcars toy dataset. In this chapter, we will go back to it, now being able to explore the capabilities of the ggplot2 library, as well as compare it to the base R plots previously created.
To load the dataset into an RStudio session and code along with this book, use the data("mtcars")
code. To make the code more generic and transferable to other data frames, I will call the dataset df
.
Histograms
Histograms are created using the geom_histogram()
function. As usual, the same questions are applicable here to write the code:
- What is the dataset? df.
- What is the kind of graphic? Histogram.
- What goes on x, and what is the color, fill color, and number of bins? Miles per gallon, with 20 bins.
- What is the title of the plot? Histogram of Miles per Gallon:
# What is the dataset to be used? ggplot(df) + # What kind of graphic? geom_histogram( # What goes on...