-
Notifications
You must be signed in to change notification settings - Fork 10
/
ggplot.R
122 lines (62 loc) · 4.29 KB
/
ggplot.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"ggplot2 is a powerful and a flexible R package, implemented by Hadley Wickham,
for producing elegant graphics"
"The concept behind ggplot2 divides plot into three different fundamental parts:
Plot = data + Aesthetics + Geometry"
"The principal components of every plot can be defined as follow:"
"Data is a data frame
Aesthetics is used to indicate x and y variables. It can also be used to control the color, the size or the shape of points, the height of bars, etc…..
Geometry defines the type of graphics (histogram, box plot, line plot, density plot, dot plot, ….)"
#------------------------------------
"Install Package ggplot2"
#------------------------------------
install.packages("ggplot2") # download the package
library (ggplot2) # load the package
?mtcars # dataset to be used
#------------------------------------
"Basic scatter plots"
#------------------------------------
'Simple scatter plots are created using the R code. The color, the size and
the shape of points can be changed using the function geom_point()'
# mtcars = data
# aes = aesthetics
# geom_point(size, color, shape)
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
# Change the point size, and shape:
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(size=2, shape=23)
# Label points in the scatter plot:
# used "geom_text" to lavel points
ggplot(mtcars, aes(x=wt, y=mpg)) +geom_point() + geom_text(label=rownames(mtcars))
#------------------------------------
"Add regression lines in the data"
#------------------------------------
# geom_smooth() and stat_smooth() to be used
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()+ geom_smooth(method=lm)
# Remove the confidence interval:
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()+ geom_smooth(method=lm, se=FALSE)
# Loess method:
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()+ geom_smooth()
#------------------------------------
"Change the appearance of points and lines"
#------------------------------------
# Change colors and shape of point and line
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(shape=18, color="blue")+ geom_smooth(method=lm, se=FALSE, linetype="dashed", color="darkred")
# Change the confidence interval fill color
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(shape=18, color="blue")+ geom_smooth(method=lm, linetype="dashed", color="darkred", fill="blue")
#------------------------------------
"Scatter plots with multiple groups"
b #------------------------------------
# Change point shapes by the levels of cyl
ggplot(mtcars, aes(x=wt, y=mpg, shape=cyl)) + geom_point()
# Change point shapes and colors
ggplot(mtcars, aes(x=wt, y=mpg, shape=cyl, color=cyl)) + geom_point()
# Change point shapes, colors and sizes
ggplot(mtcars, aes(x=wt, y=mpg, shape=cyl, color=cyl, size=cyl)) + geom_point()
#------------------------------------
"Add multiple regression lines:"
#------------------------------------
# Add regression lines
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) + geom_point() + geom_smooth(method=lm)
# Remove confidence intervals and Extend the regression lines
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) + geom_point() + geom_smooth(method=lm, se=FALSE, fullrange=TRUE)
#The fill color of confidence bands can be changed as follow :
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl))+ geom_point() + geom_smooth(method=lm, aes(fill=cyl))