library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

data() ?msleep glimpse(msleep)

fruit <- tibble( name= c(“apple”,“banana”,“orange”), price = c(2.5,2.0,3.5), vitamin_c = c(20,45,250) ) fruit

Energy.df <- read_csv("global-data-on-sustainable-energy.csv", col_names = TRUE)
## Rows: 3649 Columns: 21
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (1): Entity
## dbl (19): Year, Access to electricity (% of population), Access to clean fue...
## num  (1): Density\n(P/Km2)
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
glimpse(Energy.df)
## Rows: 3,649
## Columns: 21
## $ Entity                                                             <chr> "Af…
## $ Year                                                               <dbl> 200…
## $ `Access to electricity (% of population)`                          <dbl> 1.6…
## $ `Access to clean fuels for cooking`                                <dbl> 6.2…
## $ `Renewable-electricity-generating-capacity-per-capita`             <dbl> 9.2…
## $ `Financial flows to developing countries (US $)`                   <dbl> 200…
## $ `Renewable energy share in the total final energy consumption (%)` <dbl> 44.…
## $ `Electricity from fossil fuels (TWh)`                              <dbl> 0.1…
## $ `Electricity from nuclear (TWh)`                                   <dbl> 0, …
## $ `Electricity from renewables (TWh)`                                <dbl> 0.3…
## $ `Low-carbon electricity (% electricity)`                           <dbl> 65.…
## $ `Primary energy consumption per capita (kWh/person)`               <dbl> 302…
## $ `Energy intensity level of primary energy (MJ/$2017 PPP GDP)`      <dbl> 1.6…
## $ Value_co2_emissions_kt_by_country                                  <dbl> 760…
## $ `Renewables (% equivalent primary energy)`                         <dbl> NA,…
## $ gdp_growth                                                         <dbl> NA,…
## $ gdp_per_capita                                                     <dbl> NA,…
## $ `Density\\n(P/Km2)`                                                <dbl> 60,…
## $ `Land Area(Km2)`                                                   <dbl> 652…
## $ Latitude                                                           <dbl> 33.…
## $ Longitude                                                          <dbl> 67.…
unique(Energy.df$Entity)
##   [1] "Afghanistan"                      "Albania"                         
##   [3] "Algeria"                          "Angola"                          
##   [5] "Antigua and Barbuda"              "Argentina"                       
##   [7] "Armenia"                          "Aruba"                           
##   [9] "Australia"                        "Austria"                         
##  [11] "Azerbaijan"                       "Bahamas"                         
##  [13] "Bahrain"                          "Bangladesh"                      
##  [15] "Barbados"                         "Belarus"                         
##  [17] "Belgium"                          "Belize"                          
##  [19] "Benin"                            "Bermuda"                         
##  [21] "Bhutan"                           "Bosnia and Herzegovina"          
##  [23] "Botswana"                         "Brazil"                          
##  [25] "Bulgaria"                         "Burkina Faso"                    
##  [27] "Burundi"                          "Cambodia"                        
##  [29] "Cameroon"                         "Canada"                          
##  [31] "Cayman Islands"                   "Central African Republic"        
##  [33] "Chad"                             "Chile"                           
##  [35] "China"                            "Colombia"                        
##  [37] "Comoros"                          "Congo"                           
##  [39] "Costa Rica"                       "Croatia"                         
##  [41] "Cuba"                             "Cyprus"                          
##  [43] "Czechia"                          "Denmark"                         
##  [45] "Djibouti"                         "Dominica"                        
##  [47] "Dominican Republic"               "Ecuador"                         
##  [49] "Egypt"                            "El Salvador"                     
##  [51] "Equatorial Guinea"                "Eritrea"                         
##  [53] "Estonia"                          "Eswatini"                        
##  [55] "Ethiopia"                         "Fiji"                            
##  [57] "Finland"                          "France"                          
##  [59] "French Guiana"                    "Gabon"                           
##  [61] "Gambia"                           "Georgia"                         
##  [63] "Germany"                          "Ghana"                           
##  [65] "Greece"                           "Grenada"                         
##  [67] "Guatemala"                        "Guinea"                          
##  [69] "Guinea-Bissau"                    "Guyana"                          
##  [71] "Haiti"                            "Honduras"                        
##  [73] "Hungary"                          "Iceland"                         
##  [75] "India"                            "Indonesia"                       
##  [77] "Iraq"                             "Ireland"                         
##  [79] "Israel"                           "Italy"                           
##  [81] "Jamaica"                          "Japan"                           
##  [83] "Jordan"                           "Kazakhstan"                      
##  [85] "Kenya"                            "Kiribati"                        
##  [87] "Kuwait"                           "Kyrgyzstan"                      
##  [89] "Latvia"                           "Lebanon"                         
##  [91] "Lesotho"                          "Liberia"                         
##  [93] "Libya"                            "Lithuania"                       
##  [95] "Luxembourg"                       "Madagascar"                      
##  [97] "Malawi"                           "Malaysia"                        
##  [99] "Maldives"                         "Mali"                            
## [101] "Malta"                            "Mauritania"                      
## [103] "Mauritius"                        "Mexico"                          
## [105] "Mongolia"                         "Montenegro"                      
## [107] "Morocco"                          "Mozambique"                      
## [109] "Myanmar"                          "Namibia"                         
## [111] "Nauru"                            "Nepal"                           
## [113] "Netherlands"                      "New Caledonia"                   
## [115] "New Zealand"                      "Nicaragua"                       
## [117] "Niger"                            "Nigeria"                         
## [119] "North Macedonia"                  "Norway"                          
## [121] "Oman"                             "Pakistan"                        
## [123] "Panama"                           "Papua New Guinea"                
## [125] "Paraguay"                         "Peru"                            
## [127] "Philippines"                      "Poland"                          
## [129] "Portugal"                         "Puerto Rico"                     
## [131] "Qatar"                            "Romania"                         
## [133] "Rwanda"                           "Saint Kitts and Nevis"           
## [135] "Saint Lucia"                      "Saint Vincent and the Grenadines"
## [137] "Samoa"                            "Sao Tome and Principe"           
## [139] "Saudi Arabia"                     "Senegal"                         
## [141] "Serbia"                           "Seychelles"                      
## [143] "Sierra Leone"                     "Singapore"                       
## [145] "Slovakia"                         "Slovenia"                        
## [147] "Solomon Islands"                  "Somalia"                         
## [149] "South Africa"                     "South Sudan"                     
## [151] "Spain"                            "Sri Lanka"                       
## [153] "Sudan"                            "Suriname"                        
## [155] "Sweden"                           "Switzerland"                     
## [157] "Tajikistan"                       "Thailand"                        
## [159] "Togo"                             "Tonga"                           
## [161] "Trinidad and Tobago"              "Tunisia"                         
## [163] "Turkey"                           "Turkmenistan"                    
## [165] "Tuvalu"                           "Uganda"                          
## [167] "Ukraine"                          "United Arab Emirates"            
## [169] "United Kingdom"                   "United States"                   
## [171] "Uruguay"                          "Uzbekistan"                      
## [173] "Vanuatu"                          "Yemen"                           
## [175] "Zambia"                           "Zimbabwe"
colSums(is.na(Energy.df))
##                                                           Entity 
##                                                                0 
##                                                             Year 
##                                                                0 
##                          Access to electricity (% of population) 
##                                                               10 
##                                Access to clean fuels for cooking 
##                                                              169 
##             Renewable-electricity-generating-capacity-per-capita 
##                                                              931 
##                   Financial flows to developing countries (US $) 
##                                                             2089 
## Renewable energy share in the total final energy consumption (%) 
##                                                              194 
##                              Electricity from fossil fuels (TWh) 
##                                                               21 
##                                   Electricity from nuclear (TWh) 
##                                                              126 
##                                Electricity from renewables (TWh) 
##                                                               21 
##                           Low-carbon electricity (% electricity) 
##                                                               42 
##               Primary energy consumption per capita (kWh/person) 
##                                                                0 
##      Energy intensity level of primary energy (MJ/$2017 PPP GDP) 
##                                                              207 
##                                Value_co2_emissions_kt_by_country 
##                                                              428 
##                         Renewables (% equivalent primary energy) 
##                                                             2137 
##                                                       gdp_growth 
##                                                              317 
##                                                   gdp_per_capita 
##                                                              282 
##                                                Density\\n(P/Km2) 
##                                                                1 
##                                                   Land Area(Km2) 
##                                                                1 
##                                                         Latitude 
##                                                                1 
##                                                        Longitude 
##                                                                1
cnames <-colnames(Energy.df) %>%
  str_replace_all( " ","_") %>%
  str_replace_all( "\\\\","_") %>%
  str_replace_all("\\%", "perc")
cnames
##  [1] "Entity"                                                             
##  [2] "Year"                                                               
##  [3] "Access_to_electricity_(perc_of_population)"                         
##  [4] "Access_to_clean_fuels_for_cooking"                                  
##  [5] "Renewable-electricity-generating-capacity-per-capita"               
##  [6] "Financial_flows_to_developing_countries_(US_$)"                     
##  [7] "Renewable_energy_share_in_the_total_final_energy_consumption_(perc)"
##  [8] "Electricity_from_fossil_fuels_(TWh)"                                
##  [9] "Electricity_from_nuclear_(TWh)"                                     
## [10] "Electricity_from_renewables_(TWh)"                                  
## [11] "Low-carbon_electricity_(perc_electricity)"                          
## [12] "Primary_energy_consumption_per_capita_(kWh/person)"                 
## [13] "Energy_intensity_level_of_primary_energy_(MJ/$2017_PPP_GDP)"        
## [14] "Value_co2_emissions_kt_by_country"                                  
## [15] "Renewables_(perc_equivalent_primary_energy)"                        
## [16] "gdp_growth"                                                         
## [17] "gdp_per_capita"                                                     
## [18] "Density_n(P/Km2)"                                                   
## [19] "Land_Area(Km2)"                                                     
## [20] "Latitude"                                                           
## [21] "Longitude"
colnames(Energy.df) <-cnames
Energy.df %>%
  filter(Entity=="Saudi Arabia")
## # A tibble: 21 × 21
##    Entity        Year Access_to_electricity_(perc_of_po…¹ Access_to_clean_fuel…²
##    <chr>        <dbl>                               <dbl>                  <dbl>
##  1 Saudi Arabia  2000                                 100                    100
##  2 Saudi Arabia  2001                                 100                    100
##  3 Saudi Arabia  2002                                 100                    100
##  4 Saudi Arabia  2003                                 100                    100
##  5 Saudi Arabia  2004                                 100                    100
##  6 Saudi Arabia  2005                                 100                    100
##  7 Saudi Arabia  2006                                 100                    100
##  8 Saudi Arabia  2007                                 100                    100
##  9 Saudi Arabia  2008                                 100                    100
## 10 Saudi Arabia  2009                                 100                    100
## # ℹ 11 more rows
## # ℹ abbreviated names: ¹​`Access_to_electricity_(perc_of_population)`,
## #   ²​Access_to_clean_fuels_for_cooking
## # ℹ 17 more variables:
## #   `Renewable-electricity-generating-capacity-per-capita` <dbl>,
## #   `Financial_flows_to_developing_countries_(US_$)` <dbl>,
## #   `Renewable_energy_share_in_the_total_final_energy_consumption_(perc)` <dbl>, …
Energy.df %>%
  filter(!is.na(`Low-carbon_electricity_(perc_electricity)`)) %>%
  group_by(Entity) %>%
summarise(avg_perc_electricity = mean(`Low-carbon_electricity_(perc_electricity)`)) %>%
  arrange(desc(avg_perc_electricity)) %>%
  top_n(20) %>%
  ggplot(aes(Entity, avg_perc_electricity))+
  geom_bar(stat = "identity")+
  coord_flip()
## Selecting by avg_perc_electricity

Value_co2_emissions (metric tons per capita): Carbon dioxide emissions per person in metric tons.

co2 <- Energy.df %>%
  filter(!is.na(`Value_co2_emissions_kt_by_country`)) %>%
  group_by(Entity) %>%
  summarise(avg_co2_emissions = mean(`Value_co2_emissions_kt_by_country`)) %>%
  arrange(desc(avg_co2_emissions)) %>%
  top_n(10)
## Selecting by avg_co2_emissions
  ggplot(co2, aes(Entity, avg_co2_emissions))+
  geom_bar(stat = "identity")+
  coord_flip()

co22 <- Energy.df %>%
  filter(!is.na(`Value_co2_emissions_kt_by_country`)) %>%
  group_by(Entity) %>%
  summarise(avg_co2_emissions = mean(`Value_co2_emissions_kt_by_country`)) %>%
  arrange(desc(avg_co2_emissions))

ccname <- unique(co22$Entity)


Energy.df %>%
  filter(!is.na(`Value_co2_emissions_kt_by_country`)) %>%
  filter(Entity %in% ccname[1:2]) %>%
  ggplot(aes(x=Year,y=Value_co2_emissions_kt_by_country,color=Entity))+
    geom_line()

Energy.df %>%
  filter(!is.na(`Value_co2_emissions_kt_by_country`)) %>%
  filter(Entity %in% ccname[3:10]) %>%
  ggplot(aes(x=Year,y=Value_co2_emissions_kt_by_country,color=Entity))+
    geom_line()

Energy.df %>%
  filter(!is.na(`Low-carbon_electricity_(perc_electricity)`)) %>%
  filter(Entity %in% ccname[1:9])  %>%
  ggplot(aes(x=Year,y=`Low-carbon_electricity_(perc_electricity)`,color=Entity))+
    geom_line()

Energy.df %>%
  filter(!is.na(`Low-carbon_electricity_(perc_electricity)`)) %>%
  filter(Entity %in% ccname[10])  %>%
  ggplot(aes(x=Year,y=`Low-carbon_electricity_(perc_electricity)`,color=Entity))+
    geom_line()

library(gplots)
## 
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
## 
##     lowess
Energy.df %>%
  filter(!is.na(`Low-carbon_electricity_(perc_electricity)`)) %>%
  filter(Entity=="France") %>%
  ggplot(aes(x=Year,y=`Low-carbon_electricity_(perc_electricity)`))+
  geom_line()

cor_df <-Energy.df %>%
  select(-Entity,-`Financial_flows_to_developing_countries_(US_$)`,-`Renewables_(perc_equivalent_primary_energy)`) %>%
  na.omit()


heatmap.2(cor(cor_df), Rowv = FALSE, Colv = FALSE, dendrogram = "none",
cellnote = round(cor(cor_df),2),
notecol = "black", key = FALSE, trace = 'none', margins = c(15,10))

library(ggplot2)
library(reshape) # to generate input for the plot
## 
## Attaching package: 'reshape'
## The following object is masked from 'package:lubridate':
## 
##     stamp
## The following object is masked from 'package:dplyr':
## 
##     rename
## The following objects are masked from 'package:tidyr':
## 
##     expand, smiths
cor.mat <- round(cor(cor_df),2) # rounded correlation matrix
melted.cor.mat <- melt(cor.mat)
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE

## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
ggplot(melted.cor.mat, aes(x = X1, y = X2, fill = value)) +
geom_tile()+
geom_text(aes(x = X1, y = X2, label = value))+
  theme(axis.text.x = element_text(angle=90, vjust = 0.5, hjust=1))

melted.cor.mat %>%
  filter(X1=="Value_co2_emissions_kt_by_country") %>%
  arrange(desc(value)) # %>%
##                                   X1
## 1  Value_co2_emissions_kt_by_country
## 2  Value_co2_emissions_kt_by_country
## 3  Value_co2_emissions_kt_by_country
## 4  Value_co2_emissions_kt_by_country
## 5  Value_co2_emissions_kt_by_country
## 6  Value_co2_emissions_kt_by_country
## 7  Value_co2_emissions_kt_by_country
## 8  Value_co2_emissions_kt_by_country
## 9  Value_co2_emissions_kt_by_country
## 10 Value_co2_emissions_kt_by_country
## 11 Value_co2_emissions_kt_by_country
## 12 Value_co2_emissions_kt_by_country
## 13 Value_co2_emissions_kt_by_country
## 14 Value_co2_emissions_kt_by_country
## 15 Value_co2_emissions_kt_by_country
## 16 Value_co2_emissions_kt_by_country
## 17 Value_co2_emissions_kt_by_country
## 18 Value_co2_emissions_kt_by_country
##                                                                     X2 value
## 1                                    Value_co2_emissions_kt_by_country  1.00
## 2                                  Electricity_from_fossil_fuels_(TWh)  0.99
## 3                                    Electricity_from_renewables_(TWh)  0.90
## 4                                       Electricity_from_nuclear_(TWh)  0.89
## 5                                                       Land_Area(Km2)  0.70
## 6                                                             Latitude  0.15
## 7                                                            Longitude  0.13
## 8                           Access_to_electricity_(perc_of_population)  0.12
## 9                                                           gdp_growth  0.08
## 10         Energy_intensity_level_of_primary_energy_(MJ/$2017_PPP_GDP)  0.06
## 11                                   Access_to_clean_fuels_for_cooking  0.05
## 12                Renewable-electricity-generating-capacity-per-capita  0.05
## 13                                                                Year  0.04
## 14                  Primary_energy_consumption_per_capita_(kWh/person)  0.02
## 15                                                      gdp_per_capita  0.01
## 16                                                    Density_n(P/Km2) -0.01
## 17                           Low-carbon_electricity_(perc_electricity) -0.06
## 18 Renewable_energy_share_in_the_total_final_energy_consumption_(perc) -0.11
 # top_n(5)
unique(Energy.df$Year)
##  [1] 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
## [16] 2015 2016 2017 2018 2019 2020