---
title: "R Markdown Week 1"
author: "Andrew Leach"
date: "2023-09-05"
output: html_document
editor: source
---


```{r packages-data,echo=T,output=F, warning=FALSE,message=FALSE}
#chunk options suppress output and warnings
library(tidyverse)
library(kableExtra)
library(readxl)
library(janitor)
```

```{r read data,echo=T,output=F,message=F,warning=FALSE}
#check to see if the file exists. The first line after the if will only run if it doesn't
#you don't need this line, but it's a nice efficiency improvement for your code and one that I should use more often
if(!file.exists("bp_2023.xlsx"))
#we'll download the data giving it a local file name that's easy to deal with - remember to use the mode="wb" so that you get the windows binary file
    download.file("https://www.energyinst.org/__data/assets/excel_doc/0007/1055545/EI-stats-review-all-data.xlsx",destfile = "bp_2023.xlsx",mode="wb")
#and now, let's read the data in, but specify the range for the data to read in from the Excel that you have open!
bp_data<-read_excel("bp_2023.xlsx",sheet = "Primary Energy - Cons by fuel",range="A3:O94")%>%clean_names()
```


```{r clean_data,cache=TRUE}
#select is for picking columns. You can use it with numbers or names
#right now, our names aren't clear, so let's use numbers
bp_data<-bp_data %>% select(1,seq(9,15))
#assign appropriate names
names(bp_data)<-c("country","Oil","Natural Gas","Coal","Nuclear","Hydro","Renewables","Total")
#make sure they're "clean"
bp_data<-bp_data%>%clean_names()
#take out missing rows
bp_data<-bp_data%>%filter(!is.na(country))
#keep the regional totals only
bp_data<-bp_data%>%filter(grepl("Total",country))
#strip the word total and the space from the categories
bp_data<-bp_data%>%mutate(country=gsub("Total ","",country))

```

```{r, echo = T,eval=T}
#quick table to show the data
  bp_data %>%
    kbl(escape = FALSE) %>%
    kable_styling(fixed_thead = T,bootstrap_options = c("hover", "condensed","responsive"))%>%
  #scroll_box(width = "1000px", height = "400px")%>%
#adding this I() at the end of a chain of commands is like a placeholder in case you would otherwise end with a %>%. 
# It makes it easier to paste in code from elsewhere
      I() 
```

```{r long_df, echo = T,eval=T}
# we're going to pivot the data to a longer style, by country
# we're going to end up values going to a variable called tpes and names going to a variable called source
  bp_data<-bp_data%>%pivot_longer(-country,names_to = "source", values_to = "tpes")
```


```{r, echo = T,eval=T,fig.width=15, fig.height=9,dpi=200}

library(ggthemes)
library(tools)
bp_data %>%
  filter(country!="World")%>% #keep everything but global observations
  filter(source!="total")%>% #don't include the totals
  mutate(source=toTitleCase(gsub("_"," ",source)))%>% #strip out the underscore, convert to title case
  mutate(source=as_factor(source))%>% #make the sources factors in the order they appear in the data
  mutate(source=fct_relevel(source,"Coal"))%>% #use fct-relevel to make coal the first factor level
  mutate(source=fct_rev(source))%>% #reverse the order
  #take out the global observations and total column

  ggplot()+
  geom_col(aes(country,tpes,fill=source),color="black",linewidth=.25)+
  theme_economist_white()+#for fun, let's use the economist theme
  guides(fill=guide_legend(nrow = 1))+
  theme(legend.position = "bottom")+
  scale_fill_manual("",values=c("Coal"="black","Oil"="grey30","Natural Gas"="grey70","Nuclear"="orange","Hydro"="dodgerblue","Renewables"="darkgreen"))+
  theme(text = element_text(size=16))+
  labs(y="Primary Energy Consumption (Exajoules)",x="",
       title="Primary Energy Consumption by Region and Source, 2023",
       caption="Source: BP Statistical Review of World Energy")
  
```
