You are on page 1of 11

D.

A LAB ASSIGNMENT-5
NAME : RICHA GUPTA
ROLL NO : 1906569
SECTION : IT-7

#1.
1. Explore any 5 built-in dataset and perform the following for each dataset

> Display the summary

> Display the structure

> Display the first 5 rows

> Display the number of rows

>Display the number of columns


SOLUTION:

INPUT:

data(PlantGrowth )
str(PlantGrowth )
head(PlantGrowth,5)
nrow(PlantGrowth )
ncol(PlantGrowth )

data(ToothGrowth)
str(ToothGrowth)
head(ToothGrowth,5)
nrow(ToothGrowth)
ncol(ToothGrowth)

data(EuStockMarkets)
str(EuStockMarkets)
head(EuStockMarkets,5)
nrow(EuStockMarkets)
ncol(EuStockMarkets)

data(HairEyeColor )
str(HairEyeColor)
head(HairEyeColor,5)
nrow(HairEyeColor )
ncol(HairEyeColor )

data(Nile)
str(Nile)
head(Nile,5)
nrow(Nile)
ncol(Nile)

OUTPUT :
#2. 2. Explore any built-in dataset and plot the following

Line Charts

Bar Plot

Histogram

Pie Chart

Box Plot

Scatter Plot

Kernel Density Plot
SOLUTION:

INPUT:
LINE CHARTS
cars <- c(1, 2, 3, 4, 5)
plot(cars)
plot(cars, type = "o")
plot(cars, type="o", col="blue")

plot(cars,type = "o", col = "red", xlab = "Month", ylab = "Unit Produced")


trucks <- c(6, 5, 3, 2, 2)
lines(trucks, type = "o", col = "blue")

OUTPUT:
BAR PLOT
INPUT:

H <- c(7,12,28,3,41)
barplot(H)
M <- c("Mar","Apr","May","Jun","Jul")
barplot(H,names.arg=M,xlab="Month",ylab="Revenue",col="blue",

OUTPUT:

INPUT

colors = c("green","orange","brown")
months <- c("Mar","Apr","May","Jun","Jul")
regions <- c("East","West","North")
values <- matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11), nrow = 3, ncol = 5, byrow = TRUE)
barplot(values, main = "Total Revenue", names.arg = months, xlab = "month", ylab

OUTPUT:

Histogram

INPUT :

v <- c(9,13,21,8,36,22,12,41,31,33,19)
hist(v,xlab = "Weight",col = "yellow",border = "blue")
hist(v,xlab = "Weight",col = "blue",border = "red", xlim = c(0,40), ylim = c(0,5),breaks=5)
OUTPUT:

PIE CHART

INPUT:
x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore", "Mumbai")
pie(x,labels)
pie(x, labels, main = "City pie chart", col = rainbow(length(x)))
piepercent<- round(100*x/sum(x), 1)
pie(x, labels = piepercent, main = "City pie chart",col = rainbow(length(x)))
legend("topright", c("London","New York","Singapore","Mumbai"), cex = 0.8,fill=rainbow(length(x))

OUTPUT

INPUT :
install.packages("plotrix")
library(plotrix)
x <- c(21, 62, 10,53)
lbl <- c("London","New York","Singapore","Mumbai")
pie3D(x,labels = lbl, explode = 0.1, main = "Pie Chart of Countries") #2.4.Dot Chart
dotchart(PlantGrowth$weight,col="red",pch=1,labels=PlantGrowth$group, main="group vs
weight", xlab="weight")
pg <- PlantGrowth
pg$color[pg$group=="ctrl"] <- "red"
pg$color[pg$group=="trt1"] <- "Violet"
pg$color[pg$group=="trt2"] <- "blue“
dotchart(PlantGrowth$weight, labels=PlantGrowth$group,cex=0.8,groups= PlantGrowth$group,
main="group vs weight",xlab="weight",gcolor="black",color=pg$color)
OUTPUT:

INPUT:

data2 = USArrests[order(USArrests$Murder),]
dotchart(data2$Murder, labels = row.names(data2),
cex = .5, main = "Murder arrests by state, 1973",
xlab = "Murder arrests per 100,000 population")

OUTPUT
BOX PLOT

INPUT:

input <- mtcars[,c('mpg','cyl')]


print(head(input))
boxplot(mpg ~ cyl, data = mtcars, xlab = "Number of Cylinders",
ylab = "Miles Per Gallon", main = "Mileage Data")
boxplot(mpg ~ cyl, data = mtcars, xlab = "Number of Cylinders", ylab = "Miles Per Gallon",
main = "Mileage Data", notch = TRUE, varwidth = TRUE, col = c("green","yellow","purple"),
names = c("High","Medium","Low")

OUTPUT:

Scatter Plot:

INPUT:
input <- mtcars[,c('wt','mpg')]
plot(x = input$wt,y = input$mpg,
xlab = "Weight",
ylab = "Age",
xlim = c(2.5,5),
ylim = c(15,30),
main = "Weight vs Age"
)

OUTPUT:
Scatter Plot

INPUT
input <- mtcars[,c('wt','mpg')]
plot(x = input$wt,y = input$mpg,
xlab = "Weight",
ylab = "Age",
xlim = c(2.5,5),
ylim = c(15,30),
main = "Weight vs Age"
)

OUTPUT

Kernel Density Plot

INPUT:
den = density(mtcars$mpg)
plot(den,main=" Kernel Density of Miles Per Gallon") polygon(d, col="red",
border="blue")

OUTPUT:

# 3. Draw a Bubble Chart using ggplot2 package

INPUT:

# creating data set columns


x <- c(12,23,43,61,78,54,34,76,58,103,39,46,52,33,11) y <-
c(12,54,34,76,54,23,43,61,78,23,12,34,56,98,67) r <-
c(1,5,13,8,12,3,2,16,7,40,23,45,76,8,7)

color <- c(rep("color1", 1), rep("color2", 2),


rep("Color3", 3), rep("color4", 4),
rep("color5", 5))
# creating the dataframe from the above columns data <- data.frame(x, y, r,
color)

ggplot(data, aes(x = x, y = y,size = r, color=color))+ geom_point(alpha = 0.7)

OUTPUT:

# creating data set columns


x <- c(12,23,43,61,78,54,34,76,58,103,39,46,52,33,11) y <-
c(12,54,34,76,54,23,43,61,78,23,12,34,56,98,67)
r <- c(1,5,13,8,12,3,2,16,7,40,23,45,76,8,7)

colors <- c(1,2,3,1,2,3,1,1,2,3,1,2,2,3,3)

# creating the dataframe from the # above columns


data <- data.frame(x, y, r, colors)

# importing the ggplot2 library and


# RColorBrewer
library(RColorBrewer)
library(ggplot2)
# Draw plot
ggplot(data, aes(x = x, y = y,size = r, color=as.factor(colors))) + geom_point(alpha = 0.7)+
scale_color_brewer(palette="Spectral")

You might also like