-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphPlot.R
198 lines (186 loc) · 5.56 KB
/
GraphPlot.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
A<- matrix(1:6,nrow = 2)
dim(A)
B<- matrix(5:10,nrow=2)
dim(B)
A+B
2A
2*A
C<-matrix(5:10,nrow = 3)
C
A*C
A%*%C
t(A)
D <- matrix(1:4,nrow=2)
solve(D)
D%*%solve(D)
data(iris)
iris[10,] # returns the 10th row of the iris data frame
iris[1:10,4] #first 10 rows of the 4th variable in iris
iris[1:10,"Species"] #first 10 rows of the Species variable
iris[1:10,4]
iris[1:10,"Petal.Widht"]
iris$Petal.Width[1:10]
x <- array(1:4,dim=c(4)) #1-dim array (like a vector)
x
is.array(x)
is.vector(x)
x <- array(1:4,dim=c(2,2)) #2-dim array (like a matrix)
x
x <- array(1:27,dim=c(3,3,3))
# Graphics in R Introduction and Preliminaries
dim(x)
x[1,1,1]
x[2,,]
x <- array(1:4,dim=c(2,2)) #2-dim array (like a matrix)
x <- array(1:4,dim=c(2,2)) #2-dim array (like a matrix)
x <- array(1:4,dim=c(2,2)) #2-dim array (like a matrix)
x <- array(1:4,dim=c(2,2)) #2-dim array (like a matrix)
dim(x)
x[1,1,1]
x[2,,]
length(dim(x))
# The ability to represent data graphically is an important part of data analysis for the following reasons:
# *Investigating the data visually helps you to get to know the data.
# *As part of analysis, graphs can be used to check validity of any assumptions made and to interpret the results of the analysis.
#3. High Level Plots
xvalues<- -5:5
quadx<-xvalues^2
plot(xvalues,quadx,type = "p") # p - point
plot(xvalues,quadx,type = "l") # line
plot(xvalues,quadx,type = "h") # hi-density
plot(xvalues,quadx,type = "s") # stepped
plot(xvalues,quadx,type = "n") # none
plot(xvalues,quadx,type = "b") # both
#Example here
plot(exp(xvalues),quadx,type = "l")
plot(abs(xvalues),quadx,type = "l")
plot(sin(xvalues),quadx,type = "l")
plot(cos(xvalues),quadx,type = "l")
plot(log(xvalues),quadx,type = "l") # showing warning messages
plot(rnorm(10))
plot(rnorm(50))
plot(rnorm(50,mean=10,sd=0.5))
#Bar Charts
help(VADeaths)
VADeaths
class(VADeaths)
barplot(VADeaths["50-54",])
barplot(VADeaths)
barplot(VADeaths,beside = TRUE)
t(VADeaths)
barplot(t(VADeaths))
hist(rnorm(1000))
hist(rexp(1000,4))
#BoxPlots
?InsectSprays
boxplot(count ~ spray,data = InsectSprays)
boxplot(count ~ spray, data = InsectSprays, + col = "lightblue",pch=16)
boxplot(count ~ spray, data = InsectSprays,
+ col = "lightblue",pch=16)
# Quantile plots
qqnorm(x)
qqline(x)
sum(iris$Petal.Length<5.5)
sum(iris$Petal.Length<5.5) / length(iris$Petal.Length)
elem<- which(iris$Species=="virginica")
elem
qqnorm(iris$Petal.Length[elem])
qqnorm(iris$Petal.Length[elem])
#Plotting Multivariate Data
matplot()
data(USArrests)
help(USArrests)
matplot(USArrests, type = "l")
USPopRank<- order(USArrests@Assault)
#Pairwise plots
pairs(USArrests)
# Condition Plots
plot(iris$Sepal.Length,iris$Petal.Length)
coplot(Petal.Length~Sepal.Length | Species, data = iris)
data(swiss)
names(swiss)
coplot(Fertility~Education|Agriculture, data=swiss, overlap = 0)
coplot(Fertility~Education|Agriculture, data=swiss, overlap = 0.5)
coplot(Fertility~Education|Agriculture, data=swiss, overlap = 0.5, + panel = panel.smooth)
class (swiss)
plot(swiss)
data(state)
class(state.division)
plot(state.division)
class(xvalues)
plot.default(xvalues,quadx)
#Enhancing and Adapting Graphics
xvalues<- 5:5
quadx<-xvalues^2
plot(xvalues,quadx,type = "b",main="this is a title",+ sub="this is the subtitle",xlab="x",ylab="x squared",col.main="green",+col="blue",lty=2,pch="*",cex=3)
plot(xvalues,quadx,type="b",main="this is a title",
+ sub="this is the subtitle",xlab="x", ylab="x squared",col.main="green",
+ col="blue", lty=2,pch="*",cex=3)
?morley
plot(morley$Expt,morley$Speed)
plot(jitter(morley$Expt),morley$Speed)
sunflowerplot(morley$Expt,morley$Speed)
plot(mtcars$wt,mtcars$mpg)
fourcyl<-mtcars[,"cyl"]==4
sixcyl<-mtcars[,"cyl"]==6
eightcyl<-mtcars[,"cyl"]==8
plot(mtcars[,"wt"],mtcars[,"mpg"],type="n")
points(mtcars[fourcyl,"wt"],mtcars[fourcyl,"mpg"],pch="+",col="blue")
points(mtcars[sixcyl,"wt"],mtcars[sixcyl,"mpg"],pch="o",col="red")
points(mtcars[eightcyl,"wt"],mtcars[eightcyl,"mpg"],pch="x",col="magenta")
plot(mtcars[fourcyl,"wt"],mtcars[fourcyl,"mpg"],pch="+",col="blue")
abline(h=median(mtcars[,"mpg"]),col="green")
legend(1.5,14.0,legend=c("Four Cylinder","Six Cylinder","Eight Cylinder"),+ col=c("blue","red","magenta"),pch=c("+","o","x"))
legend(1.5,14.0,legend=c("Four Cylinder","Six Cylinder","Eight Cylinder"),
+ col=c("blue","red","magenta"),pch=c("+","o","x"))
colors()
palette()
barplot(VADeaths,beside = TRUE)
palette()
barplot(VADeaths,beside = TRUE,col = 1:5)
palette(rainbow(7))
palette()
barplot(VADeaths,beside = TRUE,col=1:5)
palette("default")
palette()
# Example of Using axis()
plot(xvalues,quadx,axes = FALSE)
axis(side = 1,pos=0,at=c(-5,0,5))
axis(side=2,pos=0,at=c(0,5,10,15,20,25), + labels=c("","5","10","15","20","25"),las=1)
axis(side=2,pos=0,at=c(0,5,10,15,20,25),
labels=c("","5","10","15","20","25"),las=1)
box()
plot(mtcars[,"wt"],mtcars[,"mpg"])
plot(xvalues,quadx,type="b")
par(mfrow=c(3,2))
par(mfrow=c(1,1))
# Graphics Device
RStudioGD()
dev.list()
dev.set(2)
dev.off()
RStudioGD()
M1<-matrix(c(1,2,5,3,1,6,7,2,8),ncol = 1)
M2<-matrix(c(1,-1,-6,2,1,1),ncol = 2)
M3<-M1+M2
dim(M1)
dim(M2)
xvalues <- -5:5
quadx<- xvalues^2
plot(xvalues,quadx,type="b",main="This is a title", sub="This is the subtitle",
xlab="x",ylab="x squared",col.main="green",col="blue",lty=2,pch="???",cex=3)
#Exercise 6
#Question 1
M1<-matrix(c(1,2,5,3,1,6,7,2,8),nrow=3)
M2<-matrix(c(1,-1,-6,2,1,1),ncol=2)
M3<- M1%*%M2
#Question 2
M<-seq(from=0,to=2*pi,length=20)
length(M)
range(M)
plot(sin(M),col="green")
plot(M,sin(M),main = "Overlaying Graphs",
ylab = "",type = "l",col="green")
lines(M,cos(M),col="red")
legend("topleft",
c("sin(M)","cos(M)"),fill=c("green","red"))