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

if echo = false, then no code will show in output

Packages

Loading required packages:

library(ggplot2)
library(dplyr)
library(ggthemes)

Setting global theme

theme_set(theme_bw())

Data

Iris data

data(iris)

Loading excel data

Student <- readxl::read_excel("student_dataset-1.xlsx")

Scatter plot

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

For species wise scatter plot

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

For geom_point design

ggplot(data= iris, aes(x=Sepal.Length, y=Sepal.Width)) +
  geom_point(col= "red", size= 3, shape= 16)

For speices wise color change

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

Histogram

binwidth

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

bins number

ggplot(iris, aes(x= Sepal.Length)) +
  geom_histogram(bins = 15, fill= "blue", col= "black")

Species wise color change

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

Reduce gap between plot and axis============

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

ggplot(data = iris)+
  geom_histogram(aes(x=Sepal.Length, fill = Species),
                 bins = 10, col= "black", alpha= .6)+
  scale_y_continuous(
    breaks = seq(0, 40, by= 5),
    expand = expansion(
      mult = c(0, 0),                 #expand upper portion of the plot 
      add = c(0, 0)                   #increase gap at the bottom portion
    )
  )

ggplot(data = iris)+
  geom_histogram(aes(x=Sepal.Length, fill = Species),
                 bins = 10, col= "white", alpha= .6)+
  scale_y_continuous(expand = expansion(add = c(0, 5)))+
  scale_x_continuous(expand = expansion(add = c(0, 0)))

Facet

facet_wrap

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

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

ggplot(data = iris)+
  geom_histogram(aes(x=Sepal.Length, fill = Species),
                 bins = 10, col="black", alpha= 0.6)+
  facet_wrap(vars(Species), ncol=3, scale= "free_x")

Facet_grid

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

ggplot(data = Student)+
  geom_histogram(aes(x=GPA, fill = Employment),
                 bins = 10, col= "black", alpha= .6)+
  facet_grid(rows= vars(Employment), cols= vars(Gender))

ggplot(data = Student)+
  geom_histogram(aes(x=GPA, fill = Employment),
                 bins = 10, col= "black", alpha= .6)+
  facet_grid(rows= vars(Employment), cols= vars(Class))

Student %>% 
  mutate(Class= factor(Class, levels = c("Freshman", "Sophomore", "Junior", "Senior"))) %>%
  summary()
##        ID            Gender               Age              Class   
##  Min.   :  1.00   Length:132         Min.   :18.00   Freshman : 4  
##  1st Qu.: 33.75   Class :character   1st Qu.:20.00   Sophomore:29  
##  Median : 66.50   Mode  :character   Median :22.00   Junior   :24  
##  Mean   : 66.50                      Mean   :21.84   Senior   :75  
##  3rd Qu.: 99.25                      3rd Qu.:24.00                 
##  Max.   :132.00                      Max.   :30.00                 
##     Major           Grad Intention          GPA         Employment       
##  Length:132         Length:132         Min.   :2.340   Length:132        
##  Class :character   Class :character   1st Qu.:2.950   Class :character  
##  Mode  :character   Mode  :character   Median :3.000   Mode  :character  
##                                        Mean   :3.016                     
##                                        3rd Qu.:3.300                     
##                                        Max.   :3.680                     
##      Salary      Social Networking  Satisfaction      Spending     
##  Min.   :30.00   Min.   : 0.000    Min.   :1.000   Min.   : 700.0  
##  1st Qu.:50.00   1st Qu.: 0.000    1st Qu.:3.000   1st Qu.: 800.0  
##  Median :60.00   Median : 6.500    Median :4.000   Median : 900.0  
##  Mean   :57.89   Mean   : 4.902    Mean   :3.523   Mean   : 952.3  
##  3rd Qu.:65.00   3rd Qu.: 8.000    3rd Qu.:4.000   3rd Qu.:1100.0  
##  Max.   :85.00   Max.   :12.000    Max.   :6.000   Max.   :1200.0  
##    Computer         Text Messages       Wealth       
##  Length:132         Min.   :  0.0   Min.   :  0.100  
##  Class :character   1st Qu.: 40.0   1st Qu.:  0.200  
##  Mode  :character   Median :300.0   Median :  1.500  
##                     Mean   :267.6   Mean   :  6.825  
##                     3rd Qu.:500.0   3rd Qu.: 10.000  
##                     Max.   :600.0   Max.   :100.000

Theme======

Build in Theme

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

Other pre-build theme (ggthemes)

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

ggthemeAssist

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

Manually changing color

ggplot(data=iris)+
  geom_histogram(aes(x=Sepal.Length, fill = Species),
                 bins = 10, col= "black", alpha =.6)+
  facet_wrap(vars(Species), ncol= 1)+
  scale_fill_manual(values = c("setosa"= "tomato", "versicolor"="black", "virginica" ="skyblue"))

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