http://www.datavisualisation-r.com/bar-chart-simple/
rm(list = ls())
library(tidyverse)
library(magrittr)
library(readxl)
library(extrafont)
extrafont::loadfonts(device = "win")
my_df <- read_excel("D:\\dell_E\\BooksforusingR\\DataVisualization\\scriptsanddata\\myData\\ipsos.xlsx") %>%
mutate(Cou = paste(.$Country, .$Percent),
bol = case_when(Country %in% c("Germany") ~ "bold",
!Country %in% c("Germany") ~ "plain"),
grad = case_when(Country == "Germany" ~ "grey10",
Country != "Germany" ~ "grey50")) %>%
arrange(Percent) %>%
mutate(Cou = factor(Cou, levels = Cou))
my_rect <- data.frame(y1 = seq(0, 80, 20),
y2 = seq(20, 100, 20),
colour = rep(c("#BFEFFF50", "#BFEFFF78"), length.out = 5))
# Plot kiểu 1:
my_df %>%
ggplot() +
geom_bar(aes(Cou, Percent), fill = "grey", stat = "identity") +
geom_bar(data = my_df %>% filter(Country %in% c("Brazil", "Germany")),
aes(Cou, Percent), fill = "#FF00D2", stat = "identity") +
geom_rect(data = my_rect,
mapping = aes(ymin = y1, ymax = y2, xmin = -Inf, xmax = +Inf),
fill = my_rect$colour) +
geom_hline(aes(yintercept = 45), colour = "skyblue3", size = 1) +
coord_flip() +
scale_y_continuous(breaks = seq(0, 100, 20),
limits = c(0, 105),
expand = c(0, 0)) +
theme_minimal() +
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_text(face = my_df$bol, color = my_df$grad),
plot.subtitle = element_text(color = "gray20", size = 12, face = "italic"),
plot.caption = element_text(size = 10),
text = element_text(family = "Georgia", size = 15)) +
labs(title = "I Definitely Believe in God or a Supreme Being",
subtitle = "Created by Nguyen Chi Dung",
x = NULL, y = NULL,
caption = "Source: Adapted from http://www.datavisualisation-r.com/bar-chart-simple/") ->> p1
p1
# Plot kiểu 2:
p1 +
annotate("text",
label = "The average belief in God based on data\ncollected from 16 countries is 45%.",
family = "Georgia",
x = 4, y = 60,
size = 3.5, hjust = 0, vjust = 1) +
annotate("curve",
curvature = 0.4,
x = 3.5,
xend = 5,
y = 59,
yend = 45.5,
arrow = arrow(angle = 35, length = unit(.2, "cm")), size = .7) ->> p2
p2
# Plot kiểu 3:
my_df %>%
mutate(Percent = Percent / 100) %>%
ggplot() +
geom_col(aes(Cou, Percent), fill = "#377eb8") +
geom_col(data = my_df %>% filter(Country == "Germany"),
aes(Cou, Percent / 100), fill = "#e41a1c") +
geom_hline(aes(yintercept = 0.45), colour = "orange", size = 1) +
coord_flip() +
scale_y_continuous(breaks = seq(0, 1, 0.2),
limits = c(0, 1.05),
expand = c(0, 0),
labels = scales::percent) +
theme_minimal() +
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
panel.grid.major.x = element_line(color = "gray", linetype = 8),
axis.ticks = element_blank(),
axis.text.y = element_text(face = my_df$bol, color = my_df$grad),
plot.subtitle = element_text(color = "gray20", size = 12, face = "italic"),
plot.caption = element_text(size = 10),
text = element_text(family = "Georgia", size = 15)) +
labs(title = "I Definitely Believe in God or a Supreme Being",
subtitle = "Created by Nguyen Chi Dung",
x = NULL, y = NULL,
caption = "Source: Adapted from http://www.datavisualisation-r.com/bar-chart-simple/") +
annotate("text",
label = "The average belief in God based on data\ncollected from 16 countries is 45%.",
family = "Georgia",
x = 5, y = 0.61,
size = 3.5, hjust = 0, vjust = 1) +
annotate("curve",
curvature = 0,
x = 4.3,
xend = 4.3,
y = 0.59,
yend = 0.46,
arrow = arrow(angle = 20, length = unit(.3, "cm")), size = .8) ->> p3
p3
# Plot kiểu 4 - sử dụng font của tạp chí The Economist:
font_import(pattern = "OfficinaSansITCMedium.ttf", prompt = FALSE)
extrafont::loadfonts(device = "win")
my_df %>%
mutate(Percent = Percent / 100) %>%
ggplot() +
geom_col(aes(Cou, Percent), fill = "#377eb8") +
geom_col(data = my_df %>% filter(Country == "Germany"),
aes(Cou, Percent / 100), fill = "#e41a1c") +
geom_hline(aes(yintercept = 0.45), colour = "orange", size = 1) +
coord_flip() +
scale_y_continuous(breaks = seq(0, 1, 0.2),
limits = c(0, 1.05),
expand = c(0, 0),
labels = scales::percent) +
theme_minimal() +
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
panel.grid.major.x = element_line(color = "gray", linetype = 8),
axis.ticks = element_blank(),
# Điều chỉnh kích cỡ text hiển thị trên trục X:
axis.text.x = element_text(size = 11),
axis.text.y = element_text(face = my_df$bol, color = my_df$grad, size = 12),
plot.subtitle = element_text(color = "gray20", size = 12),
plot.caption = element_text(size = 10),
text = element_text(family = "OfficinaSansITC", size = 18)) +
labs(title = "I Definitely Believe in God or a Supreme Being",
subtitle = "Created by Nguyen Chi Dung",
x = NULL, y = NULL,
caption = "Source: Adapted from http://www.datavisualisation-r.com/bar-chart-simple/") +
annotate("text",
label = "The average belief in God based on data\ncollected from 16 countries is 45%.",
x = 5, y = 0.61,
size = 4, hjust = 0, vjust = 1, family = "OfficinaSansITC") +
annotate("curve",
curvature = 0,
x = 4.3,
xend = 4.3,
y = 0.59,
yend = 0.46,
arrow = arrow(angle = 20, length = unit(.3, "cm")), size = .8) ->> p4
p4