Colour Gradient and Theme (ggplot2)

R for Pleasure

Nguyen Chi Dung

#---------------------------
#      Default Graph
#---------------------------

library(gapminder)
library(tidyverse)
library(magrittr)
library(scales)

theme_set(theme_minimal())

# Basic Graph 1: 
gapminder %>% 
  ggplot(aes(x = gdpPercap, y = lifeExp)) + 
  geom_point(color = "purple", alpha = 0.3) + 
  geom_smooth(method = "loess") + 
  scale_x_log10(labels = scales::dollar) +
  labs(x = "GDP Per Capita", y = "Life Expectancy in Years",
       title = "Economic Growth and Life Expectancy from 1962 to 2007",
       subtitle = "Data points are country-years.",
       caption = "Source: Gapminder Foundation")

# Basic Graph 2 using colour gradient (low-high) for lifeExp: 
gapminder %>% 
  ggplot(aes(x = gdpPercap, y = lifeExp)) + 
  geom_point(mapping = aes(color = lifeExp)) + 
  geom_smooth(method = "loess", alpha = 0.15, fill = "orange", color = "orange") + 
  scale_x_log10(labels = scales::dollar) +
  labs(x = "GDP Per Capita", y = "Life Expectancy in Years",
       title = "Economic Growth and Life Expectancy from 1952 to 2007",
       subtitle = "Data points are country-years.",
       caption = "Source: Gapminder Foundation") ->> p1

p1

#---------------------------
#     Customized Graph
#---------------------------


# Customize Graph: 
p1 +
  scale_color_gradient(name = "Life Expectancy", 
                       limits = c(20, 83), 
                       low = "white", high = "blue")

# Customize Graph using viridis package: 

library(viridis)
p1 + 
  scale_color_viridis(direction = -1, option = "D", "Life Expectancy")

p1 + 
  scale_color_viridis(direction = -1, option = "A", "Life Expectancy")

p1 + 
  scale_color_viridis(direction = -1, option = "B", "Life Expectancy")

p1 + 
  theme(legend.position = "top") + 
  scale_color_viridis(direction = -1, 
                     option = "D", 
                     name = "Life Expectancy", 
                     guide = guide_colourbar(direction = "horizontal",
                                             barheight = unit(2, units = "mm"),
                                             barwidth = unit(40, units = "mm"),
                                             draw.ulim = F,
                                             title.hjust = 0.5,
                                             label.hjust = 0.5, 
                                             title.position = "top")) ->> p2

p2

#-------------------------------
#  Using our background theme
#------------------------------

p2 + 
  theme(plot.background = element_rect(fill = "#f5f5f2", color = NA)) ->> p3

p3

p3 + 
  theme(text = element_text(color = "#22211d", face = "bold")) ->> p4

p4  

p2 + 
  theme(plot.background = element_rect(fill = "seashell")) + 
  theme(panel.grid.minor = element_blank()) ->> p5

p5  

p3 + 
  theme(panel.grid.minor = element_blank()) ->> p6 

p6  

#-----------------------------------
#  Customize fonts for our Graphs
#-----------------------------------

library(extrafont)
font_import(pattern = "OfficinaSansITCMedium.ttf", prompt = FALSE)
extrafont::loadfonts(device = "win")

p6 + 
  # Sử dụng toàn bộ hiển thị text bằng font chữ OfficinaSansITC: 
  theme(text = element_text(family = "OfficinaSansITC")) + 
  # Chọn kích cỡ + màu sắc cho hiển thị ở trục X: 
  theme(axis.text.x = element_text(size = 12, color = "gray30")) + 
  # Chọn kích cỡ + màu sắc cho hiển thị ở trục Y: 
  theme(axis.text.y = element_text(size = 12, color = "gray30")) + 
  # Chọn kích cỡ + màu sắc cho tiêu đề chính: 
  theme(plot.title = element_text(color = "gray10", size = 18)) + 
  # Chọn kích cỡ + màu sắc cho tiêu đề phụ và caption: 
  theme(plot.subtitle = element_text(color = "gray40", size = 16)) + 
  theme(plot.caption = element_text(color = "gray40", size = 12)) +   
  # Hiệu chỉnh kích cỡ + màu sắc + vị trí  cho chỉ dẫn với trục X và Y:
  theme(axis.title.x = element_text(hjust = 0, size = 14, color = "grey20")) + 
  theme(axis.title.y = element_text(vjust = 1, size = 14, color = "grey20")) + 
  # Hiệu chỉnh kích thước cho các số 40, 60 và 80:
  theme(legend.text = element_text(size = 12, color = "grey40")) + 
  # Hiệu chỉnh kích thước cho chữ Life Expectancy: 
  theme(legend.title = element_text(size = 15, color = "grey30"))

# We should creat a function for theme: 

my_theme <- function(...) {
  theme(text = element_text(family = "OfficinaSansITC")) + 
  theme(axis.text.x = element_text(size = 12, color = "gray30")) + 
  theme(axis.text.y = element_text(size = 12, color = "gray30")) + 
  theme(plot.title = element_text(color = "gray10", size = 18)) + 
  theme(plot.subtitle = element_text(color = "gray40", size = 16)) + 
  theme(plot.caption = element_text(color = "gray40", size = 12)) +   
  theme(axis.title.x = element_text(hjust = 0, size = 14, color = "grey20")) + 
  theme(axis.title.y = element_text(vjust = 1, size = 14, color = "grey20")) + 
  theme(legend.text = element_text(size = 12, color = "grey40")) + 
  theme(legend.title = element_text(size = 15, color = "grey30"))
}

# And Use this function: 

p5 +     
  my_theme() +
  theme(plot.background = element_rect(fill = "seashell", color = NA)) ->> p7

p7

# Customize above Graph: 
p7 + 
  theme(legend.position = c(0.82, 0.2))