You are on page 1of 9

EXPERIMENT-7

PROBLEM STATEMENT : DATA VISUALISATIONS-II IN R.


(A) WRITE PROGRAMS TO ILLUSTRATE THE DIFFERENT PLOTTING DATA
DISTRIBUTIONS LIKE,UNIVARIATE DISTRIBUTIONS,BIVARIATE DISTRIBUTIONS.

CODE:
#DATA VISUALISATIONS IN R
#UNIVARIATE ANALYSIS OF A CATEGORICAL VARIABLE
x = c(3,6,9,12,15,18)
length(x)
summary(x)
table(x)
plot(density(x))
install.packages("ggplot2")
library(ggplot2)
install.packages("mosaicData")
library(mosaicData)
data(Marriage,package="mosaicData")
ggplot(Marriage,aes(x=race))+geom_bar(fill="violet", color="black")
+labs(x="Race",y="Frequency",title="Participants")

OUTPUT:
> x = c(3,6,9,12,15,18)
> length(x)
[1] 6
> summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
3.00 6.75 10.50 10.50 14.25 18.00
> table(x)
x
3 6 9 12 15 18
1 1 1 1 1 1
library(ggplot2)
library(mosaicData)
CODE:
#PIE CHART
library(ggplot2)
df<-data.frame(value = c(2,5,3,9) , group=paste0("G",1:4))
ggplot(df,aes(x=" ",y=value,fill=group))+geom_col(color="black")+coord_polar(theta="y")
ggplot(df,aes(x=" ",y=value,fill=group))+geom_col(color="black")
+geom_label(aes(label=value),position=position_stack(vjust=0.5),show.legend = FALSE)
+coord_polar(theta="y")

OUTPUT:
library(ggplot2)
CODE:
#HISTOGRAM
set.seed(1234)
df1 <- data.frame(sex =
factor(rep(c("F","M"),each=200)),weight=round(c(rnorm(200,mean=55,sd=5),rnorm(200,me
an=65,sd=5))))
df1
head(df1)

OUTPUT:
> set.seed(1234)
> head(df1)
sex weight
1 F 49
2 F 56
3 F 60
4 F 43
5 F 57
6 F 58

CODE:
#BASIC HISTOGRAM
ggplot(df1,aes(x=weight))+geom_histogram()
#change the width of the bins
ggplot(df1,aes(x=weight))+geom_histogram(binwidth=2)
#change clors
p <- ggplot(df1,aes(x=weight))
+geom_histogram(color="black",fill="lightgreen")
p
###Add mean line and density plot on the histogram
##Add mean line
p+geom_vline(aes(xintercept =
mean(weight)),color="blue",linetype="dashed",size=1)
#Histogram with density plot
ggplot(df1,aes(x=weight))+geom_histogram(aes(y
= ..density..),color="black",fill="white")
+geom_density(alpha=.2,fill="#FF6666")

OUTPUT:
> ggplot(df1,aes(x=weight))+geom_histogram()
`stat_bin()` using `bins = 30`. Pick better value with
`binwidth`.

> ggplot(df1,aes(x=weight))+geom_histogram(binwidth=2)
CODE:
###BIVARIATE PLOT
##STACKED BAR CHART
##Let's plot the relationship between automobile class and drive type
###(Front_wheel,rear_wheel,4_wheel drive) for the automobile in fuel economy dataset.
library(ggplot2)
#stacked bar chart
ggplot(mpg,aes(x=class,fill=drv))+geom_bar(position = "stack")

OUTPUT:
CODE:
###BIVARIATE PLOT
##GROUPED BAR CHART
##Let's plot the relationship between automobile class and drive type
###(Front_wheel,rear_wheel,4_wheel drive) for the automobile in fuel economy dataset.
library(ggplot2)
#grouped bar chart
ggplot(mpg,aes(x=class,fill=drv))+geom_bar(position = "dodge")

OUTPUT:
(B)WRITE PROGRAMS TO DEMONSTRATE PLOTTING CATEGORICAL AND TIME-SERIES DATA.
CODE:
###Time Series
#R Time series Analysis
#getting the data points in form of a R vector.
snowfall <- c(790,1170.8,860.1,1330.6,630.4,911.5,683.5,996.6,783.2,982,881.8,1021)
snowfall
#converting it into a time series object.
snowfall_timeseries <- ts(snowfall,start = c(2013,1),frequency = 12)
#printing the time series data
print(snowfall_timeseries)
#Giving a name to the chart file.
png(file="snowfall.png")
#plotting a graph of the time series
plot(snowfall_timeseries)
#saving the file
dev.off()

OUTPUT:
> snowfall
[1] 790.0 1170.8 860.1 1330.6 630.4 911.5 683.5 996.6
783.2 982.0 881.8 1021.0
> print(snowfall_timeseries)
Jan Feb Mar Apr May Jun Jul Aug
Sep Oct Nov Dec
2013 790.0 1170.8 860.1 1330.6 630.4 911.5 683.5 996.6
783.2 982.0 881.8 1021.0
> dev.off()
RStudioGD
2

You might also like