packages <- c('tinytex','RColorBrewer','classInt','ggthemes','tidyverse')
for (p in packages){
if(!require(p,character.only =T)){
install.packages(p)
}
library(p, character.only = T)
}
#Importing Data
The codes below import “exam_data.csv” into R environment using “read_csv()” function of readr package.
exam_data <- read_csv("data/Exam_data.csv")
## Parsed with column specification:
## cols(
## ID = col_character(),
## CLASS = col_character(),
## GENDER = col_character(),
## RACE = col_character(),
## ENGLISH = col_double(),
## MATHS = col_double(),
## SCIENCE = col_double()
## )
#Essential Grammartical Elements in ggplot2 ##The ggplot function
Let us call the ggplot() function using the code below. #Geometric Objects: geom_histogram
ggplot(data=exam_data, aes(x=MATHS, fill=GENDER)) +
geom_histogram(bins=20,color="grey30")
#Geometric Objects: geom_point & geom_boxplot
#error message will be prompted if visualization used is not appropriate #boxplot x axis uses categorical data #histogram uses numerical for x axis
ggplot(data=exam_data, aes(y=MATHS, x=GENDER)) +
geom_boxplot()+
geom_point(position = "jitter", size=0.5)
#Geometric Objects: geom_dotplot
scale_y_continuous ( breaks =NULL) – removed the y axis
ggplot(data = exam_data, aes(x=MATHS)) +
geom_dotplot(binwidth=2.5,size=0.5) +
scale_y_continuous(NULL, breaks = NULL)
## Warning: Ignoring unknown parameters: size
#Working with facet_wrap()
“~” maps all the subgroups
ggplot(data=exam_data,aes(x=MATHS))+
geom_histogram(bins=20)+
facet_wrap(~CLASS)
#Working with facet_grid()
ggplot(data=exam_data,aes(x=MATHS))+
geom_histogram(bins=20)+
facet_grid(~CLASS)