forked from eoloughlin/How-To-R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
36_How_To_Code.R
32 lines (32 loc) · 993 Bytes
/
36_How_To_Code.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
#
# Author: Dr Eugene O'Loughlin
# Video Title: How To... Add a Legend to a Histogram in R
# Video Number: #36
# Data File Used: 36_Data_File.csv
#
# Read data file
height <- read.csv(file="36_Data_File.csv",
head=TRUE, sep=",") # 50 men, 75 women
head(height)
#
# Plot histogram of female heights
hist(height$Female, main = "Height Histogram", # Female histogram
col = rgb(1, 0, 0, 0.5),
xlab = "Heights (cm)",
xlim = c(150,200),
ylim = c(0,30))
#
# Add male histogram
hist(height$Male, col = rgb(0, 0, 1, 0.5), add = TRUE)
#
# Create legend labels
labels <- c("Females", "Males")
#
# Create and add legend
legend("topright", legend = labels,
cex = 0.8, # legend box size
inset = 0.01, # distance from edge
pch = 15, # shape of legend symbols
col = c(rgb(1, 0, 0, 0.5), # colours
rgb(0, 0, 1, 0.5)))
#