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

Pacages

Loading require packages:

x <- 12
y <- 30
x + y
## [1] 42

Scatter plot

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")

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

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_hex(shape = 22) 
## Warning in geom_hex(shape = 22): Ignoring unknown parameters: `shape`

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

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

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

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

Extra

res <- sample(1:100, 10)
res
##  [1] 32  9 61 46 84 51 89 74 68 93
sum(res)
## [1] 607

The total of the two number is 607 # Reduce gap between plot and axis

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

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

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

Facet

facet warp

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

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

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

facet grid

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

Loading the student survey data

result <- readxl::read_excel("C:/Users/Laptop Link/Desktop/RESULT.xlsx")
ggplot(data = result) +
  geom_histogram(aes(x = credit, fill = subject),
                 bins = 5, col = "white", alpha = 0.8)

  facet_grid(rows = vars(job), cols = vars(gender))
## <ggproto object: Class FacetGrid, Facet, gg>
##     attach_axes: function
##     attach_strips: function
##     compute_layout: function
##     draw_back: function
##     draw_front: function
##     draw_labels: function
##     draw_panel_content: function
##     draw_panels: function
##     finish_data: function
##     format_strip_labels: function
##     init_gtable: function
##     init_scales: function
##     map_data: function
##     params: list
##     set_panel_size: function
##     setup_data: function
##     setup_panel_params: function
##     setup_params: function
##     shrink: TRUE
##     train_scales: function
##     vars: function
##     super:  <ggproto object: Class FacetGrid, Facet, gg>

Themes

Built in Theme

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

Themes from other packages

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

ggplot(data = iris) +
  geom_histogram(aes(x = Sepal.Length, fill = Species),
                 bins = 6, col = "white", alpha = 0.8) +
  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 globally theme

theme_set(theme_bw())

set colour

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

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

ggplot(data = iris) +
  geom_histogram(aes(x = Sepal.Length, fill = Species),
                 bins = 6, col = "white", alpha = 0.9) +
  facet_wrap(vars(Species), ncol = 1) +
  scale_fill_hue(
    l = 80 , c = 150,
    h = c(90,360)
  )