1
+ # 2D Pie chart
2
+ # x should be a numeric vector
3
+ x = c(10 , 30 , 50 , 100 )
4
+ label = c(" Data1" , " Data2" , " Data3" , " Data4" )
5
+ pie(x , label , col = rainbow(length(x )), main = " SAMPLE 2D PIE CHART" )
6
+
7
+ # #############################################################################################
8
+
9
+ # To use 3D charts install "plotrix" package and import it
10
+ # If using for first time, uncomment this line to install plotrix package
11
+ # install.packages("plotrix")
12
+ library(plotrix )
13
+
14
+ # 3D Pie chart
15
+ x = c(10 , 30 , 50 , 100 )
16
+ label = c(" Data1" , " Data2" , " Data3" , " Data4" )
17
+ # No need for "col" parameter to be specified
18
+ pie3D(x , labels = label , explode = 0.05 , main = " SAMPLE 3D PIE CHART" )
19
+
20
+ # #############################################################################################
21
+
22
+ # BarPlot
23
+ x = c(100 , 75 , 90 , 82 , 95 )
24
+ students = c(" S1" , " S2" , " S3" , " S4" , " S5" )
25
+ # names.arg caries label for each bar in the barplot
26
+ barplot(x , names.arg = students , col = rainbow(length(x )),
27
+ xlab = " Students" , ylab = " Marks" , main = " BAR PLOT - STUDENT MARKS" )
28
+
29
+ # #############################################################################################
30
+
31
+ # Stack Barplot
32
+ m1 = c(100 , 75 , 90 , 82 , 95 )
33
+ m2 = c(40 , 70 , 60 , 54 , 81 )
34
+ m3 = c(20 , 12 , 50 , 23 , 49 )
35
+ # Creating a matrix with 3 rows and 5 columns arranging the data row wise
36
+ values = matrix (c(m1 ,m2 ,m3 ), nrow = 3 , ncol = 5 , byrow = TRUE )
37
+ print(" MATRIX OF MARKS SECURED BY 5 STUDENTS IN 3 SUBJECTS" )
38
+ print(values )
39
+ students = c(" S1" , " S2" , " S3" , " S4" , " S5" )
40
+ subjects = c(" M1" , " M2" , " M3" )
41
+ barplot(values , names.arg = students , col = rainbow(length(subjects )),
42
+ xlab = " Students" , ylab = " Marks" , main = " STACKED BAR PLOT - STUDENT MARKS SPLIT UP" )
43
+ # Adding legend for 3 subjects and specifying position of legend in the graph - 0,230
44
+ legend(0 ,230 , cex = .6 , subjects , fill = rainbow(length(subjects )))
45
+
46
+ # #############################################################################################
47
+
48
+ # Data of Boxplots - taking "mtcars" dataset in R
49
+ count = 1
50
+ cyl4 = c()
51
+ cyl6 = c()
52
+ cyl8 = c()
53
+ for (i in mtcars [,' cyl' ])
54
+ {
55
+
56
+ if (i == 4 )
57
+ {
58
+ cyl4 = c(cyl4 , mtcars [count ,' mpg' ])
59
+ }
60
+ if (i == 6 )
61
+ {
62
+ cyl6 = c(cyl6 , mtcars [count ,' mpg' ])
63
+ }
64
+ if (i == 8 )
65
+ {
66
+ cyl8 = c(cyl8 , mtcars [count ,' mpg' ])
67
+ }
68
+ count = count + 1
69
+ }
70
+ print(" MPG for 4 cylinders" )
71
+ print(sort(cyl4 ))
72
+ print(" MPG for 6 cylinders" )
73
+ print(sort(cyl6 ))
74
+ print(" MPG for 8 cylinders" )
75
+ print(sort(cyl8 ))
76
+
77
+ # BoxPlot
78
+ boxplot(mpg ~ cyl , data = mtcars ,
79
+ xlab = " No of Cylinders" , ylab = " Miles per Gallon" , main = " BOXPLOT SAMPLE" ,
80
+ col = rainbow(length(mpg ~ cyl )))
81
+
82
+ # #############################################################################################
83
+
84
+ # Histogram
85
+ # Seq(start, end, by = range of each bin)
86
+ v = c(10 ,20 ,30 ,4 ,5 ,35 ,60 )
87
+ hist(v , breaks = seq(0 ,100 , by = 20 ), main = " Histogram Plot" , col = " red" , xlab = " Values" )
88
+
89
+ # #############################################################################################
90
+
91
+ # Line charts - 1D
92
+ v1 = c(10 ,20 ,30 ,4 ,5 ,35 ,60 )
93
+ v2 = c(15 ,25 ,35 ,40 ,15 ,25 ,10 )
94
+ # Plot types -> l, p, b, o, s, h
95
+ plot(v1 , type = " o" , col = rainbow(length(v1 )), xlab = " Index" , ylab = " Frequency" ,
96
+ main = " Line Plot" )
97
+ # To plot another line in same graph
98
+ lines(v2 , type = " o" , col = rainbow(length(v2 )))
99
+
100
+ # #############################################################################################
101
+
102
+ # ScatterPlot
103
+ input = mtcars [,c(' mpg' , ' wt' )]
104
+ plot(x = input $ mpg , y = input $ wt , xlim = c(10 ,40 ), ylim = c(0 ,6.5 ),
105
+ xlab = " Miles per gallon" , ylab = " Weight of the car" , main = " Scatter Plot" )
106
+
107
+ # Scatterplot with one hot encoding using pch
108
+ with(iris , plot(Petal.Length , Petal.Width , pch = as.integer(Species )))
109
+
110
+ # #############################################################################################
111
+
112
+ # Gamma Distribution as Histogram and density function
113
+ # rgamma(n, shape, scale)
114
+ sampledata = rgamma(500 ,1 ,2 )
115
+ print(sampledata )
116
+ # Plotting Histogram and Density function for random gamma function generated
117
+ hist(sampledata , prob = T )
118
+ lines(density(sampledata ))
119
+
120
+ # #############################################################################################
0 commit comments