0% found this document useful (0 votes)
41 views21 pages

Chapter2 PDF

This document introduces visualization with ggplot2 and the tidyverse. It demonstrates how to create scatter plots with the gapminder data, including using log scales and adding aesthetics for color and size. Faceting is also introduced to split plots by variables like continent. The goal is to practice basic visualization and data wrangling techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views21 pages

Chapter2 PDF

This document introduces visualization with ggplot2 and the tidyverse. It demonstrates how to create scatter plots with the gapminder data, including using log scales and adding aesthetics for color and size. Faceting is also introduced to split plots by variables like continent. The goal is to practice basic visualization and data wrangling techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Visualizing with

ggplot2
I N T R O D U C T I O N TO T H E T I DY V E R S E

David Robinson
Chief Data Scientist, DataCamp
INTRODUCTION TO THE TIDYVERSE
Variable Assignment
gapminder_2007 <- gapminder %>%
filter(year == 2007)

gapminder_2007

# A tibble: 142 x 6
country continent year lifeExp pop gdpPercap
<fctr> <fctr> <int> <dbl> <int> <dbl>
1 Afghanistan Asia 2007 43.828 31889923 974.5803
2 Albania Europe 2007 76.423 3600523 5937.0295
3 Algeria Africa 2007 72.301 33333216 6223.3675
4 Angola Africa 2007 42.731 12420476 4797.2313
5 Argentina Americas 2007 75.320 40301927 12779.3796
6 Australia Oceania 2007 81.235 20434176 34435.3674
7 Austria Europe 2007 79.829 8199783 36126.4927
8 Bahrain Asia 2007 75.635 708573 29796.0483
9 Bangladesh Asia 2007 64.062 150448339 1391.2538
10 Belgium Europe 2007 79.441 10392226 33692.6051
# ... with 132 more rows

INTRODUCTION TO THE TIDYVERSE


Visualizing with ggplot2

library(ggplot2)

ggplot(gapminder_2007, aes(x = gdpPerCap, y = lifeExp)) +


geom_point()

INTRODUCTION TO THE TIDYVERSE


Let's practice!
I N T R O D U C T I O N TO T H E T I DY V E R S E
Log scales
I N T R O D U C T I O N TO T H E T I DY V E R S E

David Robinson
Chief Data Scientist, DataCamp
Scatter plot

library(ggplot2)

ggplot(gapminder, aes(x = gdpPerCap, y = lifeExp)) +


geom_point()

INTRODUCTION TO THE TIDYVERSE


Log scale

ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp)) +


geom_point() +
scale_x_log10()

INTRODUCTION TO THE TIDYVERSE


Let's practice!
I N T R O D U C T I O N TO T H E T I DY V E R S E
Additional aesthetics
I N T R O D U C T I O N TO T H E T I DY V E R S E

David Robinson
Chief Data Scientist, DataCamp
Scatter plots

INTRODUCTION TO THE TIDYVERSE


Additional variables
gapminder_2007

# A tibble: 142 x 6
country continent year lifeExp pop gdpPercap
<fctr> <fctr> <int> <dbl> <dbl> <dbl>
1 Afghanistan Asia 2007 43.828 31889923 974.5803
2 Albania Europe 2007 76.423 3600523 5937.0295
3 Algeria Africa 2007 72.301 33333216 6223.3675
4 Angola Africa 2007 42.731 12420476 4797.2313
5 Argentina Americas 2007 75.320 40301927 12779.3796
6 Australia Oceania 2007 81.235 20434176 34435.3674
7 Austria Europe 2007 79.829 8199783 36126.4927
8 Bahrain Asia 2007 75.635 708573 29796.0483
9 Bangladesh Asia 2007 64.062 150448339 1391.2538
10 Belgium Europe 2007 79.441 10392226 33692.6051
# ... with 132 more rows

INTRODUCTION TO THE TIDYVERSE


The color aesthetic

ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +


geom_point() +
scale_x_log10()

INTRODUCTION TO THE TIDYVERSE


The size aesthestic

ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent,


size = pop)) +
geom_point() +
scale_x_log10()

INTRODUCTION TO THE TIDYVERSE


Aesthetics
Aesthetic Variable

x gdpPerCap

y lifeExp

color continent

size pop

INTRODUCTION TO THE TIDYVERSE


Let's practice!
I N T R O D U C T I O N TO T H E T I DY V E R S E
Faceting
I N T R O D U C T I O N TO T H E T I DY V E R S E

David Robinson
Chief Data Scientist, DataCamp
INTRODUCTION TO THE TIDYVERSE
Faceting

ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp)) +


geom_point() +
scale_x_log10() +
facet_wrap(~ continent)

INTRODUCTION TO THE TIDYVERSE


INTRODUCTION TO THE TIDYVERSE
Let's practice!
I N T R O D U C T I O N TO T H E T I DY V E R S E

You might also like