#########
library(dplyr)   # for data manipulation
## 
## 载入程辑包:'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)   # for reshaping the data frame
library(stringr) # string manipulation
library(ggplot2) # graphing

# create the data frame 
# (in wide format, as needed for the line segments):
dat_wide = tibble::tribble(
  ~Country,   ~Y1990,   ~Y2015,
  'Russia',  71.5, 101.4,
  'Canada',  74.4, 102.9,
  'Other non-OECD Europe/Eurasia',  60.9, 135.2,
  'South Korea',   127, 136.2,
  'China',  58.5, 137.1,
  'Middle East', 170.9, 158.8,
  'United States', 106.8,   169,
  'Australia/New Zealand', 123.6, 170.9,
  'Brazil', 208.5, 199.8,
  'Japan',   181, 216.7,
  'Africa', 185.4,   222,
  'Other non-OECD Asia', 202.7,   236,
  'OECD Europe', 173.8, 239.9,
  'Other non-OECD Americas', 193.1, 242.3,
  'India', 173.8, 260.6,
  'Mexico/Chile', 221.1, 269.8
)

# a version reshaped to long format (for the points):
dat_long = dat_wide %>% 
  gather(key = 'Year', value = 'Energy_productivity', Y1990:Y2015) %>% 
  mutate(Year = str_replace(Year, 'Y', ''))

# create the graph:
head(dat_wide)
## # A tibble: 6 x 3
##   Country                       Y1990 Y2015
##   <chr>                         <dbl> <dbl>
## 1 Russia                         71.5  101.
## 2 Canada                         74.4  103.
## 3 Other non-OECD Europe/Eurasia  60.9  135.
## 4 South Korea                   127    136.
## 5 China                          58.5  137.
## 6 Middle East                   171.   159.
ggplot() +
  geom_segment(data = dat_wide, 
               aes(x    = Y1990, 
                   xend = Y2015, 
                   y    = reorder(Country, Y2015), 
                   yend = reorder(Country, Y2015)),
               size = 3, colour = '#D0D0D0') +
  geom_point(data = dat_long,
             aes(x      = Energy_productivity, 
                 y      = Country, 
                 colour = Year),
             size = 4) +
  labs(title = 'Energy productivity in selected countries \nand regions',
       subtitle = 'Billion dollars GDP per quadrillion BTU',
       caption = 'Source: EIA, 2016',
       x = NULL, y = NULL) +
  scale_colour_manual(values = c('#1082CD', 'darkred')) +
  theme_bw() +
  theme(legend.position = c(0.92, 0.20),
        legend.title = element_blank(),
        legend.box.background = element_rect(colour = 'black'),
        panel.border = element_blank(),
        axis.ticks = element_line(colour = '#E6E6E6'))

ggsave('energy.png', width = 20, height = 10, units = 'cm')
################################################################
#install.packages("ggforce")
library(ggforce)
#################################################
#install.packages("ggnewscale")
head(dat_wide)
## # A tibble: 6 x 3
##   Country                       Y1990 Y2015
##   <chr>                         <dbl> <dbl>
## 1 Russia                         71.5  101.
## 2 Canada                         74.4  103.
## 3 Other non-OECD Europe/Eurasia  60.9  135.
## 4 South Korea                   127    136.
## 5 China                          58.5  137.
## 6 Middle East                   171.   159.
p1 <- ggplot(data = dat_wide) +
    geom_link( aes(x = Y1990, 
                     xend = Y2015, 
                     y    = Country, 
                     yend = Country,
                     colour = stat(index)), 
              lineend = "square", size = 5, alpha = 1,
              show.legend = FALSE) +
    scale_color_continuous(low = "#CCEBC5", high = "#FBB4AE") 
p1 + geom_point(aes(x = Y2015, y = Country), shape = 19, colour = "#FBB4AE", size = 6) +
  geom_point(aes(x = Y1990, y = Country), shape = 19, colour = "#CCEBC5", size = 6) +
  theme_minimal()

  #theme_bw() +
  #theme_classic()
  #theme_minimal()
##ref https://stats.stackexchange.com/questions/423735/what-is-the-name-of-this-plot-that-has-rows-with-two-connected-dots/423861
##https://thef5.substack.com/p/how-to-comet-plot
##https://ggforce.data-imaginist.com/reference/geom_link.html
##https://stackoverflow.com/questions/69202109/how-to-order-paths-in-geom-link-based-on-the-difference-between-xend-and-x-value
##https://cran.microsoft.com/snapshot/2019-03-14/web/packages/ggforce/vignettes/Visual_Guide.html