-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathOverlayingPlots.R
52 lines (37 loc) · 1.36 KB
/
OverlayingPlots.R
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
# File: OverlayingPlots.R
# Course: R: An Introduction (with RStudio)
# INSTALL AND LOAD PACKAGES ################################
library(datasets) # Load/unload base packages manually
# LOAD DATA ################################################
# Annual Canadian Lynx trappings 1821-1934
?lynx
head(lynx)
# HISTOGRAM ################################################
# Default
hist(lynx)
# Add some options
hist(lynx,
breaks = 14, # "Suggests" 14 bins
freq = FALSE, # Axis shows density, not freq.
col = "thistle1", # Color for histogram
main = paste("Histogram of Annual Canadian Lynx",
"Trappings, 1821-1934"),
xlab = "Number of Lynx Trapped")
# Add a normal distribution
curve(dnorm(x, mean = mean(lynx), sd = sd(lynx)),
col = "thistle4", # Color of curve
lwd = 2, # Line width of 2 pixels
add = TRUE) # Superimpose on previous graph
# Add two kernel density estimators
lines(density(lynx), col = "blue", lwd = 2)
lines(density(lynx, adjust = 3), col = "purple", lwd = 2)
# Add a rug plot
rug(lynx, lwd = 2, col = "gray")
# CLEAN UP #################################################
# Clear packages
detach("package:datasets", unload = TRUE) # For base
# Clear plots
dev.off() # But only if there IS a plot
# Clear console
cat("\014") # ctrl+L
# Clear mind :)