Tuesday, June 19, 2018

Basics of R- Session 9- Data Visualization-3 using facets for adding layers in ggplot2


# Use of facet in ggplot for adding layers

ggplot(mbaper, aes(mbaper$Percentage_in_10_Class))+geom_dotplot()+facet_grid(~mbaper$Gender_MF)

ggplot(mbaper, aes(mbaper$Percentage_in_10_Class))+geom_dotplot()+facet_grid(~mbaper$Gender_MF+mbaper$Previous_Degree)

ggplot(mbaper, aes(mbaper$Percentage_in_10_Class))+geom_dotplot()+facet_grid(mbaper$Gender_MF~mbaper$Previous_Degree+mbaper$Place_you_belong_to)

ggplot(mbaper, aes(mbaper$perceivedscorecat))+geom_bar()+facet_grid(~mbaper$Gender_MF)

ggplot(mbaper, aes(mbaper$perceivedscorecat))+geom_bar()+facet_grid(~mbaper$Gender_MF+mbaper$Previous_Degree)

ggplot(mbaper, aes(mbaper$perceivedscorecat))+geom_bar()+facet_grid(mbaper$Gender_MF~mbaper$Previous_Degree+mbaper$Place_you_belong_to)

Basics of R- session 8- data visualization-2

one categorical and one scale variable
x- axis categorical, y axis scale
box plot



ggplot(mbaper, aes(mbaper$perceivedscorecat, mbaper$Percentage_in_12_Class))+geom_boxplot()
ggplot(mbaper, aes(mbaper$perceivedscorecat, mbaper$Percentage_in_12_Class))+geom_col()
ggplot(mbaper, aes(mbaper$perceivedscorecat, mbaper$Percentage_in_12_Class))+geom_count()
ggplot(mbaper, aes(mbaper$perceivedscorecat, mbaper$Percentage_in_12_Class))+geom_bin2d()
ggplot(mbaper, aes(mbaper$perceivedscorecat, mbaper$Percentage_in_12_Class))+geom_jitter()
ggplot(mbaper, aes(mbaper$perceivedscorecat, mbaper$Percentage_in_12_Class))+geom_violin()


one categorical and one scale variable
X axis scale, Y- axis categorical
box plot

ggplot(mbaper, aes(mbaper$Percentage_in_12_Class, mbaper$perceivedscorecat))+geom_bin2d()
ggplot(mbaper, aes(mbaper$Percentage_in_12_Class, mbaper$perceivedscorecat))+geom_jitter()

# better to have x axis as categorical

multiple variables
3 variables
2 scale and one categorical


ggplot(mbaper, aes(mbaper$Percentage_in_12_Class, mbaper$Percentage_in_10_Class))+geom_point()

#add more layer in terms of colour

ggplot(mbaper, aes(mbaper$Percentage_in_12_Class, mbaper$Percentage_in_10_Class))+geom_point(aes(color=perceivedscorecat))

ggplot(mbaper, aes(mbaper$Percentage_in_12_Class, mbaper$Percentage_in_10_Class))+geom_point(aes(color=Previous_Degree))

#add more layer in terms of shape

ggplot(mbaper, aes(mbaper$Percentage_in_12_Class, mbaper$Percentage_in_10_Class))+geom_point(aes(shape=perceivedscorecat))

# divide it into parts use facets
# facet_wrap()

ggplot(mbaper, aes(mbaper$Percentage_in_12_Class, mbaper$Percentage_in_10_Class))+geom_point()+facet_wrap(~perceivedscorecat)

#extra facet
ggplot(mbaper, aes(mbaper$Percentage_in_12_Class, mbaper$Percentage_in_10_Class))+geom_point()+facet_wrap(~perceivedscorecat+Marital_status)

ggplot(mbaper, aes(mbaper$Percentage_in_12_Class, mbaper$Percentage_in_10_Class))+geom_point()+facet_wrap(perceivedscorecat~Marital_status)

# we can add colour also

ggplot(mbaper, aes(mbaper$Percentage_in_12_Class, mbaper$Percentage_in_10_Class))+geom_point(aes(color=Previous_Degree))+facet_wrap(~perceivedscorecat)

Basics of R session 7- Data Visualization Mosaic Plot

Use library vcd
mosaic will be used for categorical variables
#Import the file- MBAdata.csv and save as an R object mbaper
# import the file from the location-

mbaper<-read.csv("D:/1 Teaching Material/R/importfile/MBAdata.csv")

library(vcd)
## Loading required package: grid
#here the data set used is mbaper, and the variable used is Marital_status

mosaic(~mbaper$Marital_status)
mosaic(~Marital_status, data= mbaper)
# here the output will be different in terms of labeling- the block name as the labels of the variable Marital_status, and the title will be the name of the data
Adding variables
mosaic(~mbaper$Gender+mbaper$Marital_status+mbaper$Place_you_belong_to)
mosaic(~Gender+Marital_status+Place_you_belong_to, data = mbaper)

cross tab mosaic

mosaic(mbaper$Gender~mbaper$Marital_status)

adding variables

mosaic(mbaper$Gender~mbaper$Marital_status+mbaper$perceivedscorecat)
mosaic(mbaper$Gender~mbaper$Marital_status+mbaper$perceivedscorecat+mbaper$Place_you_belong_to)
mosaic(mbaper$Gender~mbaper$Marital_status)
rotating the label for better visibility
mosaic(~mbaper$Gender+mbaper$Marital_status, labeling= labeling_border(rot_labels = c(45,45,45,45)))
if there are null blocks or no observation in the combination cell, nothing will be displayed if zero_size=0
mosaic(~mbaper$Gender+mbaper$STATE, zero_size= 0)
for colouring the mosaic, wth specific colour
mosaic(~mbaper$Marital_status, gp= gpar(fill=c("red", "green")))
different types of shading based on residual
mosaic(~mbaper$Gender+mbaper$Marital_status, gp = shading_hcl)
mosaic(~mbaper$Gender+mbaper$Marital_status, gp = shading_hsv)
mosaic(~mbaper$Gender+mbaper$Marital_status, gp = shading_max)
mosaic(~mbaper$Gender+mbaper$Marital_status, gp = shading_Friendly)
mosaic(~mbaper$Gender+mbaper$Marital_status, gp = shading_Friendly2)
mosaic(~mbaper$Gender+mbaper$Marital_status, gp = shading_sieve)
mosaic(~mbaper$Gender+mbaper$Marital_status, gp = shading_binary)