You are on page 1of 13

EXPERIMENT 7(A)

A) Write programs to illustrate the different plotting data distributions


like univariate distributions , bivariate distributions.

#UNIVARIATE DATA
X = c(20,28,26,24,23,29,54,25,19,
36,20,14,22,21,59,75,27,50,39,63,28,41,60,24,79
,24,57,23,38,64,87)
length(x)
summary(x)
table(x)
plot(density(x))#
OUTPUT:
> length(x)
[1] 31
> summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
14.00 23.50 28.00 38.68 55.50 87.00
> table(x)
x
14 19 20 21 22 23 24 25 26 27 28 29 36 38 39 41 50 54
1 1 2 1 1 2 3 1 1 1 2 1 1 1 1 1 1 1
57 59 60 63 64 75 79 87
1 1 1 1 1 1 1 1
> plot(density(x))#
library(ggplot2)
install.packages("mosaicData")
library(mosaicData)
data(Marriage,package="mosaicData")
ggplot(Marriage,aes(x=competition)+
geom_bar(fill="pink",color="black")+
labs(x="competition",y="Frequency",title="candidates"))
## Pie Chart
library(ggplot2)
df <- data.frame(value = c(15,25,19,16),
group = paste0("G", 1:4))
ggplot(df,aes(x = "",y = value,fill = group)) +
geom_col()+
coord_polar(theta = "y")

##Histogramset.seed(1234)
df <- data.frame(
sex=factor(rep(c("F", "M"),each=100)),
weigth=round(c(rnorm(100,mean=55, sd=5), rnorm(100,
mean=65,sd=5)))
)
df
head(df)
library(ggplot2)
OUTPUT:
>df
sex weigth
1 F 61
2 F 52
3 F 58
4 F 54
5 F 56
6 F 54
7 F 60
8 F 48
9 F 48
10 F 56
11 F 59
12 F 52
13 F 55
14 F 55
15 F 62
16 F 69
17 F 58
18 F 54
19 F 55
20 F 54
21 F 50
22 F 54
23 F 59
24 F 55
25 F 56
26 F 62
27 F 49
28 F 56
29 F 49
30 F 46
31 F 55
32 F 70
33 F 51
34 F 54
35 F 49
36 F 59
37 F 56
38 F 53
39 F 53
40 F 49
41 F 50
42 F 57
43 F 59
44 F 53
45 F 59
46 F 48
47 F 50
48 F 52
49 F 58
50 F 45
51 F 54
52 F 53
53 F 54
54 F 49
55 F 54
56 F 54
57 F 59
58 F 54
59 F 58
60 F 54
61 F 54
62 F 46
63 F 56
64 F 57
65 F 59
66 F 56
67 F 52
68 F 45
69 F 57
70 F 55
71 F 54
72 F 64
73 F 59
74 F 50
75 F 55
76 F 55
77 F 54
78 F 60
79 F 60
80 F 57
81 F 58
82 F 61
83 F 60
84 F 53
85 F 54
86 F 52
87 F 60
88 F 51
89 F 51
90 F 53
91 F 50
92 F 60
93 F 56
94 F 56
95 F 57
96 F 55
97 F 50
98 F 52
99 F 64
100 F 58
101 M 66
102 M 73
103 M 66
104 M 67
105 M 60
106 M 63
107 M 55
108 M 62
109 M 63
110 M 69
111 M 61
112 M 76
113 M 62
114 M 61
115 M 62
116 M 55
117 M 66
118 M 65
119 M 68
120 M 62
121 M 62
122 M 61
123 M 57
124 M 56
125 M 66
126 M 61
127 M 64
128 M 60
129 M 70
130 M 67
131 M 68
132 M 71
133 M 69
134 M 67
135 M 78
136 M 63
137 M 67
138 M 66
139 M 61
140 M 60
141 M 71
142 M 67
143 M 64
144 M 63
145 M 63
146 M 60
147 M 71
148 M 61
149 M 63
150 M 76
151 M 68
152 M 61
153 M 72
154 M 70
155 M 65
156 M 57
157 M 71
158 M 71
159 M 69
160 M 66
161 M 61
162 M 65
163 M 63
164 M 67
165 M 66
166 M 66
167 M 67
168 M 62
169 M 66
170 M 68
171 M 58
172 M 58
173 M 65
174 M 62
175 M 70
176 M 61
177 M 70
178 M 67
179 M 60
180 M 69
181 M 62
182 M 70
183 M 69
184 M 69
185 M 75
186 M 69
187 M 61
188 M 66
189 M 69
190 M 67
191 M 63
192 M 70
193 M 63
194 M 63
195 M 64
196 M 66
197 M 66
198 M 58
199 M 61
200 M 66

> head(df)

sex weigth
1 F 61
2 F 52
3 F 58
4 F 54
5 F 56
6 F 54

#Basic Histogram
ggplot(df,aes(x = weigth))+geom_histogram()
#change the width of bins
ggplot(df, aes(x = weigth))+geom_histogram(binwidth=1)
#change colours
p <- ggplot(df, aes(x=weigth))+
geom_histogram(color="black", fill="skyblue")
p
## Add mean line and density plot on the histogrm
## Add mean line
p+ geom_vline(aes(xintercept=mean(weigth)),color="blue",
linetype="dashed",size = 2)
OUTPUT:
#Histogram with density plot
ggplot(df,aes(x=weigth))+
geom_histogram(aes(y = ..density..),fill = "skyblue")+
geom_density(alpha=.2,fill="red")
OUTPUT:
#BIVARIATE DATA
# stacked bar chart
ggplot(mpg,aes(x= class, fill = drv))+
geom_bar(position = "stack")
OUTPUT:

# grouped bar chart


library(ggplot2)
#grouped bar plot
ggplot(mpg, aes(x = class,fill = drv))+
geom_bar(position = "dodge")
OUTPUT:
EXPERIMENT 7(B):
cat_frame<-
data.frame(gender=c('M','F','F','F','F','M','M','M','F'),team=c('A','B','C',
'D','B','C','A','C','D'),score =c(3,2,15,4,10,18,11,19,18))
cat_frame
OUTPUT:
> cat_frame

gender team score


1 M A 3
2 F B 2
3 F C 15
4 F D 4
5 F B 10
6 M C 18
7 M A 11
8 M C 19
9 F D 18
#barplot
ggplot(cat_frame,aes(x=gender))+geom_bar()
OUTPUT:

#boxplot
ggplot(cat_frame,aes(x=gender,y=score))+geom_boxplot()
OUTPUT:
#time series data plot
ggplot(iris,aes(x=Petal.Width,y=Petal.Length))+geom_line()

OUTPUT:

You might also like