-
Notifications
You must be signed in to change notification settings - Fork 0
/
R5.R
153 lines (107 loc) · 4.05 KB
/
R5.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
#......Data Visualization with --base R-- --ggplo2-- --lattice--
#import data
oj <- read.csv("C:/Users/shwetag/Downloads/oj.csv", header = TRUE)
str(oj)
#histrogram
hist(oj$price)
hist(oj$price, col=c("red", "green", "blue"))
#...Line chart
x=c(2,3,5,4,6,4,8,3,7,8,0,3,2,6,9)
plot(x, type="l") #Simple Line Plot
plot(oj$price,type="l")
#....bar plot
iris
barplot(iris$Petal.Length) #Creating simple Bar Graph
barplot(iris$Petal.Length, col=("red"))
#boxplot
boxplot(iris$Petal.Length~iris$Species) #Creating Box Plot between two variable
data(iris)
par(mfrow=c(2,2)) #display multiple graphs on same graphics window
boxplot(iris$Sepal.Length,col="red")
boxplot(iris$Sepal.Length~iris$Species,col="red")
boxplot(iris$Sepal.Length~iris$Species,col=heat.colors(3))
#Scatter plot
plot(x=iris$Petal.Length) #Simple Scatter Plot
plot(x=iris$Petal.Length,y=iris$Species) #Multivariate Scatter Plot
#pie chart
pie(table(iris$Species))
# correlation plot
install.packages("corrplot")
library(corr)
m<-cor(iris[,-5]) #corr of numeriacal variables
corrplot(m,method="number",type<-"lower")
#ggplot
#scatter plot
install.packages("ggplot2")
library(ggplot2)
ggplot(iris, aes(Sepal.Length, Petal.Length )) +
geom_point() + scale_x_continuous("Sepal.Length", breaks = seq(0,0.35,0.05))+ scale_y_continuous("Petal.Length", breaks = seq(0,270,by = 30))+ theme_bw()
#kernel density plot for mpg
data(mtcars)
qplot(mpg, data=mtcars, geom="density", fill=gear, alpha=I(.5),
main="Distribution of Gas Milage", xlab="Miles Per Gallon",
ylab="Density")
#Separate regressions of mpg on weight for each number of cylinders
qplot(wt, mpg, data=mtcars, geom=c("point", "smooth"),
method="lm", formula=y~x, color=cyl,
main="Regression of MPG on Weight",
xlab="Weight", ylab="Miles per Gallon")
#Boxplots of mpg by number of gears
qplot(gear, mpg, data=mtcars, geom=c("boxplot", "jitter"),
fill=gear, main="Mileage by Gear Number",
xlab="", ylab="Miles per Gallon")
#Bar chart example
c <- ggplot(mtcars, aes(factor(cyl)))
c + geom_bar() #default plotting
c + geom_bar(fill = "red") #change interior colour
c + geom_bar(colour = "red") #changes just the bar outline
c + geom_bar(fill = "blue", colour = "red") #do the both
#size changes
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point(size = 4)
p + geom_point(aes(size = qsec))
p + geom_point(size = 2.5) + geom_hline(yintercept = 25, size = 3.5)
#shape
p + geom_point()
p + geom_point(shape = 5)
p + geom_point(shape = "k", size = 3)
p + geom_point(shape = ".")
p + geom_point(aes(shape = factor(cyl)))
#facet grid
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(. ~ cyl)
#plottins with lattice
install.packages("lattice")
library(lattice)
attach(mtcars)
#create factors with value labels
gear.f<-factor(gear,levels=c(3,4,5),
labels=c("3gears","4gears","5gears"))
cyl.f <-factor(cyl,levels=c(4,6,8),
labels=c("4cyl","6cyl","8cyl"))
#Kernel density plot
densityplot(~mpg,
main="Density Plot",
xlab="Miles per Gallon")
#Kernel density plots by factor level
densityplot(~mpg|cyl.f,
main="Density Plot by Number of Cylinders",
xlab="Miles per Gallon")
#Boxplots for each combination of two factors
bwplot(cyl.f~mpg|gear.f,
ylab="Cylinders", xlab="Miles per Gallon",
main="Mileage by Cylinders and Gears",
layout=(c(1,3)))
#Scatterplots for each combination of two factors
xyplot(mpg~wt|cyl.f*gear.f,
main="Scatterplots by Cylinders and Gears",
ylab="Miles per Gallon", xlab="Car Weight")
# 3D sctter plot
cloud(mpg~wt*qsec|cyl.f,
main="3D Scatterplot by Cylinders")
#Dotplot for each combination of two factors
dotplot(cyl.f~mpg|gear.f,
main="Dotplot Plot by Number of Gears and Cylinders",
xlab="Miles Per Gallon")
#scatter plot matrix
splom(mtcars[c(1,3,4,5,6)],
main="MTCARS Data")