### Open Source Your Knowledge, Become a Contributor

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

## Smoothing Histograms Using qplot

The `ggplot2`

package incorporates smoothed histogram graphing into `qplot`

.
The value of `h`

is controlled via the `adjust`

parameter:

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

qplot(x = c(2, 3, 6, 7),

y = ..density..,

geom = c("density"),

kernel = "gaussian",

adjust = 0.05,

main = "adjust = 0.05",

xlab = "$x$",

ylab = "$f_h(x)$",

xlim = c(0, 9))

# Increasing the value of h causes more overlap

qplot(x = c(2,3,6,7),

y = ..density..,

geom = c("density"),

kernel = "gaussian",

adjust = 0.2,

main = "adjust = 0.2",

xlab = "$x$",

ylab = "$f_h(x)$",

xlim = c(0, 9))

# Increasing the value of h further aggregates the four peaks

# into two, each responsible for a corresponding pair of nearby points

qplot(x = c(2, 3, 6, 7),

y = ..density..,

geom = c("density"),

kernel = "gaussian",

adjust = 0.5,

main = "adjust = 0.5",

xlab = "$x$",

ylab = "$f_h(x)$",

xlim = c(0, 9))

# Finally, further increasing the value of h results in a histogram

# with very wide bins in which all points fall in the same bin

qplot(x = c(2, 3, 6, 7),

y = ..density..,

geom = c("density"),

kernel = "gaussian",

adjust = 10,

main = "adjust = 10",

xlab = "$x$",

ylab = "$f_h(x)$",

xlim = c(0, 9))

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Suggested playgrounds

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