You are on page 1of 33

19BCE0761 | PARTH SHARMA

DATA VISUALIZATION

19BCE0761 | PARTH SHARMA

SLOT: F2

THEORY DA

-BRAMHAPUTRA DATA
A) Calculate long term mean in each of grid. Map the derived result into geographical structure with
suitable color palettes.

CODE:

## 19BCE0761 | PARTH SHARMA

setwd("C:/Users/parth/Desktop/dataviz_da/Data vis _lab")

par(mar = c(10,10,10,10))

#par(mfrow=c(1,4))

## Importing the required libraries

library(ggplot2)

library(grid)

library(gridExtra)

library(extrafont)

library(ggmap)

library(maptools)

library(plyr)

library(maps)

## Importing the dataset


19BCE0761 | PARTH SHARMA

p <- read.csv("Brahmputra_basin_data.csv")

t=dim(p)

mat<-matrix(0, nrow = t[2],ncol=1, byrow = FALSE)

for(i in 1:(t[2]))

## Calculating the monthly mean

total = sum(p[,i])

m <- total/(t[1]/12)

mat[i]<-m

write.csv(mat,'mean_sd.csv')

q<-read.csv("Bramhaputra_grid.csv")

chart<-list()

w <- c(746,1.55e+03,1.99e+03,2.25e+03,3.45e+03)

cols <- c("[746,1.55e+03)" = "red", "[1.55e+03,1.99e+03)" = "blue", "[1.99e+03,2.25e+03)" = "darkgreen",


"[2.25e+03,3.45e+03)" = "darkorange")

q$A1 <- cut(q$Mean,breaks = w,right = FALSE)

h <- ggplot(data = q, aes(x = x, y = y,fill = A1)) + geom_tile(aes()) + scale_colour_manual(values = cols,aesthetics =


c("colour", "fill")) + theme_classic()

River_B <- readShapeSpatial("11-9-2019-3772456")

ch <- geom_polygon(data=River_B, aes(x=long, y=lat, group=group),

colour="black", fill="white", alpha=0)

cha <- h+ch+xlab("Longitude")+ylab("Latitude") #+ labs(title= z[i])

cha <- cha+theme(text = element_text(family = "Times New Roman", size=16,

face = "bold"))
19BCE0761 | PARTH SHARMA

chart[[1]] <- h+ch+xlab("Longitude")+ylab("Latitude") + labs(title="Long term Mean of Brahmaputra River")

## Multiplot function

multiplot <- function(..., plotlist=NULL, cols) {

require(grid)

# Creating a list from the '...' arguments and plotlist

plots <- c(list(...), plotlist)

numPlots = length(plots)

# Making the panel

plotCols = cols # Number of columns of plots

plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols

# Setting up the page

grid.newpage()

pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))

vplayout <- function(x, y)

viewport(layout.pos.row = x, layout.pos.col = y)

# Make each plot, in the correct location

for (i in 1:numPlots) {

curRow = ceiling(i/plotCols)

curCol = (i-1) %% plotCols + 1

print(plots[[i]], vp = vplayout(curRow, curCol ))

tiff(filename = "Brahmputa_ltm.tiff", pointsize =8, res = 600, units = "in", width = 8, height = 3, restoreConsole =
TRUE, type = "cairo")

multiplot(chart[1], cols = 1)

dev.off()
19BCE0761 | PARTH SHARMA

jpeg(filename = "Brahmaputa_ltm.jpeg", pointsize =8, res = 600, units = "in", width = 8, height = 2, restoreConsole
= TRUE, type = "windows")

multiplot(chart[1], cols = 1)

dev.off()

OUTPUT:

The colours: red, green, blue and yellow are used to display the various mean of rainfall in different
regions of the Brahmaputra River.

The boundaries are provided in the shp file.

The Red area is the part which receives the lowest rainfall whereas Yellow region receives the most
rainfall.

B) Calculate the change in average annual rainfall with respect to global warming in each grid. Then
plot the derived result.

CODE:

## 19BCE0761 | PARTH SHARMA

setwd("C:/Users/parth/Desktop/dataviz_da/Data vis _lab")

par(mar = c(10,10,10,10))

#par(mfrow=c(1,4))

## Importing the required libraries


19BCE0761 | PARTH SHARMA

library(ggplot2)

library(grid)

library(gridExtra)

library(extrafont)

library(ggmap)

library(maptools)

library(plyr)

library(maps)

## Importing the dataset

p <- read.csv("Brahmputra_basin_data.csv")

t=dim(p)

mat<-matrix(0, nrow = t[2],ncol=3, byrow = FALSE)

for(i in 1:(t[2]))

sum1=0

sum2=0

for(j in 1:840){

sum1 =sum1+ p[j,i]

for(k in 841:t[1]){

sum2 =sum2+ p[k,i]

}
19BCE0761 | PARTH SHARMA

avg1 <- sum1/70

mat[i,1]<-avg1

avg2<- sum2/45

mat[i,2]<-avg2

mat[i,3] <- (avg1-avg2)

write.csv(mat,'Brahma_q2_mean.csv')

q<-read.csv("Brahma_q2_grid.csv")

chart<-list()

w <- c(-14,30,60,88,185)

cols <- c("[-14,30)" = "red", "[30,60)" = "blue", "[60,88)" = "darkgreen", "[88,185)" = "darkorange")

q$A1 <- cut(q$Mean,breaks = w,right = FALSE)

h <- ggplot(data = q, aes(x = x, y = y,fill = A1)) + geom_tile(aes()) + scale_colour_manual(values =


cols,aesthetics = c("colour", "fill"))+theme_classic()

River_B <- readShapeSpatial("11-9-2019-3772456")

ch <- geom_polygon(data=River_B, aes(x=long, y=lat, group=group), colour="black",

fill="white", alpha=0)

cha <- h+ch+xlab("Longitude")+ylab("Latitude") #+ labs(title= z[i])

cha <- cha+theme(text = element_text(family = "Times New Roman", size=16, face = "bold"))

chart[[1]] <- h+ch+xlab("Longitude")+ylab("Latitude") + labs(title="Change in avg annual rainfall of


Brahmaputra River due to global warming")

## Multiplot function

multiplot <- function(..., plotlist=NULL, cols) {


19BCE0761 | PARTH SHARMA

require(grid)

# Making a list from the '...' arguments and plotlist

plots <- c(list(...), plotlist)

numPlots = length(plots)

# Creating the panel

plotCols = cols # Number of columns of plots

plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols

# Setting up the page

grid.newpage()

pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))

vplayout <- function(x, y)

viewport(layout.pos.row = x, layout.pos.col = y)

# Making each plot, in the correct location

for (i in 1:numPlots) {

curRow = ceiling(i/plotCols)

curCol = (i-1) %% plotCols + 1

print(plots[[i]], vp = vplayout(curRow, curCol ))

tiff(filename = "Brahmputa_q2.tiff", pointsize =8, res = 600, units = "in", width = 8, height = 5,
restoreConsole = TRUE, type = "cairo")

multiplot(chart[1], cols = 1)

dev.off()

jpeg(filename = "Brahmaputa_q2.jpeg", pointsize =8, res = 600, units = "in", width =8, height = 5,
restoreConsole = TRUE, type = "windows")
19BCE0761 | PARTH SHARMA

multiplot(chart[1], cols = 1)

dev.off()

OUTPUT:

C) Calculate the change in seasonal average rainfall with respect to global warming in each of grids.
Plot the derived results.

CODE:

## 19BCE0761 | PARTH SHARMA

setwd("C:/Users/parth/Desktop/dataviz_da/Data vis _lab")

par(mar = c(10,10,10,10))

#par(mfrow=c(1,4))

## Importing all required libraries


19BCE0761 | PARTH SHARMA

library(ggplot2)

library(grid)

library(gridExtra)

library(extrafont)

library(ggmap)

library(maptools)

library(plyr)

library(maps)

## Importing the dataset

p <- read.csv("Brahmputra_basin_data.csv")

t=dim(p)

mat<-matrix(0, nrow = t[2],ncol=3, byrow = FALSE)

for(i in 1:(t[2]))

sum1=0

sum2=0

x=6

while(x<840)

for(j in x:(x+3)){

sum1 =sum1+ p[j,i]

}
19BCE0761 | PARTH SHARMA

x=x+12

y=846

while(y<t[1])

for(k in y:(y+3)){

sum2 =sum2+ p[k,i]

y=y+12

avg1 <- sum1/70

mat[i,1]<-avg1

avg2<- sum2/45

mat[i,2]<-avg2

mat[i,3] <- (avg1-avg2)

write.csv(mat,'Brahma_q3_mean.csv')

q<-read.csv("Brahma_q3_grid.csv")

chart<-list()

w <- c(21,54,67,80,205)

cols <- c("[21,54)" = "red", "[54,67)" = "blue", "[67,80)" = "darkgreen", "[80,205)" = "darkorange")

q$A1 <- cut(q$Mean,breaks = w,right = FALSE)

h <- ggplot(data = q, aes(x = x, y = y,fill = A1)) + geom_tile(aes()) + scale_colour_manual(values =


cols,aesthetics = c("colour", "fill"))+theme_classic()

River_B <- readShapeSpatial("11-9-2019-3772456")


19BCE0761 | PARTH SHARMA

ch <- geom_polygon(data=River_B, aes(x=long, y=lat, group=group), colour="black", fill="white",


alpha=0)

cha <- h+ch+xlab("Longitude")+ylab("Latitude") #+ labs(title= z[i])

cha <- cha+theme(text = element_text(family = "Times New Roman", size=16, face = "bold"))

chart[[1]] <- h+ch+xlab("Longitude")+ylab("Latitude") + labs(title="Change in avg seasonal rainfall of


Brahmaputra River due to global warming")

## Multiplot function

multiplot <- function(..., plotlist=NULL, cols) {

require(grid)

# Making a list from the '...' arguments and plotlist

plots <- c(list(...), plotlist)

numPlots = length(plots)

# Making the panel plotCols = cols

# Number of columns of plots plotRows = ceiling(numPlots/plotCols)

# Number of rows needed, calculated from # of cols

# Setting up the page

grid.newpage()

pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))

vplayout <- function(x, y)

viewport(layout.pos.row = x, layout.pos.col = y)

# Creating each plot, in the correct location

for (i in 1:numPlots) {

curRow = ceiling(i/plotCols)

curCol = (i-1) %% plotCols + 1

print(plots[[i]], vp = vplayout(curRow, curCol ))


19BCE0761 | PARTH SHARMA

tiff(filename = "Brahmputa_q3.tiff", pointsize =8, res = 600, units = "in", width = 8, height = 5,
restoreConsole = TRUE, type = "cairo")

multiplot(chart[1], cols = 1)

dev.off()

jpeg(filename = "Brahmaputa_q3.jpeg", pointsize =8, res = 600, units = "in", width = 8, height = 5,
restoreConsole = TRUE, type = "windows")

multiplot(chart[1], cols = 1)

dev.off()

OUTPUT:
19BCE0761 | PARTH SHARMA

D) Calculate the average seasonal (June-Sept) rainfall in each grid for the period 1901-1920. Similarly
calculate the average seasonal (June-Sept) rainfall in each grid for the period 1990-last year. Calculate
the change in average seasonal and plot them.

CODE:

## 19BCE0761 | PARTH SHARMA

setwd("C:/Users/parth/Desktop/dataviz_da/Data vis _lab")

par(mar = c(10,10,10,10))

#par(mfrow=c(1,4))

## Importing all required libraries

library(ggplot2)

library(grid)

library(gridExtra)

library(extrafont)

library(ggmap)

library(maptools)

library(plyr)

library(maps)

## Importing the dataset

p <- read.csv("Brahmputra_basin_data.csv")

t=dim(p)

mat<-matrix(0, nrow = t[2],ncol=3, byrow = FALSE)

for(i in 1:(t[2]))

{
19BCE0761 | PARTH SHARMA

sum1=0

sum2=0

x=6

while(x<240)

for(j in x:(x+3)){

sum1 =sum1+ p[j,i]

x=x+12

y=1086

while(y<t[1])

for(k in y:(y+3)){

sum2 =sum2+ p[k,i]

y=y+12

avg1 <- sum1/20

mat[i,1]<-avg1

avg2<- sum2/25

mat[i,2]<-avg2

mat[i,3] <- (avg1-avg2)

}
19BCE0761 | PARTH SHARMA

write.csv(mat,'Brahma_q4_mean.csv')

q<-read.csv("Brahma_q4_grid.csv")

chart<-list()

w <- c(7,49,82,114,238)

cols <- c("[7,49)" = "red", "[49,82)" = "blue", "[82,114)" = "darkgreen", "[114,238)" = "darkorange")

q$A1 <- cut(q$Mean,breaks = w,right = FALSE)

h <- ggplot(data = q, aes(x = x, y = y,fill = A1)) + geom_tile(aes()) + scale_colour_manual(values =


cols,aesthetics = c("colour", "fill"))+theme_classic()

River_B <- readShapeSpatial("11-9-2019-3772456")

ch <- geom_polygon(data=River_B, aes(x=long, y=lat, group=group), colour="black", fill="white",


alpha=0)

cha <- h+ch+xlab("Longitude")+ylab("Latitude") #+ labs(title= z[i])

cha <- cha+theme(text = element_text(family = "Times New Roman", size=16, face = "bold"))

chart[[1]] <- h+ch+xlab("Longitude")+ylab("Latitude") + labs(title="Change in avg seasonal rainfall of


Brahmaputra River between 1901-20 and 1990-2015")

## Multiplot function

multiplot <- function(..., plotlist=NULL, cols) {

require(grid)

# Make a list from the '...' arguments and plotlist

plots <- c(list(...), plotlist)

numPlots = length(plots)

# Make the panel

plotCols = cols # Number of columns of plots

plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols

# Setting up the page

grid.newpage()
19BCE0761 | PARTH SHARMA

pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))

vplayout <- function(x, y)

viewport(layout.pos.row = x, layout.pos.col = y)

# Make each plot, in the correct location

for (i in 1:numPlots) {

curRow = ceiling(i/plotCols)

curCol = (i-1) %% plotCols + 1

print(plots[[i]], vp = vplayout(curRow, curCol ))

tiff(filename = "Brahmputa_q4.tiff", pointsize =8, res = 600, units = "in", width = 8, height = 5,
restoreConsole = TRUE, type = "cairo")

multiplot(chart[1], cols = 1)

dev.off()

jpeg(filename = "Brahmaputa_q4.jpeg", pointsize =8, res = 600, units = "in", width = 8, height = 5,
restoreConsole = TRUE, type = "windows")

multiplot(chart[1], cols = 1)

dev.off()

OUTPUT:
19BCE0761 | PARTH SHARMA

-INDIA DATA
A) Calculate long term mean in each of grid. Map the derived result into geographical structure with
suitable color palettes.

CODE:

## 19BCE0761 | PARTH SHARMA

setwd("C:/Users/parth/Desktop/dataviz_da/Data vis _lab")

par(mar = c(10,10,10,10))

#par(mfrow=c(1,4))

## Importing required libraries

library(ggplot2)

library(grid)

library(gridExtra)
19BCE0761 | PARTH SHARMA

library(extrafont)

library(ggmap)

library(maptools)

library(plyr)

library(maps)

## Importing the dataset

p <- read.csv("India_gridded_data.csv")

t=dim(p)

mat<-matrix(0, nrow = t[2],ncol=1, byrow = FALSE)

for(i in 1:(t[2]))

## Calculating monthly mean

total = sum(p[,i])

m <- total/(t[1]/12)

mat[i]<-m

write.csv(mat,'India_q1_mean.csv')

q<-read.csv("India_q1_grid.csv")

chart<-list()

w <- c(62,676,978,1.32e+03,4.34e+03)

cols <- c("[62,676)" = "red", "[676,978)" = "blue", "[978,1.32e+03)" = "darkgreen",


"[1.32e+03,4.34e+03)" = "darkorange")

q$A1 <- cut(q$Mean,breaks = w,right = FALSE)

h <- ggplot(data = q, aes(x = x, y = y,fill = A1)) + geom_tile(aes()) + scale_colour_manual(values =


cols,aesthetics = c("colour", "fill"))+theme_classic()
19BCE0761 | PARTH SHARMA

River_B <- readShapeSpatial("Ind")

ch <- geom_polygon(data=River_B, aes(x=long, y=lat, group=group), colour="black", fill="white",


alpha=0)

cha <- h+ch+xlab("Longitude")+ylab("Latitude") #+ labs(title= z[i])

cha <- cha+theme(text = element_text(family = "Times New Roman", size=16, face = "bold"))

chart[[1]] <- h+ch+xlab("Longitude")+ylab("Latitude") + labs(title="Long term Mean of rainfall in India")

## Multiplot function

multiplot <- function(..., plotlist=NULL, cols) {

require(grid)

# Making a list from the ... arguments and plotlist

plots <- c(list(...), plotlist)

numPlots = length(plots)

# Making the panel

plotCols = cols # Number of columns of plots

plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from #

of cols

# Setting up the page

grid.newpage()

pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))

vplayout <- function(x, y)

viewport(layout.pos.row = x, layout.pos.col = y)

# Creating each plot, in the correct location

for (i in 1:numPlots) {

curRow = ceiling(i/plotCols)

curCol = (i-1) %% plotCols + 1


19BCE0761 | PARTH SHARMA

print(plots[[i]], vp = vplayout(curRow, curCol ))

tiff(filename = "India_q1.tiff", pointsize =8, res = 600, units = "in", width = 8, height = 8, restoreConsole =
TRUE, type = "cairo")

multiplot(chart[1], cols = 1)

dev.off()

jpeg(filename = "India_q1.jpeg", pointsize =8, res = 600, units = "in", width = 8, height = 8,
restoreConsole = TRUE, type = "windows")

multiplot(chart[1], cols = 1)

dev.off()

OUTPUT:
19BCE0761 | PARTH SHARMA

B) Calculate the change in average annual rainfall with respect to global warming in each grid. Then
plot the derived result.

CODE:

## 19BCE0761 | PARTH SHARMA

setwd("C:/Users/parth/Desktop/dataviz_da/Data vis _lab")

par(mar = c(10,10,10,10))

#par(mfrow=c(1,4))

## Importing required libraries

library(ggplot2)

library(grid)

library(gridExtra)

library(extrafont)

library(ggmap)

library(maptools)

library(plyr)

library(maps)

## Importing the dataset

p <- read.csv("India_gridded_data.csv")

t=dim(p)

mat<-matrix(0, nrow = t[2],ncol=3, byrow = FALSE)

for(i in 1:(t[2]))

sum1=0
19BCE0761 | PARTH SHARMA

sum2=0

for(j in 1:840){

sum1 =sum1+ p[j,i]

for(k in 841:t[1]){

sum2 =sum2+ p[k,i]

avg1 <- sum1/70

mat[i,1]<-avg1

avg2<- sum2/44

mat[i,2]<-avg2

mat[i,3] <- (avg1-avg2)

write.csv(mat,'India_q2_mean.csv')

q<-read.csv("India_q2_grid.csv")

chart<-list()

w <- c(-215,-34,-2,67,277)

cols <- c("[-215,-34)" = "red", "[-34,-2)" = "blue", "[-2,67)" = "darkgreen", "[67,277)" = "darkorange")

q$A1 <- cut(q$Mean,breaks = w,right = FALSE)

h <- ggplot(data = q, aes(x = x, y = y,fill = A1)) + geom_tile(aes()) + scale_colour_manual(values =


cols,aesthetics = c("colour", "fill"))+theme_classic()

River_B <- readShapeSpatial("Ind")

ch <- geom_polygon(data=River_B, aes(x=long, y=lat, group=group), colour="black",

fill="white", alpha=0)

cha <- h+ch+xlab("Longitude")+ylab("Latitude") #+ labs(title= z[i])


19BCE0761 | PARTH SHARMA

cha <- cha+theme(text = element_text(family = "Times New Roman", size=16, face = "bold"))

chart[[1]] <- h+ch+xlab("Longitude")+ylab("Latitude") + labs(title="Change in avg annual rainfall in India


due to global warming")

## Multiplot function

multiplot <- function(..., plotlist=NULL, cols) {

require(grid)

# Making a list from the '...' arguments and plotlist

plots <- c(list(...), plotlist)

numPlots = length(plots)

# Making the panel

plotCols = cols # Number of columns of plots

plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from #of cols

# Setting up the pagegrid.newpage()

pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))

vplayout <- function(x, y)

viewport(layout.pos.row = x, layout.pos.col = y)

# Creating each plot, in the correct location

for (i in 1:numPlots) {

curRow = ceiling(i/plotCols)

curCol = (i-1) %% plotCols + 1

print(plots[[i]], vp = vplayout(curRow, curCol ))

}
19BCE0761 | PARTH SHARMA

tiff(filename = "India_q2.tiff", pointsize =8, res = 600, units = "in", width = 8, height = 8, restoreConsole =
TRUE, type = "cairo")

multiplot(chart[1], cols = 1)

dev.off()

jpeg(filename = "India_q2.jpeg", pointsize =8, res = 600, units = "in", width = 8, height = 8,
restoreConsole = TRUE, type = "windows")

multiplot(chart[1], cols = 1)

dev.off()

OUTPUT:
19BCE0761 | PARTH SHARMA

C) Calculate the change in seasonal average rainfall with respect to global warming in each of grids.
Plot the derived results.

CODE:

## 19BCE0761 | PARTH SHARMA

setwd("C:/Users/parth/Desktop/dataviz_da/Data vis _lab")

par(mar = c(10,10,10,10))

#par(mfrow=c(1,4))

## Importing all required libraries

library(ggplot2)

library(grid)

library(gridExtra)

library(extrafont)

library(ggmap)

library(maptools)

library(plyr)

library(maps)

## Importing the dataset

p <- read.csv("India_gridded_data.csv")

t=dim(p)

mat<-matrix(0, nrow = t[2],ncol=3, byrow = FALSE)

for(i in 1:(t[2]))

sum1=0
19BCE0761 | PARTH SHARMA

sum2=0

x=6

while(x<840)

for(j in x:(x+3)){

sum1 =sum1+ p[j,i]

x=x+12

y=846

while(y<t[1])

for(k in y:(y+3)){

sum2 =sum2+ p[k,i]

y=y+12

avg1 <- sum1/70

mat[i,1]<-avg1

avg2<- sum2/44

mat[i,2]<-avg2

mat[i,3] <- (avg1-avg2)

write.csv(mat,'India_q3_mean.csv')
19BCE0761 | PARTH SHARMA

q<-read.csv("India_q3_grid.csv")

chart<-list()

w <- c(-197,-20,9,59,275)

cols <- c("[-197,-20)" = "red", "[-20,9)" = "blue", "[9,59)" = "darkgreen", "[59,275)" = "darkorange")

q$A1 <- cut(q$Mean,breaks = w,right = FALSE)

h <- ggplot(data = q, aes(x = x, y = y,fill = A1)) + geom_tile(aes()) + scale_colour_manual(values =


cols,aesthetics = c("colour", "fill"))+theme_classic()

River_B <- readShapeSpatial("Ind")

ch <- geom_polygon(data=River_B, aes(x=long, y=lat, group=group), colour="black", fill="white",


alpha=0)

cha <- h+ch+xlab("Longitude")+ylab("Latitude") #+ labs(title= z[i])

cha <- cha+theme(text = element_text(family = "Times New Roman", size=16, face = "bold"))

chart[[1]] <- h+ch+xlab("Longitude")+ylab("Latitude") + labs(title="Change in avg seasonal rainfall in


India due to global warming")

## Multiplot function

multiplot <- function(..., plotlist=NULL, cols) {

require(grid)

# Making a list from the '...' arguments and plotlist

plots <- c(list(...), plotlist)

numPlots = length(plots)

# Making the panel

plotCols = cols # Number of columns of plots

plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols

# Setting up the page

grid.newpage()

pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))


19BCE0761 | PARTH SHARMA

vplayout <- function(x, y)

viewport(layout.pos.row = x, layout.pos.col = y)

# Creating each plot, in the correct location

for (i in 1:numPlots) {

curRow = ceiling(i/plotCols)

curCol = (i-1) %% plotCols + 1

print(plots[[i]], vp = vplayout(curRow, curCol ))

tiff(filename = "India_q3.tiff", pointsize =8, res = 600, units = "in", width = 8, height = 8, restoreConsole =
TRUE, type = "cairo")

multiplot(chart[1], cols = 1)

dev.off()

jpeg(filename = "India_q3.jpeg", pointsize =8, res = 600, units = "in", width = 8, height = 8,
restoreConsole = TRUE, type = "windows")

multiplot(chart[1], cols = 1)

dev.off()
19BCE0761 | PARTH SHARMA

OUTPUT:

D) Calculate the average seasonal (June-Sept) rainfall in each grid for the period 1901-1920. Similarly
calculate the average seasonal (June-Sept) rainfall in each grid for the period 1990-last year. Calculate
the change in average seasonal and plot them.

CODE:

## 19BCE0761 | PARTH SHARMA

setwd("C:/Users/parth/Desktop/dataviz_da/Data vis _lab")

par(mar = c(10,10,10,10))

#par(mfrow=c(1,4))
19BCE0761 | PARTH SHARMA

## Importing all required libraries

library(ggplot2)

library(grid)

library(gridExtra)

library(extrafont)

library(ggmap)

library(maptools)

library(plyr)

library(maps)

## Importing the dataset

p <- read.csv("India_gridded_data.csv")

t=dim(p)

mat<-matrix(0, nrow = t[2],ncol=3, byrow = FALSE)

for(i in 1:(t[2]))

sum1=0

sum2=0

x=6

while(x<240)

for(j in x:(x+3)){

sum1 =sum1+ p[j,i]

}
19BCE0761 | PARTH SHARMA

x=x+12

y=1086

while(y<t[1])

for(k in y:(y+3)){

sum2 =sum2+ p[k,i]

y=y+12

avg1 <- sum1/20

mat[i,1]<-avg1

avg2<- sum2/24

mat[i,2]<-avg2

mat[i,3] <- (avg1-avg2)

write.csv(mat,'India_q4_mean.csv')

q<-read.csv("India_q4_grid.csv")chart<-list()

w <- c(-492,-48,-5,65,314)

cols <- c("[-492,-48)" = "red", "[-48,-5)" = "blue", "[-5,65)" = "darkgreen", "[65,314)" = "darkorange")

q$A1 <- cut(q$Mean,breaks = w,right = FALSE)

h <- ggplot(data = q, aes(x = x, y = y,fill = A1)) + geom_tile(aes()) + scale_colour_manual(values =


cols,aesthetics = c("colour", "fill"))+theme_classic()

River_B <- readShapeSpatial("Ind")

ch <- geom_polygon(data=River_B, aes(x=long, y=lat, group=group), colour="black",


19BCE0761 | PARTH SHARMA

fill="white", alpha=0)

cha <- h+ch+xlab("Longitude")+ylab("Latitude") #+ labs(title= z[i])

cha <- cha+theme(text = element_text(family = "Times New Roman", size=16, face = "bold"))

chart[[1]] <- h+ch+xlab("Longitude")+ylab("Latitude") + labs(title="Change in avg seasonal rainfall in


India between 1901-20 and 1990-2015")

## Multiplot function

multiplot <- function(..., plotlist=NULL, cols) {

require(grid)

# Making a list from the '...' arguments and plotlist

plots <- c(list(...), plotlist)

numPlots = length(plots)

# Making the panel

plotCols = cols # Number of columns of plots

plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols

# Setting up the page

grid.newpage()pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))

vplayout <- function(x, y)

viewport(layout.pos.row = x, layout.pos.col = y)

# Creating each plot, in the correct location

for (i in 1:numPlots) {

curRow = ceiling(i/plotCols)

curCol = (i-1) %% plotCols + 1

print(plots[[i]], vp = vplayout(curRow, curCol ))

}
19BCE0761 | PARTH SHARMA

tiff(filename = "India_q4.tiff", pointsize =8, res = 600, units = "in", width = 8, height = 8, restoreConsole =
TRUE, type = "cairo")

multiplot(chart[1], cols = 1)

dev.off()

jpeg(filename = "India_q4.jpeg", pointsize =8, res = 600, units = "in", width = 8, height = 8,
restoreConsole = TRUE, type = "windows")

multiplot(chart[1], cols = 1)

dev.off()

OUTPUT:

You might also like