Conclusion
By completing this module, you’ve taken your first steps toward data visualization with R. You’ve learned how to use Base R graphics to create some of the most useful types of data visualization: histograms, kernel density plots, boxplots, bar plots, and scatterplots. You also learned how to convert univariate plots into conditional plots and how to add layers to plots to increase the information provided by a single figure.
Self-Test
The following assignment brings together every technique covered in this module, applied to a new dataset: mtcars, a collection of design and performance measurements for 32 cars from a 1974 issue of Motor Trend magazine.
Using the mtcars dataset, complete each of the following tasks.
- Create a histogram of fuel efficiency,
mpg, choosing a reasonable number of bins. Add a proper axis label and title. - Create a kernel density plot of
mpg. Add a proper axis label and title. - Create a boxplot of horsepower,
hp, and use it to check for possible outliers. - Create a bar plot showing the proportion of cars with each number of cylinders,
cyl. - Create a scatterplot of
mpg(y-axis) against weight,wt(x-axis), and add the best fit line from a linear regression ofmpgonwt. - Create a conditional boxplot comparing
mpgbetween automatic and manual transmission cars, using theamvariable. What does the figure suggest about the relationship between transmission type and fuel efficiency?
Next Steps
Base R graphics are a solid starting point, and the kinds of figures we covered in this module are a useful for model diagnostics, data screening, and exploratory data analysis. To create complex, publication-ready figures, however, you will probably want something more. To continue your data visualization journey, we recommend following the Data Visualization with GGPlot module where you will learn how to use the GGPlot visualization suite to create publication-quality figures in R.