Combining Plots

Learn how to combine charts and optimize configurations.

Combining plots with built-in functions

We may need multiple geometric objects in one chart to represent different data types and compare or display the same data in diverse ways.

For example, we may need to show the scores of two basketball players in one chart to compare their performances. This allows us to convey our intended messages more effectively to the audience. Also, we tell more with less effort and using fewer resources.

While combining multiple geometric objects in base R, we add layers to the initial function. For example, we can combine bars with lines, lines with points, or areas with lines.

Here is a list of layer functions in R:

  • lines(): This adds a line layer to the chart. The arguments of this function are similar to the plot(type = "l").

  • points(): This adds a scatter plot layer to the chart. It takes the same arguments as scatter plots in R.

  • polygon(): This adds an area layer to the chart.

In the example given below, we will combine two area layers and one line layer with a line plot. Imagine we have some data about sales in the automotive industry. We have data in numbers for total sales and SUV sales. We are asked to create a chart that compares sales in time. Let’s use the polygon() and lines() functions to visualize the data.

Get hands-on with 1200+ tech skills courses.