Homework 1 (Sustainable Finance)

Author

Adam Savino

options(repos = c(CRAN = "https://cran.rstudio.com/"))

install.packages("esquisse")
Installing package into 'C:/Users/adams/AppData/Local/R/win-library/4.3'
(as 'lib' is unspecified)
package 'esquisse' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\adams\AppData\Local\Temp\RtmpOq5cdK\downloaded_packages
library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.3.2
github_url <- "https://raw.githubusercontent.com/t-emery/sais-susfin_data/main/datasets/etf_comparison-2022-10-03.csv"
blackrock_esg_vs_non_esg_etf <- github_url |>
  read.csv()
library(ggplot2)

#Homework Problem 1

ggplot(blackrock_esg_vs_non_esg_etf) +
 aes(x = esg_etf, y = standard_etf, colour = sector) +
 geom_point(shape = "circle", 
 size = 1.5) +
 geom_smooth(span = 0.75) +
 scale_color_viridis_d(option = "plasma", direction = 1) +
 scale_x_continuous(trans = "log10") +
 scale_y_continuous(trans = "log10") +
 labs(x = "ESG ETF (ESGU)", 
 y = "Standard ETF (IVV)", title = "We made this chart using Esquisse!", subtitle = "It's a great tool for learning ggplot2. Even if it has limitations.", 
 caption = "I made this!") +
 theme_minimal() +
 theme(legend.position = "none") +
 facet_wrap(vars(sector))
Warning: Transformation introduced infinite values in continuous x-axis
Warning: Transformation introduced infinite values in continuous y-axis
Warning: Transformation introduced infinite values in continuous x-axis
Warning: Transformation introduced infinite values in continuous y-axis
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 261 rows containing non-finite values (`stat_smooth()`).

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.3.2
Warning: package 'dplyr' was built under R version 4.3.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.3     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ lubridate 1.9.2     ✔ tibble    3.2.1
✔ purrr     1.0.2     ✔ tidyr     1.3.0
── 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
library(dplyr)
library(ggplot2)


#Homework Problem 2:

blackrock_esg_vs_non_esg_etf_long <- blackrock_esg_vs_non_esg_etf |>
  pivot_longer(cols = contains("etf"), names_to = "fund_type", values_to = "weight") |> 
  mutate(fund_type = case_when(fund_type == "esg_etf" ~ "ESG ETF (ESGU)", fund_type == "standard_etf" ~ "Standard ETF (IVV)"))

blackrock_esg_vs_non_esg_etf_long
# A tibble: 1,074 × 14
   ticker company_name      sector in_esg_only in_standard_only in_on_index_only
   <chr>  <chr>             <chr>  <lgl>       <lgl>            <lgl>           
 1 PRU    PRUDENTIAL FINAN… Finan… FALSE       FALSE            FALSE           
 2 PRU    PRUDENTIAL FINAN… Finan… FALSE       FALSE            FALSE           
 3 GIS    GENERAL MILLS INC Consu… FALSE       FALSE            FALSE           
 4 GIS    GENERAL MILLS INC Consu… FALSE       FALSE            FALSE           
 5 K      KELLOGG           Consu… FALSE       FALSE            FALSE           
 6 K      KELLOGG           Consu… FALSE       FALSE            FALSE           
 7 ADP    AUTOMATIC DATA P… Infor… FALSE       FALSE            FALSE           
 8 ADP    AUTOMATIC DATA P… Infor… FALSE       FALSE            FALSE           
 9 ECL    ECOLAB INC        Mater… FALSE       FALSE            FALSE           
10 ECL    ECOLAB INC        Mater… FALSE       FALSE            FALSE           
# ℹ 1,064 more rows
# ℹ 8 more variables: esg_tilt <dbl>, esg_uw_ow <chr>, esg_tilt_z_score <dbl>,
#   esg_tilt_rank <int>, esg_tilt_percentile <dbl>, esg_tilt_quantile_5 <int>,
#   fund_type <chr>, weight <dbl>
blackrock_esg_vs_non_esg_etf_long %>%
#The line under this filers out weights under 1%
 filter(weight >= 1L & weight <= 7L) %>%
 ggplot() +
#The aes grouping defines weight as the x variable and specific companies as the y variable, with the color based on fund type. The size of the circles on the dot plot are determined by weight, though this is arguably unnecessary.
 aes(x = weight, y = company_name, colour = fund_type, size = weight) +
 #Geom point is what tells us our graph is a scatter plot. The scale color manual was used to make the ESG fund stand out as green and the normal one gray.The labs are just the labels of the graph. 
geom_point(shape = "circle") +
 scale_color_manual(values = c(`ESG ETF (ESGU)` = "#0DF905", `Standard ETF (IVV)` = "#A0A0A0"
)) +
 labs(x = "Weight", y = "Company Name", title = "Companies with a Weight above 1% in ESGU and IVV ETFs", 
 subtitle = "How much each ETF factors in", caption = "By Adam Savino") +
 theme_minimal()

#The companies with a particularly large presence in th ETFs may dominate their industries and be particularly stable due to thier position in the economy. Google (aka Alphabet) isn't going anywhere soon. It makes sense as a safe investment.

#Question 3:


library(ggplot2)

ggplot(blackrock_esg_vs_non_esg_etf) +
 aes(x = sector, y = esg_etf, fill = sector, colour = sector) +
 geom_col() +
 scale_fill_hue(direction = 1) +
 scale_color_hue(direction = 1) +
 labs(x = "Sector", 
 y = "Amount in the ESG ETF", title = "Amount of Companies in the ESG ETFper Sector", caption = "By Adam Savino") +
 theme_minimal()

#The code for this starts by defining the x variable (business sector) and the y variable (ESG ETF). It then charts how many companies in each sector make up the ETF by taking the form of the bar graph through the geom col function. I also used the labs function to title the graph and the axes.


library(ggplot2)

ggplot(blackrock_esg_vs_non_esg_etf) +
 aes(x = esg_etf, y = standard_etf, colour = standard_etf) +
 geom_point(shape = "circle", 
 size = 2.8) +
 scale_color_viridis_c(option = "plasma", direction = 1) +
 labs(x = "ESG ETF Concentration", 
 y = "Standard ETF Concentration", title = "A Comparison of Companies on the Standard ETF and the ESG ETF", 
 subtitle = "Mapping the presence of companies in the standard ETF", caption = "By Adam Savino") +
 theme_minimal()

#I used a scatterplot to see the relationship between the normal ETF and the ESG ETF in terms of company-by-company concentration, but I used the color changing ability to make the dots representing a company get lighter as they had a greater presence in the normal ETF.
#As per usual, the ggplot function begins the graph, you define the x (esg etf) and y (standard etf) variables, you map the color to the standard etf and change said color to plasma to act like "heat", you use geom point to get a scatterplot, increase the size of the circle to 2.8, and use the labs function to add the captions and title.

#Question 4:

library(ggplot2)

#the ggplot function defines the data source, then defines the x and y variables as well as what other variable color is assigned to.
ggplot(blackrock_esg_vs_non_esg_etf) +
  aes(x = standard_etf, y = esg_etf, colour = sector) +
#geom point is what makes it a scatter plot. The shape is the standard circle rather than a square or something else.
  geom_point(shape = "circle", 
             size = 1.5) +
  #geom smooth creates lines that fit the scatterplot to demonstrate the relationship between the two variables. In this graph, the lines are divided by sector.
  geom_smooth(span = 0.75) +
  scale_color_hue(direction = 1) +
  scale_x_continuous(trans = "log10") +
  scale_y_continuous(trans = "log10") +
  theme_minimal()
Warning: Transformation introduced infinite values in continuous x-axis
Warning: Transformation introduced infinite values in continuous y-axis
Warning: Transformation introduced infinite values in continuous x-axis
Warning: Transformation introduced infinite values in continuous y-axis
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 261 rows containing non-finite values (`stat_smooth()`).

ggplot(blackrock_esg_vs_non_esg_etf) +
  aes(x = standard_etf, y = esg_etf) +
#Notice that the filling for geom point is different from it used to be, and that we took the color assignment outside of ggplot. 
  geom_point(mapping = aes(color = sector)) +
  geom_smooth() +
  scale_color_hue(direction = 1) +
  scale_x_continuous(trans = "log10") +
  scale_y_continuous(trans = "log10") +
  theme_minimal()
Warning: Transformation introduced infinite values in continuous x-axis
Transformation introduced infinite values in continuous y-axis
Warning: Transformation introduced infinite values in continuous x-axis
Warning: Transformation introduced infinite values in continuous y-axis
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 261 rows containing non-finite values (`stat_smooth()`).

#I changed this one from the first one by mapping the points so that the geom smooth function wouldn't create a line for all sectors individually.

library(ggplot2)

ggplot(blackrock_esg_vs_non_esg_etf) +
 aes(x = esg_etf, y = standard_etf) +
 geom_point(shape = "circle", 
 size = 1.5, colour = "#4008E6") +
 geom_smooth(span = 0.75, colour = "gold1") +
 scale_x_continuous(trans = "log10") +
 scale_y_continuous(trans = "log10") +
 theme_minimal()
Warning: Transformation introduced infinite values in continuous x-axis
Transformation introduced infinite values in continuous y-axis
Warning: Transformation introduced infinite values in continuous x-axis
Warning: Transformation introduced infinite values in continuous y-axis
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 261 rows containing non-finite values (`stat_smooth()`).

#This one works by no longer assigning color to the kind of sector, but instead assigning colors to the dots (purple) and the line (gold)

#Question 5

ggplot(blackrock_esg_vs_non_esg_etf) +
  aes(
    x = in_esg_only,
    fill = sector,
    colour = sector,
    weight = esg_etf
  ) +
  geom_bar() +
  scale_fill_hue(direction = 1) +
  scale_color_hue(direction = 1) +
  labs(
    x = "In ESG Exclusively",
    y = "In ESG in General",
    title = "Exclusivity of Companies in the ESG ETF (By Sector)",
    caption = "By Adam Savino"
  ) +
  theme_minimal()

#This is interesting to me because it demonstrates that most of the companies on the ESG ETF are also on the normal one, and also that most of the overlapping companies are in IT.

#Question 6

install.packages("ggthemes")
Installing package into 'C:/Users/adams/AppData/Local/R/win-library/4.3'
(as 'lib' is unspecified)
package 'ggthemes' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\adams\AppData\Local\Temp\RtmpOq5cdK\downloaded_packages
install.packages("gganimate")
Installing package into 'C:/Users/adams/AppData/Local/R/win-library/4.3'
(as 'lib' is unspecified)
package 'gganimate' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\adams\AppData\Local\Temp\RtmpOq5cdK\downloaded_packages
library(ggthemes)
Warning: package 'ggthemes' was built under R version 4.3.2
library(gganimate)
Warning: package 'gganimate' was built under R version 4.3.2
No renderer backend detected. gganimate will default to writing frames to separate files
Consider installing:
- the `gifski` package for gif output
- the `av` package for video output
and restarting the R session
install.packages("patchwork")
Installing package into 'C:/Users/adams/AppData/Local/R/win-library/4.3'
(as 'lib' is unspecified)
package 'patchwork' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\adams\AppData\Local\Temp\RtmpOq5cdK\downloaded_packages
library(patchwork)
Warning: package 'patchwork' was built under R version 4.3.2
g1 <- ggplot(blackrock_esg_vs_non_esg_etf) +
  aes(standard_etf, esg_etf, color = sector) + geom_point()

g2 <- 
  ggplot(blackrock_esg_vs_non_esg_etf) +
  aes(sector, esg_etf) + geom_boxplot()

g1 + g2

#I used the patchwork function to combine two simple graphs. I also downloaded the animate capabilities and realized I was not yet prepared to use it properly. 

The echo: false option disables the printing of code (only output is displayed).