##Installing packages if needed##install.packages("tidyverse")##install.packages("ggplot2")##install.packages("esquisse")##install.packages("dplyr")##install.packages("tidyr")##install.packages("ggridges")##install.packages("forcats")##install.packages('gganimate')##Importing required libraries and packageslibrary(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ 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(ggplot2)library(esquisse)library(dplyr)
##Importing the Blackrock Datalibrary(tidyverse)library(ggplot2)library(esquisse)library(dplyr)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() |>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.
##Using Esquisse##esquisser(blackrock_esg_vs_non_esg_etf)##Recreating the graphggplot(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") +geom_abline()+#add a 45degree line manuallylabs(x ="ESG ETF (ESGU)",y ="Standard ETF (IVV)",title ="We made this chart using Esquisse!",subtitle ="It's a great invention to learn about ggplot2, even with its limitations!",caption ="Yuhao Shen made this!" ) +theme_minimal() +theme(plot.title =element_text(size = 14L,face ="bold"),plot.caption =element_text(face ="bold.italic"),axis.title.y =element_text(face ="bold"),axis.title.x =element_text(face ="bold") ) +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'
##Recreating the graph (without any error codes)ggplot(blackrock_esg_vs_non_esg_etf) +aes(x =log10(esg_etf +1), y =log10(standard_etf +1)) +geom_point(aes(colour = sector), shape ="circle", size =1.5) +geom_smooth(method ='lm') +scale_color_hue(direction =1) +labs(x ="ESG ETF (ESGU)",y ="Stadard ETF (IVV)",title ="Recreating ESG vs non-ESG across sectors",subtitle ="Using esquisse",caption ="Made by Yuhao Shen" ) +theme_minimal() +theme(plot.title =element_text(size = 14L,face ="bold"),plot.caption =element_text(face ="bold.italic"),axis.title.y =element_text(face ="bold"),axis.title.x =element_text(face ="bold") ) +facet_wrap(vars(sector))
`geom_smooth()` using formula = 'y ~ x'
Homework 2: exploring the outliers
##Changing data from wide to longblackrock_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
##esquisser(blackrock_esg_vs_non_esg_etf_long)##Reproducing Esquisse Graphslibrary(dplyr)library(ggplot2)blackrock_esg_vs_non_esg_etf_long %>%filter(weight >= 1L & weight <= 7L) %>%#filter out datapoints that are above 1%ggplot() +aes(x = weight,y = company_name,colour = fund_type,size = weight ) +geom_point(shape ="circle") +#scatter plotscale_color_manual(values =c(`ESG ETF (ESGU)`="#76F856",`Standard ETF (IVV)`="#C9C9C9") ) +#changing the colors that represent different fundslabs(x ="Weight (%)",y ="Company Names",title ="Portfolio Weight Distribution by Company in ESG vs. Standard ETFs",subtitle ="with companies over 1% weight",caption ="Yuhao Shen made this!",color ="Fund Type",size ="Weight (%)" ) +#labeling legendstheme_minimal() +theme(plot.title =element_text(face ="bold"),plot.caption =element_text(face ="italic") ) #changing the fonts of the title and caption
Implication of the chart
This scatter plot is showing us the weight distribution of the companies in the ESG and non-ESG funds. The larger points suggest larger weights in the funds where the colors represent different types of funds. Noticeably, both funds seem to have adopted similar strategies selecting weights in these large cap companies.
Homework 3: Making Your Own Graphs
First Graph
##esquisser(blackrock_esg_vs_non_esg_etf_long)##Reproducing Esquisse Graphslibrary(dplyr)library(ggplot2)blackrock_esg_vs_non_esg_etf_long %>%filter(sector %in%c("Financials", "Utilities", "Real Estate")) %>%filter(weight >=0.1& weight <=1) %>%#filter for selceted sectors and weightsggplot() +aes(x = weight,y = company_name,colour = fund_type, #color coding the ESG and Non-ESG fundssize = weight #size of points shows graphically the size of weights ) +geom_point(shape ="circle") +#scatter plotscale_color_hue(direction =1) +labs(x ="Weight Distribution (%)",y ="Company Name",title ="Comparison of ESG Commitment Across Three Sectors",subtitle ="Looking at weights that are between 0 to 1% in the funds",caption ="Made by Yuhao Shen",color ="Fund Type",size ="Weight" ) +theme_minimal() +theme(plot.title =element_text(face ="bold"),plot.subtitle =element_text(face ="italic"),plot.caption =element_text(face ="bold.italic") ) +facet_wrap(vars(sector)) #facet by the different sectors
Implication of the chart
This scatter plot is showing us the weight distribution of the small percentage companies (0% to 1% weight) in the ESG and non-ESG funds. The larger points suggest larger weights in the funds where the colors represent different types of funds. Noticeably, we can see that the two funds seem to have a more different strategy investing in smaller percentage firms.
ggplot(blackrock_esg_vs_non_esg_etf_long) +aes(x =log10(weight +1), y = sector) +#fixing for values of log10(0)geom_point(aes(colour = fund_type))+geom_boxplot(fill ="#112446") +scale_color_manual(values =c(`ESG ETF (ESGU)`="#76F856",`Standard ETF (IVV)`="#C9C9C9") ) +labs(x ="Weight (log10)",y ="Sectors",title ="Box Plot Showing How Fund Strategies Differ",subtitle ="with log10 x(weight) scale ",caption ="Made by Yuhao Shen",color ="Fund Type" ) +theme_minimal() +theme(plot.title =element_text(face ="bold"),plot.subtitle =element_text(face ="italic"),plot.caption =element_text(face ="bold.italic") )
Implication of the chart
This box plot is showing us the distribution of the companies in the ESG and non-ESG funds. We can see that ESG in general has a similar interquartile range of distribution to that of a non-ESG funds, which leads us to think about how different are these two types of funds.
Homework 4
Reproduction of ‘elegantaly’ fixed graph 1
#?geom_smoothggplot(blackrock_esg_vs_non_esg_etf) +aes(x =log10(esg_etf +1), y =log10(standard_etf +1)) +geom_point(shape ='circle', size =1.5, aes(colour = sector)) +#Since we want points to be colored based on species but don’t want the lines #to be separated out for them, we should specify color = species for geom_point() only.geom_smooth(method ="lm", formula = y ~poly(x, 3), se =TRUE) +labs(x ="ESG ETF (ESGU)",y ="Stadard ETF (IVV)", )
Reproduction of non-fixed graph 2
ggplot(blackrock_esg_vs_non_esg_etf) +aes(x = esg_etf, y = standard_etf, color = sector) +#having the color here means we're passing down the different labels to geom_pointsgeom_point(shape ='circle', size =1.5) +geom_smooth(method ="lm", formula = y ~poly(x, 3), se =TRUE) +labs(x ="ESG ETF (ESGU)",y ="Standard ETF (IVV)", ) +scale_x_log10()+scale_y_log10()
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
ggplot(blackrock_esg_vs_non_esg_etf) +aes(x = esg_etf, y = standard_etf) +#NO color coding means that are points are treated as onegeom_point(shape ='circle', size =1.5, color ="purple") +geom_smooth(method ="lm", formula = y ~poly(x, 3), se =TRUE, color ="yellow") +labs(x ="ESG ETF (ESGU)",y ="Standard ETF (IVV)", ) +scale_x_log10()+scale_y_log10()
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
library(ggridges)library(ggplot2)library(dplyr)library(tidyr)library(forcats)#?scale_fill_viridis_bggplot(blackrock_esg_vs_non_esg_etf_long, aes(x = weight, y = sector, fill = ..x..)) +geom_density_ridges_gradient(scale =3, rel_min_height =0.01) +scale_fill_viridis_b(name ="Weight, %", option ="B") +labs(title ='Ridgeline plot showing frequency of investment in each sectors')
Warning: The dot-dot notation (`..x..`) was deprecated in ggplot2 3.4.0.
ℹ Please use `after_stat(x)` instead.
Picking joint bandwidth of 0.0435
Animated :( Tried and failed
library(gganimate)
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