You are on page 1of 9

Venkata Raman B, 20BCE1499

CSE3020 - Data Visualization


Lab 2
Constructing the various visual analytics charts using Lattice and ggplot2 in R – Practice
Problem

For hsb2.csv dataset:

hsb2 <- read.table('C:/Users/VENKATARAMAN


B/Downloads/Lab-2_LatticePlot/Lab-2_LatticePlot/hsb2.csv',
header=T, sep=",")
attach(hsb2)
library(lattice)
#defining ses.f to be a factor variable
hsb2$ses.f = factor(hsb2$ses, labels=c("low", "middle", "high"))
#histograms
histogram(~write, hsb2)

#conditional plot
histogram(~write | ses.f, hsb2)
#density plot
densityplot(~socst, hsb2)

#conditional plot
densityplot(~socst | ses.f, hsb2)

#Quantile-quantile plots
qqmath(~write, hsb2)
Q) Using iris dataset

library(lattice)
data("iris")
#Creating dataset
df<-iris
df

col= c("length", "width", "species", "SP")


de<- data.frame(matrix(nrow = 0, ncol = length(col)))
colnames(de) <- col
temp<- df[c(1,2,5)]
colnames(temp)= c("length", "width", "species")
temp <- cbind(temp, SP="Sepal")
de <- rbind(de, temp)
temp<- df[c(3,4,5)]
colnames(temp)= c("length", "width", "species")
temp <- cbind(temp, SP="Petal")
de <- rbind(de, temp)
de

#Factoring
de$species.f = factor(de$species, labels=c("setosa", "versicolor", "virginica"))
de$SP.f = factor(de$SP, labels=c("Sepal", "Petal"))
#Scatterplot
xyplot(length~width | species.f, group=SP, data=de, layout=c(3,1),
main= "Scatterplot",
auto.key = list(title = "Structure", cex.title=1.2, space = "right"),
par.settings= list(
superpose.symbol = list(
pch = 21, cex = 1, col = c('#f8766d','#00bfc4'), fill= c('#f8766d','#00
bfc4')
)
),
xlab= "Width", ylab="Length"
)
#BoxPlot
bw_lattice <- bwplot(length ~ species | SP.f, data = de, main= "BoxPlot", xlab= "S
pecies", ylab="Length")
bw_theme <- trellis.par.get()
bw_theme$box.dot$cex <- 1
bw_theme$box.dot$pch <- "|"
bw_theme$box.rectangle$col <- "black"
bw_theme$box.rectangle$lwd <- 1
bw_theme$box.rectangle$fill <- "white"
bw_theme$box.umbrella$lty <- 1
bw_theme$box.umbrella$col <- "black"
bw_theme$plot.symbol$pch <- 16
bw_theme$plot.symbol$col <- "black"
bw_theme$strip.background$col <- "orange"
bw_theme$strip.title$col <- "orange"
update(bw_lattice, par.settings = bw_theme)
#violinplot
bwplot(
width ~ species | SP,
de,
panel = function
(...) {
panel.grid(-
1,-
1
)
panel.violin(...
)
},
par.settings = list(
strip.background = list(col="black"
)
),
col="white" )
#histogram
histogram(~length | SP*species, de, col="azure4", border = F, panel = function(...) {
panel.grid(-1,-1) panel.histogram(...) }, par.settings = list( strip.background = list(col="green") ))

You might also like