Skip to main content
HomeAbout RLearn R

Graphics with ggplot2 Tutorial

Data visualization is an essential skill for data scientists. It combines statistics and design in meaningful and appropriate ways.
Sep 2020  · 6 min read

Data visualization is an essential skill for data scientists. It combines statistics and design in meaningful and appropriate ways. On the one hand, data visualization is a form of graphical data analysis, emphasizing accurate representation, and data interpretation. On the other hand, data visualization relies on good design choices to make our plots attractive and aid both the understanding and communication of results. On top of that, there is an element of creativity, since data visualization is a form of visual communication at its heart.

data visualization venn diagram

It's important to understand the distinction between exploratory and explanatory visualizations. Exploratory visualizations are easily-generated, data-heavy, and intended for a small specialist audience, such as yourself and your colleagues - their primary purpose is graphical data analysis. Explanatory visualizations are labor-intensive, data-specific, and intended for a broader audience, e.g., in publications or presentations - they are part of the communications process. As a data scientist, it's essential that you can quickly explore data, but you'll also be tasked with explaining your results to stake-holders. Good design begins with thinking about the audience - and sometimes that just means ourselves.

data scientist visualization diagram

Scatter Plot

Below, we have a dataset that contains the average brain and body weights of 62 mammals.

 MASS::mammals
                              body   brain
Arctic fox                   3.385   44.50
Owl monkey                   0.480   15.50
Mountain beaver              1.350    8.10
Cow                        465.000  423.00
Grey wolf                   36.330  119.50
Goat                        27.660  115.00
Roe deer                    14.830   98.20
...
Pig                        192.000  180.00
Echidna                      3.000   25.00
Brazilian tapir            160.000  169.00
Tenrec                       0.900    2.60
Phalanger                    1.620   11.40
Tree shrew                   0.104    2.50
Red fox                      4.235   50.40 

To understand the relationship here, the most obvious first step is to make a scatter plot, like the one shown below:

ggplot(mammals, aes(x = body, y = brain)) +
  geom_point()
scatter plot

Two mammals, the African and the Asian Elephants have both very large brain and body weights, leading to a positive skew on both axes.

Linear Model

Now, if we were to apply a linear model, it would be a poor choice since a few extreme values have a large influence.

ggplot(mammals, aes(x = body, y = brain)) +
  geom_point(alpha = 0.6) +
  stat_smooth(
    method = "lm",
    color = "red",
    se = FALSE
  )
linear model on scatter plot

Applying a log transformation of both variables allows for a better fit.

ggplot(mammals, aes(x = body, y = brain)) +
  geom_point(alpha = 0.6) +
  coord_fixed() +
  scale_x_log10() +
  scale_y_log10() +
  stat_smooth(
    method = "lm",
    color = "#C42126",
    se = FALSE,
    size = 1
  )
log transformation on linear scatter plot

So, although we began with a rough exploratory plot, it informed us about our data and led us to a meaningful result.

Anscombe's Plots

When we imagine a linear model, as presented on this anonymous plot, we imagine that we are describing data that looks something like this.

linear model on anonymous plot

But this same model could be describing a very different set of data, such as a parabolic relationship, which calls for a different model.

parabolic relationship plot

Or data in which an extreme value has a large effect. which becomes clear when the outlier is removed.

removed outlier on linear plot

And sometimes, the model may be describing a relationship where, in fact, there is none at all because some extreme values may be incorrect.

incorrect extreme values on linear plot

If we relied solely on the numerical output without plotting our data, we'd have missed distinct and interesting underlying trends.

trends in linear plots

We can see that data visualization is rooted in statistics and graphical data analysis, but it's also a creative process that involves some amount of trial and error.

Interactive Example

In the following example, you will first Load the ggplot2 package using library(). Then, you will use str() to explore the structure of the mtcars dataset.

Finally, you will visualize the ggplot and try to understand what ggplot does with the data.

You will use the mtcars dataset contains information on 32 cars from a 1973 issue of Motor Trend magazine. This dataset is small, intuitive, and contains a variety of continuous and categorical variables.

# Load the ggplot2 package
library(ggplot2)

# Explore the mtcars data frame with str()
str(mtcars)

# Execute the following command
p <- ggplot(mtcars, aes(cyl, mpg)) +
  geom_point()

When we run the above code, it produces the following result:

data.frame':    32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
gg plot

Try it for yourself.

To learn more about data visualization with ggplot, please see this video from our course Introduction to Data Visualization with ggplot2.

This content is taken from DataCamp’s Introduction to Data Visualization with ggplot2 course by Rick Scavetta.

Topics

Related Courses

Course

Intermediate Data Visualization with ggplot2

4 hr
42.9K
Learn to use facets, coordinate systems and statistics in ggplot2 to create meaningful explanatory plots.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

Data Science in Finance: Unlocking New Potentials in Financial Markets

Discover the role of data science in finance, shaping tomorrow's financial strategies. Gain insights into advanced analytics and investment trends.
 Shawn Plummer's photo

Shawn Plummer

9 min

5 Common Data Science Challenges and Effective Solutions

Emerging technologies are changing the data science world, bringing new data science challenges to businesses. Here are 5 data science challenges and solutions.
DataCamp Team's photo

DataCamp Team

8 min

Navigating R Certifications in 2024: A Comprehensive Guide

Explore DataCamp's R programming certifications with our guide. Learn about Data Scientist and Data Analyst paths, preparation tips, and career advancement.
Matt Crabtree's photo

Matt Crabtree

8 min

A Data Science Roadmap for 2024

Do you want to start or grow in the field of data science? This data science roadmap helps you understand and get started in the data science landscape.
Mark Graus's photo

Mark Graus

10 min

R Markdown Tutorial for Beginners

Learn what R Markdown is, what it's used for, how to install it, what capacities it provides for working with code, text, and plots, what syntax it uses, what output formats it supports, and how to render and publish R Markdown documents.
Elena Kosourova 's photo

Elena Kosourova

12 min

Introduction to DynamoDB: Mastering NoSQL Database with Node.js | A Beginner's Tutorial

Learn to master DynamoDB with Node.js in this beginner's guide. Explore table creation, CRUD operations, and scalability in AWS's NoSQL database.
Gary Alway's photo

Gary Alway

11 min

See MoreSee More