-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson25-scatterplot-charts.R
84 lines (47 loc) · 1.68 KB
/
lesson25-scatterplot-charts.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
hw <- read.csv("hw.csv")
View(hw)
##########
### how to build scatterplot charts with ggplot2
##########
### we will create a scatterplot with the variables
### height and weight
### load the package
require(ggplot2)
### create the plot
ggplot()+geom_point(data=hw, aes(x=height, y=weight))+
scale_x_continuous(limits=c(150,193))
#########
### build a clustered scatterplot
### by gender
lgd <- hw$gender
### get points of different colors
ggplot()+geom_point(data=hw, aes(x=height, y=weight, color=lgd))+
scale_x_continuous(limits=c(150,193))
### get points of different shapes
ggplot()+geom_point(data=hw, aes(x=height, y=weight, shape=lgd))+
scale_x_continuous(limits=c(150,193))
### get points of both different shapes and colors
ggplot()+geom_point(data=hw, aes(x=height, y=weight, shape=lgd, color=lgd))+
scale_x_continuous(limits=c(150,193))
###########
### add a trendline to the scatterplot
### create a linear model
### with weight as the dependent variable and height as the explainer
model <- lm(weight~height, data=hw)
print(model)
### get the minimum and the maximum height
minh <- min(hw$height)
maxh <- max(hw$height)
### create a new vector height
height <- c(minh, maxh)
print(height)
### predict the weight based on the height, with the model above
fit <- predict(model, data.frame(height))
print(fit)
### create a data frame with the line end points
endpoints <- data.frame(height, fit)
View(endpoints)
### build the scatter plot with trend line
ggplot()+
geom_point(data=hw, aes(x=height, y=weight))+
geom_line(data=endpoints, aes(x=height, y=fit), color="red", size=1)