Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

ASSIGNMENT 2

DATA VISUALIZATION
UDAY AGARWAL

18BCE0777
1)
> Month=c("Jan","Feb","March","April","May","June")

> Rainfall=c(14.8,12.5,225,157,287,33)

> p=data.frame(Month,Rainfall)

>p

Month Rainfall

1 Jan 14.8

2 Feb 12.5

3 March 225.0

4 April 157.0

5 May 287.0

6 June 33.0

> pie(Rainfall,Month, main="Avg Rainfall", col= rainbow(length(Rainfall)))

 May has the highest amount of rainfall


 February has the lowest amount of rainfall.
2)
i)

> library(colormap)

> colormap = colormap(colormap = colormaps$viridis, nshades =47, format = "hex",


alpha = 1, reverse = TRUE)

> barplot(mtcars$mpg, col=colormap, main= "Miles per Gallon", xlab= "Values", ylab
= "Mpg")

ii)

> data("mtcars")

> colour <- gray.colors(nrow(mtcars), start = 0, end = 1, gamma = 2.2)

> gears <- mtcars$gear

> threegears = 0

> fourgears = 0

> fivegears = 0

> for (val in gears) {

+ if(val==3)

+ threegears = threegears+1
+ else if (val == 4)

+ fourgears = fourgears+1

+ else

+ fivegears = fivegears +1

+}

> values = c(threegears, fourgears, fivegears)

> numgears = c('3 gears', '4 gears', '5 gears')

> barplot(values, names.arg=numgears, xlab = 'No. of gears', ylab = 'Frequency', col =


c('white', 'black', 'white'), main="Gear Frequency", border = "Red")

iii)

> data("mtcars")

> colour <- gray.colors(nrow(mtcars), start = 0, end = 1, gamma = 2.2)

> cyl <- mtcars$cyl

> fourcyl = 0

> sixcyl = 0

> eightcyl = 0

> for (val in cyl) {

+ if(val==4)

+ fourcyl = fourcyl+1

+ else if (val == 6)
+ sixcyl = sixcyl+1

+ else

+ eightcyl = eightcyl +1

+}

> values = c(fourcyl, sixcyl, eightcyl)

> numcyl = c('4 Cylinders', '6 Cylinders', '8 Cylinders')

> pie(values, numcyl, col = colour)

iv)

> library(ggplot2)
> theme_set(theme_bw())
> data("mtcars")
> mtcars$`car name` <- rownames(mtcars)
> mtcars$mpg_z <- round((mtcars$mpg - mean(mtcars$mpg))/sd(mtcars$mpg), 2)
> mtcars$mpg_type <- ifelse(mtcars$mpg_z < 0, "below", "above")
> mtcars <- mtcars[order(mtcars$mpg_z), ]
> mtcars$`car name` <- factor(mtcars$`car name`, levels = mtcars$`car name`)
> ggplot(mtcars, aes(x=`car name`, y=mpg_z, xlab="Mileage")) +
+ geom_bar(stat='identity', aes(fill=mpg_type), width=.5) +
+ scale_fill_manual(name="Mileage",
+ labels = c("Above Average", "Below Average"),
+ values = c("above"="Red", "below"="Black")) +
+ labs(subtitle="Normalised mileage from 'mtcars'",
+ title= "Diverging Bars") +
+ coord_flip()
3)
> library(plotly)

> q=plot_ly(data= df, x= ~States,y = ~Population, type = "bar")

>

>q

> x= list(title="States")

> y= list(title="Population (in lakhs)")

> q=q %>% layout(xaxis=x, yaxis=y)

>q
4)
> x<-c("0","100","200","300","400","500")

> first<-c(0.98155,0.93184,0.85236,0.83138,0.74541,0.68018)

> second<-c(0.76069,0.76704,0.75307,0.75459,0.70204,0.70876)

> third<-c(0.977619,0.833065,0.718551,0.620307,0.578857,0.462435)

> fourth<-c(0.95326,0.86758,0.77488,0.78744,0.56428,0.0003)

> fifth<-c(0.89643,0.88747,0.77117,0.68177,0.64568,0.56176)

> sixth<-c(0.79457,0.78358,0.81168,0.78602,0.75865,0.69616)

> seventh<-c(0.9312,0.857898,0.767417,0.686024,0.522793,0.402)

> eighth<-c(1,0.86861,0.8833,0.45419,0.55793,0.302)

> ninth<-c(0.98765,0.93654,0.8866,0.83074,0.76175,0.70709)

> tenth<-c(0.812616,0.817037,0.809836,0.788359,0.813977,0.769617)

> eleventh<-c(0.99179,0.864673,0.778307,0.688372,0.596801,0.535633)

> twelfth<-c(0.93905,0.88222,0.84321,0.75361,0.67828,0.595)

> thirteenth<-c(0.96333,0.86725,0.81722,0.76547,0.69812,0.66298)

> fourteenth<-c(0.70786,0.714283,0.696162,0.686,0.687553,0.671429)

> fifteenth<-c(0.981183,0.872905,0.782988,0.689136,0.635597,0.566946)

> sixteenth<-c(0.99396,0.9302,0.83703,0.83658,0.73221,0.60853)

>data<data.frame(x,first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth,eleventh,t
welfth,thirteenth,fourteenth,fifteenth,sixteenth)

> fig<-plot_ly(data,x=~x,y=~first,type="bar",name="First group Algo 1")

> fig <- fig %>% add_trace(y =~second,name="First group Algo 2")

> fig <- fig %>% add_trace(y =~third,name="First group Algo 3")

> fig <- fig %>% add_trace(y =~fourth,name="First group Algo 4")

> fig <- fig %>% add_trace(y =~fifth,name="Second Group Algo 1")

> fig <- fig %>% add_trace(y =~sixth,name="Second Group Algo 2")

> fig <- fig %>% add_trace(y =~seventh,name="Second Group Algo 3")
> fig <- fig %>% add_trace(y =~eighth,name="Second Group Algo 4")

> fig <- fig %>% add_trace(y =~ninth,name="Third Group Algo 1")

> fig <- fig %>% add_trace(y =~tenth,name=" Third Group Algo 2")

> fig <- fig %>% add_trace(y =~eleventh,name=" Third Group Algo 3")

> fig <- fig %>% add_trace(y =~twelfth,name=" Third Group Algo 4")

> fig <- fig %>% add_trace(y =~thirteenth,name="Fourth Group Algo 1")

> fig <- fig %>% add_trace(y =~fourteenth,name=" Fourth Group Algo 2")

> fig <- fig %>% add_trace(y =~fifteenth,name=" Fourth Group Algo 3")

> fig <- fig %>% add_trace(y =~sixteenth,name=" Fourth Group Algo 4")

> x= list(title="Ping Nodes")

> y= list(title="Values")

> fig=fig %>% layout(xaxis=x, yaxis=y)

> fig

 We infer from this graph that as we increase the overlapping


ping nodes, the value of all the four algorithms decreases.
 There is no consistent pattern between the four algorithm’s
values with respect to one another with different no. of
overlapping ping nodes.

You might also like