Book Image

Applied Data Visualization with R and ggplot2

By : Dr. Tania Moulik
Book Image

Applied Data Visualization with R and ggplot2

By: Dr. Tania Moulik

Overview of this book

Applied Data Visualization with R and ggplot2 introduces you to the world of data visualization by taking you through the basic features of ggplot2. To start with, you’ll learn how to set up the R environment, followed by getting insights into the grammar of graphics and geometric objects before you explore the plotting techniques. You’ll discover what layers, scales, coordinates, and themes are, and study how you can use them to transform your data into aesthetical graphs. Once you’ve grasped the basics, you’ll move on to studying simple plots such as histograms and advanced plots such as superimposing and density plots. You’ll also get to grips with plotting trends, correlations, and statistical summaries. By the end of this book, you’ll have created data visualizations that will impress your clients.
Table of Contents (10 chapters)

Facets


In data visualization, we sometimes have the need to compare different groups, looking at data alongside each other. One method for doing this is creating a subplot for each group. These kinds of plots are known as Trellis displays. In ggplot2, they're called facets. Facets divide the data by some discrete or categorical variable and display the same type of graph for each data subset.

Let's look at electricity consumption versus GDP for different countries, which we calculated in the previous activity.

We don't know which country has the highest GDP or electricity consumption. Let's split the data now.

Using Facets to Split Data

In this section, we'll plot subsets of data as separate subplots. Let's begin by implementing the following steps:

  1. Use the gapminder.csv dataset.
  2. Make a scatter plot of Electricity_consumption_per_capita versus gdp_per_capita:
p <- ggplot (df, aes (x=gdp_per_capita, y=Electricity_consumption_per_capita)) + geom_point ()
  1. Use facet_grid() to specify the variables...