knitr::opts_chunk$set(echo = TRUE , cache = TRUE)

#Packages

install.packages("ggplot2")
library (ggplot2)
library (patchwork)
library (esquisse)
library (CGPfunctions)
library ( dplyr)
library (tinytex)
library (ggthemes)
library(tidyr)
library (rmarkdown)
library (knitr)
library (ggplotAssist)
library (forcats)
library (readxl)
library (ggpubr)
if (!require(scales)) {
  install.packages("scales", repos = "https://cloud.r-project.org")
  library(scales)
}

#header 1

data (iris)

ggplot(data = iris , aes (x = Sepal.Length , y = Sepal.Length)) + 
  geom_point ()

#scatter plot

ggplot (data = iris) + geom_point(mapping = aes (x= Sepal.Length, y = Sepal.Width )) +
  labs ( title = "Scatter plot of Sepal.Length and Sepal.Width", 
         x = " Sepal Length", y= "Sepal Width")

ggplot (data = iris , aes ( x= Sepal.Length , y = Sepal.Width)) + 
  geom_point () +
facet_wrap ( ~ Species)

ggplot (data = iris , aes ( x= Sepal.Length , y = Sepal.Width)) + 
  geom_point (col = "violetred4")

ggplot (data = iris , aes ( x= Sepal.Length , y = Sepal.Width)) + 
  geom_point ( size= 3)

ggplot (data = iris , aes ( x= Sepal.Length , y = Sepal.Width)) + 
  geom_point (Shape = 15)
## Warning in geom_point(Shape = 15): Ignoring unknown parameters: `Shape`

ggplot (data = iris , aes ( x= Sepal.Length , y = Sepal.Width)) + 
  geom_point (shape = "diamond filled")

ggplot (data = iris , aes ( x= Sepal.Length , y = Sepal.Width, col = Species)) + 
  geom_point ()

ggplot (data = iris , aes ( x= Sepal.Length , y = Sepal.Width , col = Species)) + 
  geom_point () + labs ( x= "Sepal Length", y = "Sepal Width", col= "Species", title = "Scatter plot of sepal length vs width")

#histogram

ggplot (data = iris , aes ( x= Sepal.Length)) +
  geom_histogram (binwidth =1 , fill = "lightblue" , col = "black")

ggplot (data = iris) +  geom_histogram( aes ( x= Sepal.Length), bins = 10, fill = "lightblue", col = "black") 

ggplot (data = iris) +  geom_histogram( aes ( x= Sepal.Length), bins = 10, fill = "lightblue", col = "white") 

ggplot (data = iris) +  geom_histogram( aes ( x= Sepal.Length ,fill = Species),bins = 10,  col = "white") 

ggplot (data = iris) +  geom_histogram( aes ( x= Sepal.Length ,fill = Species),bins = 10,  col = "white" , alpha = 0.6) 

#Extra

res <- sample (1:100 , 10)
res
##  [1] 56 72 23  5 38 45 82 17 70 93
sum (res)
## [1] 501

The total of the two number are 501

#Reduce gap between plot and Axis

ggplot (data = iris) +
  geom_histogram(
    aes (x = Sepal.Length , fill = Species),
    bins = 10 ,
    col = "white",
    alpha = 0.5
  ) +
  coord_cartesian (expand = FALSE)

ggplot(data = iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width)) +
  labs(
    title = "Scatter plot of Sepal.Length and Sepal.Width",
    x = "Sepal Length",
    y = "Sepal Width"
  ) +
  scale_y_continuous(expand = expansion(mult = c(0, 0), add = c(10, 0)))

#Facet #facet_wrap

ggplot ( data = iris)+
  geom_histogram( aes (x = Sepal.Length ,fill = Species), bins = 10, col = "white", alpha = 0.5) +
  facet_wrap (vars (Species), ncol = 1)

ggplot ( data = iris)+
  geom_histogram( aes (x = Sepal.Length ,fill = Species), bins = 10, col = "white", alpha = 0.5) +
  facet_wrap (vars (Species),scales = "free")

ggplot ( data = iris)+
  geom_histogram( aes (x = Sepal.Length ,fill = Species), bins = 10, col = "white", alpha = 0.5) +
  facet_wrap (vars (Species),scales = "free_y")

#facet_gird

ggplot (data = iris) +
  geom_histogram(aes (x = Sepal.Length , fill = Species), bins = 10, col = "white", alpha = 0.5) + facet_grid( rows= vars (Species))

student <- readxl::read_excel ("student_data.xlsx")

ggplot(data = student) +
  geom_histogram(aes(x = GPA, fill = ID),
                 bins = 10, col = "white", alpha = 0.5) +
  facet_grid(rows = vars(Number), cols = vars(Name))

#theme

#Built in theme

ggplot (data = iris) +
  geom_histogram(aes (x = Sepal.Length , fill  = Species), bins = 10, col = "white", alpha = 0.5) + facet_wrap( vars(Species), ncol = 1)+ theme_minimal ()

ggplot (data = iris) +
  geom_histogram(aes (x = Sepal.Length , fill  = Species), bins = 10, col = "white", alpha = 0.5) + facet_wrap( vars(Species), ncol = 1)+ theme_bw()

ggplot (data = iris) +
  geom_histogram(aes (x = Sepal.Length , fill  = Species), bins = 10, col = "white", alpha = 0.5) + facet_wrap( vars(Species), ncol = 1)+ theme_classic ()

#Themes from other packages

ggplot (data = iris) +
  geom_histogram(aes (x = Sepal.Length , fill  = Species), bins = 10, col = "white", alpha = 0.5) + facet_wrap( vars(Species), ncol = 1 ) +
  theme_base()

p1 <- ggplot(data = iris) +
  geom_histogram(aes (x = Sepal.Length , fill = Species), bins = 10 , col = "white" , alpha =0.5) + facet_wrap (vars (Species), ncol = 1) +
  labs (
    title = "Histogram of Sepal Length by Species", 
    x = "Sepal.Length",
    y = "Frequency",
    fill = "Species",
    subtitle =  "Using Facet and other customizations", 
    caption = "Data : Iris"
  )

#set theme globally

theme_set( theme_bw())

#manually set colors

ggplot (data =iris) +
  geom_histogram(aes (x = Sepal.Length , fill = Species), bins = 10 , col ="white" , alpha =0.5) + facet_wrap(vars (Species) , ncol =1) + scale_fill_manual(values = c("setosa" = "#601C80", "versicolor" = "#30A19C", "virginica" = "#123B96"))

ggplot (data =iris) +
  geom_histogram(aes (x = Sepal.Length , fill = Species), bins = 10 , col ="white" , alpha =0.5) + facet_wrap(vars (Species) , ncol =1) + scale_fill_brewer(palette = "Set1")

ggplot (data =iris) +
  geom_histogram(aes (x = Sepal.Length , fill = Species), bins = 10 , col ="white" , alpha =0.5) + facet_wrap(vars (Species) , ncol =1) + scale_fill_hue(
    l = 80, c =150,   #adjust luminosity and chroma
    h = c (90, 360)  #adjust range of hues
  )

#Density plot

ggplot (data =iris) +
  geom_density(aes (x = Sepal.Length , fill = Species), alpha = 0.5)

ggplot (data =iris) +
  geom_density(aes (x = Sepal.Length , fill = Species), alpha = 0.5) + facet_wrap (vars (Species), ncol = 1)

#Histogram + Density Plot

ggplot (data = iris, aes ( x = Sepal.Length , fill = Species)) + geom_density(alpha = 0.5 , color = "white") + geom_histogram( aes ( y = after_stat (density)), alpha = 0.5 , bins = 10) + facet_wrap( vars ( Species), ncol = 1)

ggplot (iris , aes ( Sepal.Length))+ geom_histogram(aes (y = after_stat (density)), color = "#000000" , fill = "#0099F8")+
  geom_density(color = "#000000" , fill = "#0EE100" , alpha = 0.5) + geom_vline ( aes (xintercept = mean (Sepal.Length)), color = "#000000" , size =1 , linetype = "dashed") + labs (
    title = "Distribution of Sepal Length", 
    subtitle = "Made by ggplot2",
    caption = "Source : Iris Data", 
    x = "Sepal Length",
    y = "Density"
  ) +
  theme_classic () +
  theme (
    plot.title = element_text (color = "blue" , size = 16, face = "bold"),
    plot.subtitle = element_text ( size = 10),
    plot.caption = element_text ( face = "italic")
  ) +
  annotate("text" , x = 5.9, y= 0.45,
           label = paste0("Mode: " ,round (DescTools::Mode(iris$Sepal.Length)[1],1)), hjust= 0 )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#geom_bar

Student <- read_excel("student_devices.xlsx")

ggplot(Student) +
  geom_bar (aes ( x= Computer))

ggplot(Student) +
  geom_bar(aes(x = Computer)) +
  coord_flip()

ggplot (Student) +
  geom_bar (aes (y = Computer))

#geom_col

Student %>% 
  count (Computer) %>% 
  ggplot () +
  geom_col(aes (x = Computer , y = n))

Student %>% 
  count ( Computer) %>% 
  ggplot () +
  geom_col (aes (x = Computer , y = n)) +
  coord_flip ()

Student %>% 
  count( Computer) %>% 
  ggplot () +
  geom_col ( aes ( y = Computer, x = n))

Student %>% 
  count (Computer) %>% 
  ggplot() +
  geom_bar (aes (x = Computer , y = n) , stat = "identity")

#Arranging Bars

geom_bar modification

ggplot(Student) +
  geom_bar (aes (x = Computer)) +
  scale_x_discrete(limits = c("Laptop" , "Desktop"))

ggplot(Student) +
  geom_bar (aes (x = fct_infreq(Computer)))

ggplot(Student) +
  geom_bar (aes (x= fct_infreq(Computer) %>%  fct_rev()))

ggplot(Student) +
  geom_bar(aes(x = Computer, fill = Computer)) +
  scale_fill_manual(values = c("black", "bisque"))

#geom_col modification

Student %>% 
  count (Computer) %>% 
  ggplot() +
  geom_col (aes (x= Computer , y = n)) + scale_x_discrete(limits = c ("Laptop", "Desktop"))

Student %>% 
  count (Computer) %>% 
  ggplot() +
  geom_col (aes (x= reorder(Computer , -n) , y = n))

Student %>% 
  count (Computer) %>% 
  ggplot(aes (x= reorder(Computer , -n) , y = n)) +
  geom_col() + geom_text (aes (label = n), vjust = 1.6 , color = "white" , size = 3.5) +
    theme_minimal ()

Student %>% 
  count (Computer) %>% 
  ggplot(aes (x= reorder(Computer , -n) , y = n)) +
  geom_col() + geom_text (aes (label = n), vjust = 0.5 , color = "black" , size = 3.5) +
    theme_minimal ()

Student %>% 
  count (Computer) %>% 
  ggplot(aes (x= reorder(Computer , -n) , y = n)) +
  geom_col() + geom_text (aes (label = n), vjust = 0.5 , color = "black" , size = 3.5) +
    theme_minimal () +
  facet_wrap (vars (Computer)) +
  ylim (0 , 35) +
  labs(x = "Computer Uages")

Student %>% 
  count (Computer) %>%
  ggplot (aes (x = reorder(Computer , -n), y = n)) + geom_col(fill = "cornflowerblue") + geom_text ( aes ( label = n) , vjust = - 0.5 , color = "black" , size = 3) + theme_light () +
  ylim (0, 35) + labs ( x = "Device usage" ,  y = "Freq" , title = "Frequency of Device Usage by Computer") + theme ( plot.title = element_text (hjust = 0.5),
 strip.text = element_text (color = "black"))

#value on the bars

ggplot (Student , aes (y = Computer)) +
  geom_bar () + 
  geom_text (aes (x = after_stat (count - 1 ), label = after_stat (count)), stat  = "count" ,
             size = 4 , col = "white") + labs (x = "Freq." , y = NULL)

ggplot (Student , aes (y = Computer)) +
  geom_bar () + 
  geom_text (aes (x = after_stat (count - 1 ), label = after_stat (count)), stat  = "count" ,
             size = 3 , position = position_dodge (1)) + labs (x = "Freq." , y = NULL)

#Stacked and percentage Filled Bar Plot

ggplot(Student) + 
  geom_bar (aes (x = Computer , fill = Tablet))

ggplot (Student) +
  geom_bar (aes (x = Laptop , fill = Tablet) , position = "stack")

ggplot (Student) +
  geom_bar (aes (x = Laptop , fill = Tablet) , position = "dodge")

ggplot (Student) +
  geom_bar (aes (x = Laptop , fill = Tablet) , position = "dodge2")

ggplot (Student) +
  geom_bar (aes (x = Laptop , fill = Tablet) , position = "fill")

#Arraning Bars

Student %>% 
  mutate (Laptop = factor (Laptop , level = c ("Dell", "HP" , "Lenovo" , "Asus" , "Apple"))) %>% 
  ggplot () +
  geom_bar (aes (x = Laptop , fill = Tablet), position = "fill")

ggplot(Student) +
  geom_bar (aes ( x = Laptop , fill = Tablet) , position = "fill") +
  scale_x_discrete(limits = c("Dell", "HP" , "Lenovo" , "Asus" , "Apple"))

#Values on bars

ggplot (Student , aes (x = Laptop , fill = Tablet)) + geom_bar (position = "fill") +
  geom_text (aes (label = after_stat (count)), size = 3 , stat = "count" , position = position_fill (vjust = 0.5))

ggplot(
  data = Student,
  y= Laptop,
  x = Computer,
  results.subtitle = FALSE,
  sample.size.label = TRUE , palette = "set3",
  ggtheme = ggplot::theme_bw()
) + labs(title = "Stacked Barplot of Device Usage by Laptop")

#Legend customization

ggplot(data = iris) +
  geom_point (aes (x= Sepal.Length , y = Sepal.Width , col = Species , size = Petal.Length)) + labs ( x = "Sepal Length" , y = "Sepal Length" ,
 title = "Scatter Plot of Sepal :Ength vs Width" ) + theme(legend.position =  "bottom")

ggplot(data = iris) +
  geom_point (aes (x= Sepal.Length , y = Sepal.Width , col = Species , size = Petal.Length)) + labs ( x = "Sepal Length" , y = "Sepal Length" ,
 title = "Scatter Plot of Sepal :Ength vs Width" ) + guides(color = guide_legend(position = "bottom"))

ggplot(data = iris) +
  geom_point (aes (x= Sepal.Length , y = Sepal.Width , col = Species , size = Petal.Length)) + labs ( x = "Sepal Length" , y = "Sepal Length" ,
 title = "Scatter Plot of Sepal :Ength vs Width" ) + guides(color = guide_legend(
   title = "Species",
   position = "bottom",
   direction = "horizontal",
   title.position = "left",
   reverse = FALSE))

#Hide legend for a specific

ggplot (data = iris) +
  geom_point (aes (x= Sepal.Length , y = Sepal.Width  , col = Species , size = Petal.Length)) +
  guides ( color = "none")

ggplot (data = iris) +
  geom_point (aes (x= Sepal.Length , y = Sepal.Width  , col = Species , size = Petal.Length)) +
  guides ( size = "none")

ggplot (data = iris) +
  geom_point (aes (x= Sepal.Length , y = Sepal.Width  , col = Species , size = Petal.Length)) +
  guides ( color = "none" , size = "none")

#Rordering levels of legend

ggplot (data = Student) +
  geom_bar (aes ( y = Computer , fill =Laptop), position = "fill") +
  scale_fill_discrete(breaks = c("Male", "Female"))

#Axis Customization

install.packages("scales")
## Warning: package 'scales' is in use and will not be installed
library (scales)

ggplot(Student , aes (x = Computer , fill = Laptop)) + geom_bar (position = "fill") + geom_text ( aes (label = after_stat(count)) , size = 3, stat = "count" , positon = position_fill (vjust = 0.5)) + scale_y_continuous(labels = scales::label_percent (accuracy = 0.1))
## Warning in geom_text(aes(label = after_stat(count)), size = 3, stat = "count",
## : Ignoring unknown parameters: `positon`

Student %>%
  mutate(Computer = factor(Computer, levels = c("Desktop", "Laptop"))) %>%
  count(Computer, Tablet) %>%
  ggplot(aes(x = Computer, y = n, fill = Tablet)) +
  geom_col(position = "dodge") +
  theme(legend.position = "bottom") +
  scale_y_continuous(labels = scales::dollar_format(prefix = "$"))

#Box Plot

library(ggplot2)

ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_histogram(bins = 30L) +
  scale_fill_manual(
    values = c(
      setosa = "#6c1c80",
      versicolor = "#30A19c",
      virginica = "#123B96"
    )
  ) +
  labs(
    x = "X label",
    y = "Y label",
    title = "Title",
    subtitle = "Subtitle",
    caption = "Caption",
    fill = "Fill label"
  ) +
  theme_bw() +
  theme(
    legend.position = "bottom",
    plot.title = element_text(face = "bold.italic"),
    plot.subtitle = element_text(face = "italic"),
    axis.title.y = element_text(face = "italic"),
    axis.title.x = element_text(face = "italic")
  ) +
  facet_wrap(vars(Species), ncol = 1)