library(tidyverse)
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'stringr' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── 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(esquisse)
## Warning: package 'esquisse' was built under R version 4.2.3
library(ggplot2)
# the URL of our data on GitHub
github_url <- "https://raw.githubusercontent.com/t-emery/sais-susfin_data/main/datasets/etf_comparison-2022-10-03.csv"
# read the data from GitHub
blackrock_esg_vs_non_esg_etf <- github_url %>%
read_csv() %>%
# select the four columns we will use in our anlaysis here
select(company_name:standard_etf)
## Rows: 537 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): ticker, company_name, sector, esg_uw_ow
## dbl (7): esg_etf, standard_etf, esg_tilt, esg_tilt_z_score, esg_tilt_rank, e...
## lgl (3): in_esg_only, in_standard_only, in_on_index_only
##
## ℹ 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.
blackrock_esg_vs_non_esg_etf %>%
glimpse()
## Rows: 537
## Columns: 4
## $ company_name <chr> "PRUDENTIAL FINANCIAL INC", "GENERAL MILLS INC", "KELLOGG…
## $ sector <chr> "Financials", "Consumer Staples", "Consumer Staples", "In…
## $ esg_etf <dbl> 0.5366803, 0.5522180, 0.4534279, 0.6486836, 0.4407025, 0.…
## $ standard_etf <dbl> 0.10574313, 0.15134370, 0.05920732, 0.31168123, 0.1184507…
#code for the chart recreation
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_hue(direction = 1) +
scale_x_continuous(trans = "log10") +
scale_y_continuous(trans = "log10") +
labs(x = "ESG ETF (ESGU)", y = "Standard ETF (IVV)", title = "I made this chart using Esquisse",
subtitle = "It is a powerful ggplot2 GUI", caption = "Jiayi Ding") +
theme_minimal() +
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()`).
#1.5.1.2 Homework problem 2: exploring the outliers
blackrock_esg_vs_non_esg_etf_long <- blackrock_esg_vs_non_esg_etf %>%
# we'll learn a lot more about long data & pivot_longer() in future weeks.
pivot_longer(cols = contains("etf"), names_to = "fund_type", values_to = "weight") |>
# case_when() is like an extended "if else"
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 × 4
## company_name sector fund_type weight
## <chr> <chr> <chr> <dbl>
## 1 PRUDENTIAL FINANCIAL INC Financials ESG ETF (ESGU) 0.537
## 2 PRUDENTIAL FINANCIAL INC Financials Standard ETF (IV… 0.106
## 3 GENERAL MILLS INC Consumer Staples ESG ETF (ESGU) 0.552
## 4 GENERAL MILLS INC Consumer Staples Standard ETF (IV… 0.151
## 5 KELLOGG Consumer Staples ESG ETF (ESGU) 0.453
## 6 KELLOGG Consumer Staples Standard ETF (IV… 0.0592
## 7 AUTOMATIC DATA PROCESSING INC Information Technology ESG ETF (ESGU) 0.649
## 8 AUTOMATIC DATA PROCESSING INC Information Technology Standard ETF (IV… 0.312
## 9 ECOLAB INC Materials ESG ETF (ESGU) 0.441
## 10 ECOLAB INC Materials Standard ETF (IV… 0.118
## # ℹ 1,064 more rows
## Write a short reflection (a few sentences) on how to interpret this
chart, and what it tells you about the large outliers in the dataset.
The large outliers are apple inc., Microsoft Corp., Alphabet Inc and
Amazon com Inc. These are all relative huge tech firms. The chart also
shows that the weight of standard ETF is larger than the weight of ESG
ETF.
The chart shows a comparison between the weight of standard ETF and the weight of ESG ETF among companies. For large firms, the weight is almost the same between standard and ESG. However, many firms obsviously did not adopt ESG ETF.
## 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
## Warning: Transformation introduced infinite values in continuous x-axis
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 261 rows containing non-finite values (`stat_smooth()`).
#Chart 1 annotation: geom_point() adds the scatter points with colors representing sectors. geom_smooth() adds the regression line with a shaded standard error region. scale_y_log10() and scale_x_log10() apply logarithmic transformations to both axes. labs() adds appropriate labels and a title. theme_bw() applies a theme with a white background. theme(legend.position = “right”) positions the legend on the right side of the plot.
## 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
## Warning: Transformation introduced infinite values in continuous x-axis
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 261 rows containing non-finite values (`stat_smooth()`).
#Chart 2 Annotation: geom_point() adds a scatter plot layer with points colored by sector. geom_smooth() adds a non-linear loess smoothed curve for each sector with standard error shading. scale_y_log10() and scale_x_log10() apply a logarithmic transformation to the y and x axes respectively, with labels formatted as log-scaled. theme_classic() applies a classic ggplot theme with a white background and no gridlines. theme(legend.position = “right”) moves the legend to the right side of the plot.
## 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
## Warning: Transformation introduced infinite values in continuous x-axis
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 261 rows containing non-finite values (`stat_smooth()`).
## Warning: Transformation introduced infinite values in continuous x-axis
## Warning: Transformation introduced infinite values in continuous y-axis