1. Purpose.

The purpose of this noteboook is to illustrate how the ggplot2 package can be used to create graphs.

2. Load relevant libraries and view practice dataset.

library(dplyr)
library(ggplot2)
iris <- as_tibble(iris)
iris
## # A tibble: 150 x 5
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
##           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
##  1          5.1         3.5          1.4         0.2 setosa 
##  2          4.9         3            1.4         0.2 setosa 
##  3          4.7         3.2          1.3         0.2 setosa 
##  4          4.6         3.1          1.5         0.2 setosa 
##  5          5           3.6          1.4         0.2 setosa 
##  6          5.4         3.9          1.7         0.4 setosa 
##  7          4.6         3.4          1.4         0.3 setosa 
##  8          5           3.4          1.5         0.2 setosa 
##  9          4.4         2.9          1.4         0.2 setosa 
## 10          4.9         3.1          1.5         0.1 setosa 
## # ... with 140 more rows

3. Create a scatterplot.

#title <- "Iris sepal length by species"
#subtitle <- "1936"
#x_title <- "Species"
#y_title <- "Sepal length"
#caption <- "Note: this is the famous Fisher's iris data set"

ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, colour = Species)) +
  geom_point(show.legend = F) + 
  geom_smooth() +
  facet_wrap(~Species) +
  #labs(title = title, subtitle = subtitle, x = x_title, y = y_title, caption = caption) +
  scale_colour_brewer(palette = "Paired") 

4. Create a bar graph.

#title <- "Iris sepal length by species"
#subtitle <- "1936"
#x_title <- "Species"
#y_title <- "Sepal length"
#caption <- "Note: this is the famous Fisher's iris data set"

iris %>% 
  group_by(Species) %>% 
  summarise(Sepal.Length = mean(Sepal.Length)) %>% 
  ggplot(aes(x = Species, y = Sepal.Length, fill = Species)) +
  geom_bar(stat = "identity", colour = "black", show.legend = F) +
  #facet_wrap(~Species) +
  #labs(title = title, subtitle = subtitle, x = x_title, y = y_title, caption = caption) +
  scale_fill_brewer(palette = "Paired") 

5. Create a boxplot.

#title <- "Iris sepal length by species"
#subtitle <- "1936"
#x_title <- "Species"
#y_title <- "Sepal length"
#caption <- "Note: this is the famous Fisher's iris data set"

iris %>% 
  ggplot(aes(x = Species, y = Sepal.Length, fill = Species)) +
  geom_boxplot(colour = "black", show.legend = F) +
  #facet_wrap(~Species) +
  #labs(title = title, subtitle = subtitle, x = x_title, y = y_title, caption = caption) +
  scale_fill_brewer(palette = "Paired") 

5. Create a histogram.

#title <- "Iris sepal length by species"
#subtitle <- "1936"
#x_title <- "Species"
#y_title <- "Sepal length"
#caption <- "Note: this is the famous Fisher's iris data set"

iris %>% 
  ggplot(aes(x = Sepal.Length, fill = Species)) +
  geom_histogram(show.legend = F, alpha = 0.5) +
  facet_wrap(~Species) +
  #labs(title = title, subtitle = subtitle, x = x_title, y = y_title, caption = caption) +
  scale_fill_brewer(palette = "Paired") 

6. Create a smoothed density graph.

#title <- "Iris sepal length by species"
#subtitle <- "1936"
#x_title <- "Species"
#y_title <- "Sepal length"
#caption <- "Note: this is the famous Fisher's iris data set"

iris %>% 
  ggplot(aes(x = Sepal.Length, colour = Species)) +
  geom_density(show.legend = F, alpha = 0.5) +
  #facet_wrap(~Species) +
  #labs(title = title, subtitle = subtitle, x = x_title, y = y_title, caption = caption) +
  scale_colour_brewer(palette = "Paired") 

7. Add a custom theme.

theme_minimal_adjust <- function() {
  list(theme(
  panel.border = element_rect(colour = "#d3d3d3", fill = NA, size = 0.5),
  strip.background  =  element_rect(colour = "#d3d3d3", fill = "white"),
  panel.spacing  =  unit(0.5, "lines"),
  text = element_text(family = "Arial", size = 11),
  plot.title = element_text(family = "Arial", size = 14, face = "bold", hjust = 0.5),
  plot.subtitle = element_text(family = "Arial", size = 14, face = "plain", hjust = 0.5),
  plot.caption = element_text(family = "Arial", size = 10, face = "plain", hjust = 0),
  axis.title = element_text(family = "Arial", size = 12),
  axis.text.x = element_text(family = "Arial", size = 12, angle = 90, hjust = 1, vjust = 0.5),
  axis.text.y = element_text(family = "Arial", hjust = 0.5, size = 12),
  strip.text = element_text(family = "Arial", size = 12),
  legend.text = element_text(family = "Arial", size = 12),
  axis.line = element_line(colour = "black", size = 0.5),
  axis.ticks = element_line() 
  ))
 }

ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length)) +
  geom_point(show.legend = F) + 
  facet_wrap(~Species) +
  expand_limits(y=0) +
  scale_y_continuous(expand=c(0, 0)) +
  theme_minimal() +
  theme_minimal_adjust()