### Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

## Scatter Plots

A scatter plot graphs the relationships between two numeric variables.
It graphs each pair of variables as a point in a two dimensional space whose
coordinates are the corresponding `(x, y)`

values:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

# To create a scatter plot with the graphics package,

# call plot with two dataframe columns

plot(faithful$waiting,

faithful$eruptions,

main = "Using plot",

xlab = "waiting time (min)",

ylab = "eruption time (min)")

# The arguments pch, col, and cex modify the marker's shape, color, and size

plot(faithful$waiting,

faithful$eruptions,

pch = 17,

col = 2,

cex = 1.2,

main = "Specifying pch, col, and cex",

xlab = "waiting times (min)",

ylab = "eruption time (min)")

# Distinguish the points based on the value of another dataframe column

plot(mtcars$hp,

mtcars$mpg,

pch = mtcars$am,

cex = 1.2,

main = "MPG vs. HP by Transmission",

xlab = "horsepower",

ylab = "miles per gallon")

legend("topright", c("automatic", "manual"), pch = c(0, 1))

# Using qplot

qplot(x = waiting,

y = eruptions,

data = faithful,

main = "Using qplot - Waiting Times (sec) vs. Eruptions (min)")

# The graph below shows a scatter plot of car weights vs mpg

qplot(x = wt,

y = mpg,

data = mtcars,

main = "Using qplot - MPG vs. Weight (x1000 lbs)")

# Denoting the number of cylinders by size using the size argument

qplot(x = wt,

y = mpg,

data = mtcars,

size = cyl,

main = "Using qplot with Size - MPG vs. Weight (x1000 lbs) by Cylinder")

# Alternatively, color can be used to encode the number of cylinders

qplot(x = wt,

y = mpg,

data = mtcars,

color = cyl,

main = "Using qplot with Color - MPG vs. Weight (x1000 lbs) by Cylinder")

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Suggested playgrounds

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content