# Instalar plotly si aún no está instalado
if (!requireNamespace("plotly", quietly = TRUE)) {
  install.packages("plotly")
}
# Instalación de librerías
#install.packages("ggplot2")
#install.packages("reshape2")
#install.packages("dplyr")
#install.packages("data.table")

# Carga de librerías
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
library(reshape2)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(data.table)
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## The following objects are masked from 'package:reshape2':
## 
##     dcast, melt
# Lectura del archivo CSV
energia_sus <- fread("global_data_sustainable_energy.csv")

# Visualización de los primeros y últimos 5 datos
head(energia_sus)
##         Entity Year Access to electricity (% of population)
## 1: Afghanistan 2000                                1.613591
## 2: Afghanistan 2001                                4.074574
## 3: Afghanistan 2002                                9.409158
## 4: Afghanistan 2003                               14.738506
## 5: Afghanistan 2004                               20.064968
## 6: Afghanistan 2005                               25.390894
##    Access to clean fuels for cooking
## 1:                               6.2
## 2:                               7.2
## 3:                               8.2
## 4:                               9.5
## 5:                              10.9
## 6:                              12.2
##    Renewable-electricity-generating-capacity-per-capita
## 1:                                                 9.22
## 2:                                                 8.86
## 3:                                                 8.47
## 4:                                                 8.09
## 5:                                                 7.75
## 6:                                                 7.51
##    Financial flows to developing countries (US $)
## 1:                                          20000
## 2:                                         130000
## 3:                                        3950000
## 4:                                       25970000
## 5:                                           <NA>
## 6:                                        9830000
##    Renewable energy share in the total final energy consumption (%)
## 1:                                                            44.99
## 2:                                                            45.60
## 3:                                                            37.83
## 4:                                                            36.66
## 5:                                                            44.24
## 6:                                                            33.88
##    Electricity from fossil fuels (TWh) Electricity from nuclear (TWh)
## 1:                                0.16                              0
## 2:                                0.09                              0
## 3:                                0.13                              0
## 4:                                0.31                              0
## 5:                                0.33                              0
## 6:                                0.34                              0
##    Electricity from renewables (TWh) Low-carbon electricity (% electricity)
## 1:                              0.31                               65.95744
## 2:                              0.50                               84.74577
## 3:                              0.56                               81.15942
## 4:                              0.63                               67.02128
## 5:                              0.56                               62.92135
## 6:                              0.59                               63.44086
##    Primary energy consumption per capita (kWh/person)
## 1:                                           302.5948
## 2:                                           236.8919
## 3:                                           210.8622
## 4:                                           229.9682
## 5:                                           204.2312
## 6:                                           252.0691
##    Energy intensity level of primary energy (MJ/$2017 PPP GDP)
## 1:                                                        1.64
## 2:                                                        1.74
## 3:                                                        1.40
## 4:                                                        1.40
## 5:                                                        1.20
## 6:                                                        1.41
##    Value_co2_emissions_kt_by_country Renewables (% equivalent primary energy)
## 1:                               760                                       NA
## 2:                               730                                       NA
## 3:                              1030                                       NA
## 4:                              1220                                       NA
## 5:                              1030                                       NA
## 6:                              1550                                       NA
##    gdp_growth gdp_per_capita Density\\n(P/Km2) Land Area(Km2) Latitude
## 1:         NA             NA                60         652230 33.93911
## 2:         NA             NA                60         652230 33.93911
## 3:         NA       179.4266                60         652230 33.93911
## 4:   8.832278       190.6838                60         652230 33.93911
## 5:   1.414118       211.3821                60         652230 33.93911
## 6:  11.229715       242.0313                60         652230 33.93911
##    Longitude
## 1:  67.70995
## 2:  67.70995
## 3:  67.70995
## 4:  67.70995
## 5:  67.70995
## 6:  67.70995
tail(energia_sus)
##      Entity Year Access to electricity (% of population)
## 1: Zimbabwe 2015                                33.70000
## 2: Zimbabwe 2016                                42.56173
## 3: Zimbabwe 2017                                44.17863
## 4: Zimbabwe 2018                                45.57265
## 5: Zimbabwe 2019                                46.78148
## 6: Zimbabwe 2020                                52.74767
##    Access to clean fuels for cooking
## 1:                              29.5
## 2:                              29.8
## 3:                              29.8
## 4:                              29.9
## 5:                              30.1
## 6:                              30.4
##    Renewable-electricity-generating-capacity-per-capita
## 1:                                                63.54
## 2:                                                62.88
## 3:                                                62.33
## 4:                                                82.53
## 5:                                                81.40
## 6:                                                80.61
##    Financial flows to developing countries (US $)
## 1:                                           <NA>
## 2:                                          30000
## 3:                                        5570000
## 4:                                          10000
## 5:                                         250000
## 6:                                          30000
##    Renewable energy share in the total final energy consumption (%)
## 1:                                                            80.82
## 2:                                                            81.90
## 3:                                                            82.46
## 4:                                                            80.23
## 5:                                                            81.50
## 6:                                                            81.90
##    Electricity from fossil fuels (TWh) Electricity from nuclear (TWh)
## 1:                                4.02                              0
## 2:                                3.50                              0
## 3:                                3.05                              0
## 4:                                3.73                              0
## 5:                                3.66                              0
## 6:                                3.40                              0
##    Electricity from renewables (TWh) Low-carbon electricity (% electricity)
## 1:                              5.37                               57.18850
## 2:                              3.32                               48.68035
## 3:                              4.30                               58.50341
## 4:                              5.46                               59.41241
## 5:                              4.58                               55.58253
## 6:                              4.19                               55.20422
##    Primary energy consumption per capita (kWh/person)
## 1:                                           3860.920
## 2:                                           3227.680
## 3:                                           3068.012
## 4:                                           3441.986
## 5:                                           3003.655
## 6:                                           2680.132
##    Energy intensity level of primary energy (MJ/$2017 PPP GDP)
## 1:                                                       10.36
## 2:                                                       10.00
## 3:                                                        9.51
## 4:                                                        9.83
## 5:                                                       10.47
## 6:                                                       10.00
##    Value_co2_emissions_kt_by_country Renewables (% equivalent primary energy)
## 1:                             12430                                       NA
## 2:                             11020                                       NA
## 3:                             10340                                       NA
## 4:                             12380                                       NA
## 5:                             11760                                       NA
## 6:                                NA                                       NA
##    gdp_growth gdp_per_capita Density\\n(P/Km2) Land Area(Km2)  Latitude
## 1:  1.7798727       1445.070                38         390757 -19.01544
## 2:  0.7558693       1464.589                38         390757 -19.01544
## 3:  4.7094922       1235.189                38         390757 -19.01544
## 4:  4.8242105       1254.642                38         390757 -19.01544
## 5: -6.1442363       1316.741                38         390757 -19.01544
## 6: -6.2487482       1214.510                38         390757 -19.01544
##    Longitude
## 1:  29.15486
## 2:  29.15486
## 3:  29.15486
## 4:  29.15486
## 5:  29.15486
## 6:  29.15486
# Información de la base de datos
str(energia_sus)
## Classes 'data.table' and 'data.frame':   3649 obs. of  21 variables:
##  $ Entity                                                          : chr  "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
##  $ Year                                                            : int  2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 ...
##  $ Access to electricity (% of population)                         : num  1.61 4.07 9.41 14.74 20.06 ...
##  $ Access to clean fuels for cooking                               : num  6.2 7.2 8.2 9.5 10.9 ...
##  $ Renewable-electricity-generating-capacity-per-capita            : num  9.22 8.86 8.47 8.09 7.75 7.51 7.4 7.25 7.49 7.5 ...
##  $ Financial flows to developing countries (US $)                  :integer64 20000 130000 3950000 25970000 NA 9830000 10620000 15750000 ... 
##  $ Renewable energy share in the total final energy consumption (%): num  45 45.6 37.8 36.7 44.2 ...
##  $ Electricity from fossil fuels (TWh)                             : num  0.16 0.09 0.13 0.31 0.33 0.34 0.2 0.2 0.19 0.16 ...
##  $ Electricity from nuclear (TWh)                                  : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Electricity from renewables (TWh)                               : num  0.31 0.5 0.56 0.63 0.56 0.59 0.64 0.75 0.54 0.78 ...
##  $ Low-carbon electricity (% electricity)                          : num  66 84.7 81.2 67 62.9 ...
##  $ Primary energy consumption per capita (kWh/person)              : num  303 237 211 230 204 ...
##  $ Energy intensity level of primary energy (MJ/$2017 PPP GDP)     : num  1.64 1.74 1.4 1.4 1.2 1.41 1.5 1.53 1.94 2.25 ...
##  $ Value_co2_emissions_kt_by_country                               : num  760 730 1030 1220 1030 ...
##  $ Renewables (% equivalent primary energy)                        : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ gdp_growth                                                      : num  NA NA NA 8.83 1.41 ...
##  $ gdp_per_capita                                                  : num  NA NA 179 191 211 ...
##  $ Density\n(P/Km2)                                               : chr  "60" "60" "60" "60" ...
##  $ Land Area(Km2)                                                  : int  652230 652230 652230 652230 652230 652230 652230 652230 652230 652230 ...
##  $ Latitude                                                        : num  33.9 33.9 33.9 33.9 33.9 ...
##  $ Longitude                                                       : num  67.7 67.7 67.7 67.7 67.7 ...
##  - attr(*, ".internal.selfref")=<externalptr>
# Dimensión de la base de datos
dimensiones <- dim(energia_sus)
print(dimensiones)
## [1] 3649   21
# Estadísticas básicas: media, desviación estándar, min, max, percentiles (25, 50, 75)
summary(energia_sus)
##     Entity               Year      Access to electricity (% of population)
##  Length:3649        Min.   :2000   Min.   :  1.252                        
##  Class :character   1st Qu.:2005   1st Qu.: 59.801                        
##  Mode  :character   Median :2010   Median : 98.362                        
##                     Mean   :2010   Mean   : 78.934                        
##                     3rd Qu.:2015   3rd Qu.:100.000                        
##                     Max.   :2020   Max.   :100.000                        
##                                    NA's   :10                             
##  Access to clean fuels for cooking
##  Min.   :  0.00                   
##  1st Qu.: 23.18                   
##  Median : 83.15                   
##  Mean   : 63.26                   
##  3rd Qu.:100.00                   
##  Max.   :100.00                   
##  NA's   :169                      
##  Renewable-electricity-generating-capacity-per-capita
##  Min.   :   0.00                                     
##  1st Qu.:   3.54                                     
##  Median :  32.91                                     
##  Mean   : 113.14                                     
##  3rd Qu.: 112.21                                     
##  Max.   :3060.19                                     
##  NA's   :931                                         
##  Financial flows to developing countries (US $)
##  Min.   :         0                            
##  1st Qu.:    260000                            
##  Median :   5660000                            
##  Mean   :  94224000                            
##  3rd Qu.:  55290000                            
##  Max.   :5202310000                            
##  NA's   :      2089                            
##  Renewable energy share in the total final energy consumption (%)
##  Min.   : 0.000                                                  
##  1st Qu.: 6.515                                                  
##  Median :23.300                                                  
##  Mean   :32.638                                                  
##  3rd Qu.:55.245                                                  
##  Max.   :96.040                                                  
##  NA's   :194                                                     
##  Electricity from fossil fuels (TWh) Electricity from nuclear (TWh)
##  Min.   :   0.00                     Min.   :  0.00                
##  1st Qu.:   0.29                     1st Qu.:  0.00                
##  Median :   2.97                     Median :  0.00                
##  Mean   :  70.36                     Mean   : 13.45                
##  3rd Qu.:  26.84                     3rd Qu.:  0.00                
##  Max.   :5184.13                     Max.   :809.41                
##  NA's   :21                          NA's   :126                   
##  Electricity from renewables (TWh) Low-carbon electricity (% electricity)
##  Min.   :   0.00                   Min.   :  0.000                       
##  1st Qu.:   0.04                   1st Qu.:  2.878                       
##  Median :   1.47                   Median : 27.865                       
##  Mean   :  23.97                   Mean   : 36.801                       
##  3rd Qu.:   9.60                   3rd Qu.: 64.404                       
##  Max.   :2184.94                   Max.   :100.000                       
##  NA's   :21                        NA's   :42                            
##  Primary energy consumption per capita (kWh/person)
##  Min.   :     0                                    
##  1st Qu.:  3117                                    
##  Median : 13121                                    
##  Mean   : 25744                                    
##  3rd Qu.: 33893                                    
##  Max.   :262586                                    
##                                                    
##  Energy intensity level of primary energy (MJ/$2017 PPP GDP)
##  Min.   : 0.110                                             
##  1st Qu.: 3.170                                             
##  Median : 4.300                                             
##  Mean   : 5.307                                             
##  3rd Qu.: 6.027                                             
##  Max.   :32.570                                             
##  NA's   :207                                                
##  Value_co2_emissions_kt_by_country Renewables (% equivalent primary energy)
##  Min.   :      10                  Min.   : 0.000                          
##  1st Qu.:    2020                  1st Qu.: 2.137                          
##  Median :   10500                  Median : 6.291                          
##  Mean   :  159866                  Mean   :11.987                          
##  3rd Qu.:   60580                  3rd Qu.:16.842                          
##  Max.   :10707220                  Max.   :86.837                          
##  NA's   :428                       NA's   :2137                            
##    gdp_growth      gdp_per_capita     Density\\n(P/Km2)  Land Area(Km2)   
##  Min.   :-62.076   Min.   :   111.9   Length:3649        Min.   :     21  
##  1st Qu.:  1.383   1st Qu.:  1337.8   Class :character   1st Qu.:  25713  
##  Median :  3.560   Median :  4578.6   Mode  :character   Median : 117600  
##  Mean   :  3.442   Mean   : 13283.8                      Mean   : 633213  
##  3rd Qu.:  5.830   3rd Qu.: 15768.6                      3rd Qu.: 513120  
##  Max.   :123.140   Max.   :123514.2                      Max.   :9984670  
##  NA's   :317       NA's   :282                           NA's   :1        
##     Latitude         Longitude      
##  Min.   :-40.901   Min.   :-175.20  
##  1st Qu.:  3.203   1st Qu.: -11.78  
##  Median : 17.190   Median :  19.15  
##  Mean   : 18.246   Mean   :  14.82  
##  3rd Qu.: 38.970   3rd Qu.:  46.20  
##  Max.   : 64.963   Max.   : 178.07  
##  NA's   :1         NA's   :1
# Conteo de valores NaN (NA en R) en todo el DataFrame
nan_counts <- sapply(energia_sus, function(x) sum(is.na(x)))
cat("Número de valores NaN por columna:\n")
## Número de valores NaN por columna:
print(nan_counts)
##                                                           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) 
##                                                                0 
##                                                   Land Area(Km2) 
##                                                                1 
##                                                         Latitude 
##                                                                1 
##                                                        Longitude 
##                                                                1
# Total de valores NaN en el DataFrame
total_nan <- sum(nan_counts)
cat("\nTotal de valores NaN en el DataFrame:", total_nan, "\n")
## 
## Total de valores NaN en el DataFrame: 6977
# Nombres de las columnas
print(names(energia_sus))
##  [1] "Entity"                                                          
##  [2] "Year"                                                            
##  [3] "Access to electricity (% 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 (%)"
##  [8] "Electricity from fossil fuels (TWh)"                             
##  [9] "Electricity from nuclear (TWh)"                                  
## [10] "Electricity from renewables (TWh)"                               
## [11] "Low-carbon electricity (% 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 (% equivalent primary energy)"                        
## [16] "gdp_growth"                                                      
## [17] "gdp_per_capita"                                                  
## [18] "Density\\n(P/Km2)"                                               
## [19] "Land Area(Km2)"                                                  
## [20] "Latitude"                                                        
## [21] "Longitude"
# Renombrar columnas
energia_sus <- energia_sus %>% 
  rename(CO2 = Value_co2_emissions_kt_by_country, Country = Entity)

# Seleccionar columnas de interés para el boxplot
variables <- c(
  'Access to electricity (% of population)',
  'Access to clean fuels for cooking',
  'Renewable-electricity-generating-capacity-per-capita',
  'Financial flows to developing countries (US $)',
  'Renewable energy share in the total final energy consumption (%)',
  'Electricity from fossil fuels (TWh)',
  'Electricity from nuclear (TWh)',
  'Electricity from renewables (TWh)',
  'Low-carbon electricity (% electricity)',
  'Primary energy consumption per capita (kWh/person)',
  'Energy intensity level of primary energy (MJ/$2017 PPP GDP)',
  'CO2',
  'Renewables (% equivalent primary energy)',
  'gdp_growth',
  'gdp_per_capita',
  'Land Area(Km2)'
)
# Filtrar las columnas seleccionadas
df_selected <- energia_sus %>% select(all_of(variables))

# Derretir el DataFrame para el gráfico de caja
df_melted <- melt(df_selected, variable.name = 'Variable', value.name = 'Value')
## Warning in melt.data.table(df_selected, variable.name = "Variable", value.name
## = "Value"): id.vars and measure.vars are internally guessed when both are
## 'NULL'. All non-numeric/integer/logical type columns are considered id.vars,
## which in this case are columns []. Consider providing at least one of 'id' or
## 'measure' vars in future.
## Warning in melt.data.table(df_selected, variable.name = "Variable", value.name
## = "Value"): 'measure.vars' [Access to electricity (% of population), Access to
## clean fuels for cooking, Renewable-electricity-generating-capacity-per-capita,
## Financial flows to developing countries (US $), ...] are not all of the same
## type. By order of hierarchy, the molten data value column will be of type
## 'double'. All measure variables not of type 'double' will be coerced too. Check
## DETAILS in ?melt.data.table for more on coercion.
library(ggplot2)
library(reshape2)  # Para melt()

# Supongamos que tienes un data frame df_melted ya creado que contiene las columnas "Value" y "Variable"
# Crear el gráfico de caja
ggplot(df_melted, aes(x = Value, y = Variable)) +
  geom_boxplot(outlier.colour = "red", outlier.size = 1, fill = "lightblue") +
  geom_jitter(color = "black", size = 0.5, alpha = 0.5) +
  labs(title = "Distribución de Variables de Energía y Desarrollo",
       x = "Valores",
       y = "Variables") +
  theme_minimal()
## Warning: Removed 4886 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
## Warning: Removed 4886 rows containing missing values or values outside the scale range
## (`geom_point()`).

## **Boxplot Individual para la variable 'Financial flows to developing countries (US $)'**
# Filtrar la columna específica
df_financial_flows <- energia_sus[, "Financial flows to developing countries (US $)"]

# Cargar la librería
library(plotly)
## Warning: package 'plotly' was built under R version 4.2.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
# Crear el gráfico de caja
fig <- plot_ly(
  data = df_financial_flows, 
  x = ~`Financial flows to developing countries (US $)`, 
  type = "box",
  title = 'Distribución de los Flujos Financieros hacia los Países en Desarrollo (US $)',
  xaxis = list(title = 'Flujos Financieros (US $)')
)

# Mostrar el gráfico
fig
## Warning: 'box' objects don't have these attributes: 'title'
## Valid attributes include:
## 'alignmentgroup', 'boxmean', 'boxpoints', 'customdata', 'customdatasrc', 'dx', 'dy', 'fillcolor', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'jitter', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'lowerfence', 'lowerfencesrc', 'marker', 'mean', 'meansrc', 'median', 'mediansrc', 'meta', 'metasrc', 'name', 'notched', 'notchspan', 'notchspansrc', 'notchwidth', 'offsetgroup', 'opacity', 'orientation', 'pointpos', 'q1', 'q1src', 'q3', 'q3src', 'quartilemethod', 'sd', 'sdsrc', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textsrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'upperfence', 'upperfencesrc', 'visible', 'whiskerwidth', 'width', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## **Histograma de los flujos financieros para los países en desarrollo**
# Crear el histograma
fig_hist <- plot_ly(
  data = df_financial_flows, 
  x = ~`Financial flows to developing countries (US $)`, 
  type = "histogram", 
  nbinsx = 100,
  title = 'Histograma de los Flujos Financieros hacia los Países en Desarrollo (US $)',
  xaxis = list(title = 'Flujos Financieros (US $)')
)
fig_hist
## Warning: 'histogram' objects don't have these attributes: 'title'
## Valid attributes include:
## '_deprecated', 'alignmentgroup', 'autobinx', 'autobiny', 'bingroup', 'cliponaxis', 'constraintext', 'cumulative', 'customdata', 'customdatasrc', 'error_x', 'error_y', 'histfunc', 'histnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'nbinsx', 'nbinsy', 'offsetgroup', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textsrc', 'texttemplate', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'xaxis', 'xbins', 'xcalendar', 'xhoverformat', 'xsrc', 'y', 'yaxis', 'ybins', 'ycalendar', 'yhoverformat', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## **Boxplot para el valor de las emisiones de CO2 en Kt por país**
# Filtrar la columna específica
df_box_co2 <- energia_sus %>% select(CO2)

# Crear el gráfico de caja para la variable seleccionada
fig_co2 <- plot_ly(
  data = df_box_co2, 
  x = ~CO2, 
  type = "box",
  title = 'Valor de las emisiones de CO2 en kt por país',
  xaxis = list(title = 'Valor de las emisiones de CO2 en kt por país')
)
fig_co2
## Warning: Ignoring 428 observations
## Warning: 'box' objects don't have these attributes: 'title'
## Valid attributes include:
## 'alignmentgroup', 'boxmean', 'boxpoints', 'customdata', 'customdatasrc', 'dx', 'dy', 'fillcolor', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'jitter', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'lowerfence', 'lowerfencesrc', 'marker', 'mean', 'meansrc', 'median', 'mediansrc', 'meta', 'metasrc', 'name', 'notched', 'notchspan', 'notchspansrc', 'notchwidth', 'offsetgroup', 'opacity', 'orientation', 'pointpos', 'q1', 'q1src', 'q3', 'q3src', 'quartilemethod', 'sd', 'sdsrc', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textsrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'upperfence', 'upperfencesrc', 'visible', 'whiskerwidth', 'width', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## **Histograma del valor de las emisiones de CO2 en Kt por país**
# Crear el histograma
fig_hist_co2 <- plot_ly(
  data = df_box_co2, 
  x = ~CO2, 
  type = "histogram", 
  nbinsx = 100,
  title = 'Valor de las emisiones de CO2 en kt por país',
  xaxis = list(title = 'Valor de las emisiones de CO2 en kt por país')
)
fig_hist_co2
## Warning: Ignoring 428 observations
## Warning: 'histogram' objects don't have these attributes: 'title'
## Valid attributes include:
## '_deprecated', 'alignmentgroup', 'autobinx', 'autobiny', 'bingroup', 'cliponaxis', 'constraintext', 'cumulative', 'customdata', 'customdatasrc', 'error_x', 'error_y', 'histfunc', 'histnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'nbinsx', 'nbinsy', 'offsetgroup', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textsrc', 'texttemplate', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'xaxis', 'xbins', 'xcalendar', 'xhoverformat', 'xsrc', 'y', 'yaxis', 'ybins', 'ycalendar', 'yhoverformat', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## **Coeficiente de variación**
# Excluir las columnas específicas que no quieres analizar
columnas_excluir <- c('Year', 'Land Area(Km2)', 'Latitude', 'Longitude')

# Seleccionar solo las columnas numéricas excluyendo las que no queremos
columnas_incluidas <- setdiff(names(energia_sus)[sapply(energia_sus, is.numeric)], columnas_excluir)

# Calcular la media, desviación estándar y coeficiente de variación para las columnas seleccionadas
for (column in columnas_incluidas) {
  media <- mean(energia_sus[[column]], na.rm = TRUE)
  desviacion <- sd(energia_sus[[column]], na.rm = TRUE)
  
  # Evitar la división por cero
  if (media != 0) {
    coef_variacion <- (desviacion / media) * 100
    cat("El coeficiente de variación para", column, "es:", round(coef_variacion, 2), "%\n")
  } else {
    cat("La media de la columna", column, "es cero, no se puede calcular el coeficiente de variación.\n")
  }
}
## El coeficiente de variación para Access to electricity (% of population) es: 38.36 %
## El coeficiente de variación para Access to clean fuels for cooking es: 61.72 %
## El coeficiente de variación para Renewable-electricity-generating-capacity-per-capita es: 215.81 %
## El coeficiente de variación para Financial flows to developing countries (US $) es: 316.43 %
## El coeficiente de variación para Renewable energy share in the total final energy consumption (%) es: 91.59 %
## El coeficiente de variación para Electricity from fossil fuels (TWh) es: 494.64 %
## El coeficiente de variación para Electricity from nuclear (TWh) es: 542.79 %
## El coeficiente de variación para Electricity from renewables (TWh) es: 435.71 %
## El coeficiente de variación para Low-carbon electricity (% electricity) es: 93.24 %
## El coeficiente de variación para Primary energy consumption per capita (kWh/person) es: 135.07 %
## El coeficiente de variación para Energy intensity level of primary energy (MJ/$2017 PPP GDP) es: 66.55 %
## El coeficiente de variación para CO2 es: 483.94 %
## El coeficiente de variación para Renewables (% equivalent primary energy) es: 125.09 %
## El coeficiente de variación para gdp_growth es: 165.23 %
## El coeficiente de variación para gdp_per_capita es: 148.38 %
## **Coeficiente de asimetría de Pearson**
asimetria_pearson <- list()

# Calcular la media, mediana, desviación estándar y coeficiente de asimetría de Pearson para cada columna numérica
for (column in columnas_incluidas) {
  media <- mean(energia_sus[[column]], na.rm = TRUE)
  mediana <- median(energia_sus[[column]], na.rm = TRUE)
  desviacion <- sd(energia_sus[[column]], na.rm = TRUE)
  
  # Evitar la división por cero
  if (desviacion != 0) {
    coef_asimetria <- (3 * (media - mediana)) / desviacion
    asimetria_pearson[[column]] <- coef_asimetria
    cat("El coeficiente de asimetría de Pearson para", column, "es:", round(coef_asimetria, 2), "\n")
  } else {
    cat("La desviación estándar de la columna", column, "es cero, no se puede calcular el coeficiente de asimetría.\n")
  }
}
## El coeficiente de asimetría de Pearson para Access to electricity (% of population) es: -1.93 
## El coeficiente de asimetría de Pearson para Access to clean fuels for cooking es: -1.53 
## El coeficiente de asimetría de Pearson para Renewable-electricity-generating-capacity-per-capita es: 0.99 
## El coeficiente de asimetría de Pearson para Financial flows to developing countries (US $) es: 0.89 
## El coeficiente de asimetría de Pearson para Renewable energy share in the total final energy consumption (%) es: 0.94 
## El coeficiente de asimetría de Pearson para Electricity from fossil fuels (TWh) es: 0.58 
## El coeficiente de asimetría de Pearson para Electricity from nuclear (TWh) es: 0.55 
## El coeficiente de asimetría de Pearson para Electricity from renewables (TWh) es: 0.65 
## El coeficiente de asimetría de Pearson para Low-carbon electricity (% electricity) es: 0.78 
## El coeficiente de asimetría de Pearson para Primary energy consumption per capita (kWh/person) es: 1.09 
## El coeficiente de asimetría de Pearson para Energy intensity level of primary energy (MJ/$2017 PPP GDP) es: 0.86 
## El coeficiente de asimetría de Pearson para CO2 es: 0.58 
## El coeficiente de asimetría de Pearson para Renewables (% equivalent primary energy) es: 1.14 
## El coeficiente de asimetría de Pearson para gdp_growth es: -0.06 
## El coeficiente de asimetría de Pearson para gdp_per_capita es: 1.32
# Mostrar los resultados como DataFrame
df_asimetria <- data.frame(
  Variable = names(asimetria_pearson),
  Coeficiente_Asimetria_de_Pearson = unlist(asimetria_pearson)
)
print(df_asimetria)
##                                                                                                                          Variable
## Access to electricity (% of population)                                                   Access to electricity (% of population)
## Access to clean fuels for cooking                                                               Access to clean fuels for cooking
## Renewable-electricity-generating-capacity-per-capita                         Renewable-electricity-generating-capacity-per-capita
## Financial flows to developing countries (US $)                                     Financial flows to developing countries (US $)
## Renewable energy share in the total final energy consumption (%) Renewable energy share in the total final energy consumption (%)
## Electricity from fossil fuels (TWh)                                                           Electricity from fossil fuels (TWh)
## Electricity from nuclear (TWh)                                                                     Electricity from nuclear (TWh)
## Electricity from renewables (TWh)                                                               Electricity from renewables (TWh)
## Low-carbon electricity (% electricity)                                                     Low-carbon electricity (% electricity)
## Primary energy consumption per capita (kWh/person)                             Primary energy consumption per capita (kWh/person)
## Energy intensity level of primary energy (MJ/$2017 PPP GDP)           Energy intensity level of primary energy (MJ/$2017 PPP GDP)
## CO2                                                                                                                           CO2
## Renewables (% equivalent primary energy)                                                 Renewables (% equivalent primary energy)
## gdp_growth                                                                                                             gdp_growth
## gdp_per_capita                                                                                                     gdp_per_capita
##                                                                  Coeficiente_Asimetria_de_Pearson
## Access to electricity (% of population)                                               -1.92510524
## Access to clean fuels for cooking                                                     -1.52865130
## Renewable-electricity-generating-capacity-per-capita                                   0.98572797
## Financial flows to developing countries (US $)                                         0.89112216
## Renewable energy share in the total final energy consumption (%)                       0.93709943
## Electricity from fossil fuels (TWh)                                                    0.58090482
## Electricity from nuclear (TWh)                                                         0.55269740
## Electricity from renewables (TWh)                                                      0.64630210
## Low-carbon electricity (% electricity)                                                 0.78124529
## Primary energy consumption per capita (kWh/person)                                     1.08906319
## Energy intensity level of primary energy (MJ/$2017 PPP GDP)                            0.85561098
## CO2                                                                                    0.57919341
## Renewables (% equivalent primary energy)                                               1.13959506
## gdp_growth                                                                            -0.06237959
## gdp_per_capita                                                                         1.32499239
if (!require("tidyr")) install.packages("tidyr")
## Loading required package: tidyr
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:reshape2':
## 
##     smiths
library(tidyr)
## **Valores Faltantes**
# Contar valores faltantes por variable
missing_data <- energia_sus %>%
  summarise(across(everything(), ~ sum(is.na(.)))) %>%
  pivot_longer(cols = everything(), names_to = "variable", values_to = "missing_count") 

# Crear el gráfico de valores faltantes
ggplot(missing_data, aes(x = reorder(variable, -missing_count), y = missing_count)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  coord_flip() +
  labs(title = "Cantidad de valores faltantes por variable", x = "Variables", y = "Cantidad de valores faltantes") +
  theme_minimal()

# Calcular el porcentaje de valores faltantes
missing_percentage <- energia_sus %>%
  summarise(across(everything(), ~ sum(is.na(.)) / nrow(energia_sus) * 100)) %>%
  pivot_longer(cols = everything(), names_to = "variable", values_to = "missing_percentage")

# Crear el gráfico de porcentaje de valores faltantes
ggplot(missing_percentage, aes(x = reorder(variable, -missing_percentage), y = missing_percentage)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  coord_flip() +
  labs(title = "Porcentaje de valores faltantes por variable", x = "Variables", y = "Porcentaje de valores faltantes") +
  theme_minimal()

# Gráfico de barras para visualizar el porcentaje de valores faltantes
ggplot(missing_percentage, aes(x = reorder(variable, -missing_percentage), y = missing_percentage)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  coord_flip() +
  labs(title = "Porcentaje de valores faltantes por variable en el DataFrame energia_sus",
       x = "Variables",
       y = "Porcentaje de valores faltantes") +
  theme_minimal()

# Mapa de calor del patrón de datos faltantes
library(ggplot2)
library(reshape2)

# Mapa de calor para ver patrones de valores faltantes
missing_matrix <- is.na(energia_sus)
missing_heatmap <- melt(missing_matrix)
## Warning in melt(missing_matrix): The melt generic in data.table has been passed
## a matrix and will attempt to redirect to the relevant reshape2 method; please
## note that reshape2 is deprecated, and this redirection is now deprecated as
## well. To continue using melt methods from reshape2 while both libraries are
## attached, e.g. melt.list, you can prepend the namespace like
## reshape2::melt(missing_matrix). In the next version, this warning will become
## an error.
ggplot(missing_heatmap, aes(Var2, Var1, fill = value)) +
  geom_tile() +
  scale_fill_manual(values = c("white", "blue"), labels = c("Presente", "Faltante")) +
  labs(title = "Mapa de calor del patrón de datos faltantes en 'energia_sus'",
       x = "Variables",
       y = "Observaciones") +
  theme_minimal()

## **Mapa geo-referenciado del porcentaje de población por país que tiene acceso a la electricidad en 2014**
df_2014 <- filter(energia_sus, Year == 2014)

# Filtrar los datos para el año 2014
df_2014 <- energia_sus %>% filter(Year == 2014)

# Crear el mapa coroplético para el acceso a electricidad
fig <- plot_geo(df_2014) %>%
  add_trace(
    z = ~`Access to electricity (% of population)`,
    locations = ~Country,
    color = ~`Access to electricity (% of population)`,
    colors = "Blues",  # Cambiar 'Plasma' a 'Viridis'
    colorbar = list(title = "Acceso a electricidad (%)")
  ) %>%
  layout(title = "Porcentaje de población por país que tiene acceso a electricidad en 2014")

# Mostrar el gráfico
fig
## **Mapa geo-referenciado del porcentaje de población por país que tiene acceso a combustibles limpios 2014**
fig <- plot_geo(df_2014) %>%
  add_trace(
    z = ~`Access to clean fuels for cooking`,
    locations = ~Country,
    color = ~`Access to clean fuels for cooking`,
    colors = "Blues"
  ) %>%
  layout(title = "Porcentaje de población por país que tiene acceso a combustibles limpios para cocinar en 2014")
fig
## Warning: Ignoring 8 observations
## **Principales fuentes de electricidad en Colombia**
# Agrupar y transformar datos para Colombia
energia_agrupado <- energia_sus %>%
  group_by(Country) %>%
  summarise(
    `Electricity from fossil fuels (TWh)` = sum(`Electricity from fossil fuels (TWh)`, na.rm = TRUE),
    `Electricity from nuclear (TWh)` = sum(`Electricity from nuclear (TWh)`, na.rm = TRUE),
    `Electricity from renewables (TWh)` = sum(`Electricity from renewables (TWh)`, na.rm = TRUE)
  )

df_colombia <- filter(energia_agrupado, Country == "Colombia") %>%
  pivot_longer(-Country, names_to = "Source", values_to = "Electricity (TWh)")

# Gráfico de pastel
fig <- plot_ly(df_colombia, labels = ~Source, values = ~`Electricity (TWh)`, type = 'pie') %>%
  layout(title = 'Principales fuentes de electricidad en Colombia')
fig
## **Países que usan energías renovables**
fig <- plot_geo(energia_sus) %>%
  add_trace(
    z = ~`Electricity from renewables (TWh)`,
    locations = ~Country,
    color = ~`Electricity from renewables (TWh)`,
    colors = "Blues"
  ) %>%
  layout(title = "Países que usan energías renovables")
fig
## Warning: Ignoring 21 observations
## **Países que usan energía nuclear**
fig <- plot_geo(energia_sus) %>%
  add_trace(
    z = ~`Electricity from nuclear (TWh)`,
    locations = ~Country,
    color = ~`Electricity from nuclear (TWh)`,
    colors = "Blues"
  ) %>%
  layout(title = "Países que usan energía nuclear")
fig
## Warning: Ignoring 126 observations
## **Países que usan energía de combustibles fósiles**
fig <- plot_geo(energia_sus) %>%
  add_trace(
    z = ~`Electricity from fossil fuels (TWh)`,
    locations = ~Country,
    color = ~`Electricity from fossil fuels (TWh)`,
    colors = "Blues"
  ) %>%
  layout(title = "Países que usan energía de combustibles fósiles")
fig
## Warning: Ignoring 21 observations
## **Acceso a la electricidad en Colombia a través del (2000-2020)**
# Acceso a la electricidad en promedio global
electricidad_global <- energia_sus %>%
  group_by(Year) %>%
  summarise(`Access to electricity (% of population)` = mean(`Access to electricity (% of population)`, na.rm = TRUE))

fig <- plot_ly(electricidad_global, x = ~Year, y = ~`Access to electricity (% of population)`, type = 'scatter', mode = 'lines') %>%
  layout(title = 'Acceso a la electricidad (2000-2020)', xaxis = list(title = 'Año'), yaxis = list(title = 'Acceso a la electricidad (%)'))
fig
# Acceso a la electricidad en Colombia
energia_colombia <- filter(energia_sus, Country == "Colombia")

electricidad_colombia <- energia_colombia %>%
  group_by(Year) %>%
  summarise(`Access to electricity (% of population)` = mean(`Access to electricity (% of population)`, na.rm = TRUE))

fig <- plot_ly(electricidad_colombia, x = ~Year, y = ~`Access to electricity (% of population)`, type = 'scatter', mode = 'lines') %>%
  layout(title = 'Acceso a la electricidad en Colombia (2000-2020)', xaxis = list(title = 'Año'), yaxis = list(title = 'Acceso a la electricidad (%)'))
fig
## **Países con mayores emisiones de CO2**
# Agrupar y obtener los máximos de emisiones de CO2
maxco2 <- energia_sus %>%
  group_by(Country) %>%
  summarise(CO2 = max(CO2, na.rm = TRUE)) %>%
  arrange(desc(CO2))
## Warning: There were 13 warnings in `summarise()`.
## The first warning was:
## ℹ In argument: `CO2 = max(CO2, na.rm = TRUE)`.
## ℹ In group 12: `Country = "Bahamas"`.
## Caused by warning in `max()`:
## ! ningun argumento finito para max; retornando -Inf
## ℹ Run ]8;;ide:run:dplyr::last_dplyr_warnings()dplyr::last_dplyr_warnings()]8;; to see the 12 remaining warnings.
top10co2 <- head(maxco2, 10)

# Crear el gráfico de barras
fig1 <- plot_ly(
  top10co2,
  x = ~Country,
  y = ~CO2,
  type = 'bar',
  color = ~CO2,
  title = 'Países con mayores emisiones de CO2',
  labels = list(x = 'Países', y = 'Emisiones CO2 (kt)')
)

fig1 <- fig1 %>% layout(height = 800)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
fig1
## Warning: textfont.color doesn't (yet) support data arrays
## Warning: textfont.color doesn't (yet) support data arrays
## Warning: 'bar' objects don't have these attributes: 'title', 'labels'
## Valid attributes include:
## '_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## **Mapa geo-referenciado del porcentaje de población por países con mayores emisiones de CO2**
df_co2 <- energia_sus %>%
  group_by(Country) %>%
  summarise(CO2 = sum(CO2, na.rm = TRUE)) %>%
  arrange(desc(CO2)) %>%
  head(13)

fig2 <- plot_ly(
  df_co2,
  locations = ~Country,
  locationmode = 'country names',
  z = ~CO2,
  type = 'choropleth',
  color = ~CO2,
  colorbar = list(title = 'Emisiones CO2 (kt)'),
  title = 'Países con mayores emisiones de CO2 (2000-2020)'
)

fig2
## Warning: 'choropleth' objects don't have these attributes: 'title'
## Valid attributes include:
## 'autocolorscale', 'coloraxis', 'colorbar', 'colorscale', 'customdata', 'customdatasrc', 'featureidkey', 'geo', 'geojson', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'locationmode', 'locations', 'locationssrc', 'marker', 'meta', 'metasrc', 'name', 'reversescale', 'selected', 'selectedpoints', 'showlegend', 'showscale', 'stream', 'text', 'textsrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'z', 'zauto', 'zmax', 'zmid', 'zmin', 'zsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## **Los principales países por consumo de electricidad a partir de combustibles fósiles (2000-2020)**
consumo_fossil_fuel <- energia_sus %>%
  group_by(Country) %>%
  summarise(Electricity_from_fossil_fuels = sum(`Electricity from fossil fuels (TWh)`, na.rm = TRUE)) %>%
  arrange(desc(Electricity_from_fossil_fuels)) %>%
  head(10)

fig3 <- plot_ly(
  consumo_fossil_fuel,
  x = ~Country,
  y = ~Electricity_from_fossil_fuels,
  type = 'bar',
  title = 'Los principales países por consumo de electricidad a partir de combustibles fósiles (2000-2020)'
)

fig3
## Warning: 'bar' objects don't have these attributes: 'title'
## Valid attributes include:
## '_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## **Histograma del consumo de electricidad a partir de los combustibles fósiles (2000-2020)**
# Filtrar datos para Colombia
energia_colombia <- energia_sus %>% filter(Country == 'Colombia')

# Agrupar por año y calcular el consumo total de electricidad a partir de combustibles fósiles
fosil_colombia <- energia_colombia %>%
  group_by(Year) %>%
  summarise(Electricity_from_fossil_fuels = sum(`Electricity from fossil fuels (TWh)`, na.rm = TRUE))

fig4 <- plot_ly(
  fosil_colombia,
  x = ~Year,
  y = ~Electricity_from_fossil_fuels,
  type = 'bar',
  title = 'Consumo de electricidad a partir de combustibles fósiles en Colombia (2000-2020)',
  labels = list(x = 'Año', y = 'Electricidad de combustibles fósiles (TWh)')
)

fig4
## Warning: 'bar' objects don't have these attributes: 'title', 'labels'
## Valid attributes include:
## '_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## **Tasa de consumo de combustibles fósiles a lo largo de los años para los 10 principales países**
top_fossil_fuel_countries <- energia_sus %>%
  group_by(Country) %>%
  summarise(mean_fossil_fuels = mean(`Electricity from fossil fuels (TWh)`, na.rm = TRUE)) %>%
  arrange(desc(mean_fossil_fuels))

top_fossil_fuel_countries <- head(top_fossil_fuel_countries, 10)


# Filtrar datos para los países seleccionados
filtered_data <- energia_sus %>%
  filter(Country %in% top_fossil_fuel_countries$Country)

fig5 <- plot_ly(
  filtered_data,
  x = ~Year,
  y = ~`Electricity from fossil fuels (TWh)`,
  color = ~Country,
  type = 'scatter',
  mode = 'lines',
  title = 'Tendencias en la Generación de Electricidad de Combustibles Fósiles entre los Principales 10 Países'
)

fig5
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning: 'scatter' objects don't have these attributes: 'title'
## Valid attributes include:
## 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'fillpattern', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'title'
## Valid attributes include:
## 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'fillpattern', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'title'
## Valid attributes include:
## 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'fillpattern', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'title'
## Valid attributes include:
## 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'fillpattern', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'title'
## Valid attributes include:
## 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'fillpattern', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'title'
## Valid attributes include:
## 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'fillpattern', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'title'
## Valid attributes include:
## 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'fillpattern', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'title'
## Valid attributes include:
## 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'fillpattern', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'title'
## Valid attributes include:
## 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'fillpattern', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'title'
## Valid attributes include:
## 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'fillpattern', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## **Tendencias en la generación de electricidad de combustibles fósiles entre los 10 principales países**
# Asegurar que Colombia esté en la lista de países seleccionados
# Asegurar que Colombia esté en la lista de países seleccionados
top_fossil_fuel_countries <- energia_sus %>%
  group_by(Country) %>%
  summarise(mean_fossil_fuels = mean(`Electricity from fossil fuels (TWh)`, na.rm = TRUE)) %>%
  arrange(desc(mean_fossil_fuels)) %>%
  slice(1:3)  # Selecciona los 3 principales países

# Filtrar los datos para los países seleccionados (top 3 + Colombia)
filtered_data <- energia_sus %>%
  filter(Country %in% top_fossil_fuel_countries$Country)

# Crear el gráfico de líneas para mostrar la tendencia en el uso de electricidad de combustibles fósiles
fig <- plot_ly(
  data = filtered_data,
  x = ~Year,
  y = ~`Electricity from fossil fuels (TWh)`,
  color = ~Country,
  type = 'scatter',
  mode = 'lines+markers'
) %>%
  layout(
    title = 'Tendencias en la Generación de Electricidad de Combustibles Fósiles entre los Principales 10 Países y Colombia',
    xaxis = list(title = 'Año'),
    yaxis = list(title = 'Electricidad de Combustibles Fósiles (TWh)')
  )

fig
# Definir los países que se quieren visualizar
paises <- c('United States', 'Colombia', 'China', 'Brazil')

# Filtrar los datos para los países seleccionados
tenden_co2 <- energia_sus %>% 
  filter(Country %in% paises)

# Crear el gráfico de líneas para mostrar la tendencia de emisiones de CO2
ggplot(tenden_co2, aes(x = Year, y = CO2, color = Country)) +
  geom_line() +
  ggtitle('Tendencia de Emisiones de CO2 para Estados Unidos, Colombia, China y Brasil') +
  theme_minimal()
## Warning: Removed 4 rows containing missing values or values outside the scale range
## (`geom_line()`).

# **Tendencia de emisiones de CO2 para Colombia y Brasil**
# Definir los países que se quieren visualizar
paises <- c('Colombia', 'Brazil', 'Argentina', 'Chile', 'Ecuador', 'Uruguay')

# Filtrar los datos para los países seleccionados
tenden_co2 <- energia_sus %>% 
  filter(Country %in% paises)

# Crear el gráfico de líneas para mostrar la tendencia de emisiones de CO2
ggplot(tenden_co2, aes(x = Year, y = CO2, color = Country)) +
  geom_line() +
  ggtitle('Tendencia de Emisiones de CO2 para Colombia y Brasil') +
  theme_minimal()
## Warning: Removed 6 rows containing missing values or values outside the scale range
## (`geom_line()`).