── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.3 ✔ 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(dplyr)library(ggplot2)# the URL of our data on GitHubgithub_url <-"https://raw.githubusercontent.com/t-emery/sais-susfin_data/main/datasets/etf_comparison-2022-10-03.csv"# read the data from GitHubblackrock_esg_vs_non_esg_etf <- github_url |>read_csv() |># select the four columns we will use in our anlaysis hereselect(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.
Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
-Inf
##1.5.1.3blackrock_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
blackrock_esg_vs_non_esg_etf_long %>%##filter is the tool to change the weight,filter(weight >= 1L & weight <= 7L) %>%ggplot() +##for aes we basically set up all the variable that we need.aes(x = weight,y = company_name,colour = fund_type,size = weight ) +##here we go a point chart with shape of each point 'circle'geom_point(shape ="circle") +##set up the point color for different variablesscale_color_manual(values =c(`ESG ETF (ESGU)`="#15C179",`Standard ETF (IVV)`="#A6A59F") ) +##we add title herelabs(title ="Shiyu Liao", subtitle ="developing ESG") +theme_bw() +##theme for my titletheme(plot.title =element_text(size = 19L,face ="bold",hjust =0.5),plot.subtitle =element_text(hjust =0.5) )
##This chart tells us how many ETFs of these major companies are based on ESG##and how many are not based on ESG. Several technology companies are very large ##in this picture. This is a big problem because these technology companies have##a great impact on ESG, which greatly aggravates the risk of developing ESG. ##And because the point is too large, I can’t see Apple’s weight clearly.
##1.5.1.4blackrock_esg_vs_non_esg_etf %>%filter(esg_etf >=0.9& esg_etf <=7) %>%##it's a filter to filt in the esg etf above and bwloe 0.9,7.ggplot() +aes(x = company_name, y = esg_etf, fill = sector) +##we basically build a col chartgeom_col() +scale_fill_hue(direction =1) +##label part, I set up the title and the author's namelabs(title ="Company above 1 weight in esg market",subtitle ="by Jack" ) +##size, face of the titletheme_minimal() +theme(plot.title =element_text(size = 20L, face ="bold", hjust =0.5),axis.text.x =element_text(angle =45, vjust =1, hjust=1, size =10) )
##Second oneblackrock_esg_vs_non_esg_etf_long %>%filter(weight >=0.9& weight <=7) %>%ggplot() +aes(x = company_name, fill = fund_type, weight = weight) +geom_bar() +scale_fill_hue(direction =1) +labs(title ="Compare the weight of each type of fund",subtitle ="by jack" ) +theme_minimal() +theme(plot.title =element_text(size = 20L, face ="bold", hjust =0.5),axis.text.x =element_text(angle =45, vjust =1, hjust=1, size =10) )
##1.5.2.1library(ggplot2)ggplot(blackrock_esg_vs_non_esg_etf) +aes(x = esg_etf, y = standard_etf, colour = sector, group =1) +##set up the x, y, colorgeom_point(shape ="circle", size =1.5) +geom_smooth(method ="lm", formula = y ~poly(x, 3, raw =TRUE)) +##here we set up a line, in order to make it curve, we do poly.scale_color_hue(direction =1) +scale_x_continuous(trans ="log10") +##basic logscale_y_continuous(trans ="log10") +theme_minimal()## theme of our title
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
Warning: The following aesthetics were dropped during statistical transformation: colour
ℹ This can happen when ggplot fails to infer the correct grouping structure in
the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
variable into a factor?
##simple one we just basically set up the smooth and logggplot(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") +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'
ggplot(blackrock_esg_vs_non_esg_etf) +aes(x = esg_etf, y = standard_etf, colour = sector, group =1) +geom_point(shape ="circle", size =1.5, colour ="purple") +##basically we only need purple geom_smooth(method ="lm", formula = y ~poly(x, 3, raw =TRUE), colour ="yellow") +##set the color for smooth linescale_color_manual(values ="purple") +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
##The key change made was applying a logarithmic transformation to both the ##x-axis and y-axis of the plot. This transformation helps to better visualize ##the data when it is highly concentrated in a small range of values, ##effectively spreading out the data points and allowing for clearer interpretation of trends and relationships.
Last question
install.packages(“ggthemes”)library(ggthemes)ggplot(blackrock_esg_vs_non_esg_etf) + aes(x = esg_etf, y = standard_etf, colour = sector, group = 1) + geom_point(shape = 21, size = 2, fill = “white”) + geom_smooth(method = “lm”, formula = y ~ poly(x, 3, raw = TRUE), colour = “blue”) + scale_color_hue(direction = 1) + scale_x_continuous(trans = “log10”) + scale_y_continuous(trans = “log10”) + theme_excel() + labs(title = “Relationship between ESG and Standard ETFs”, x = “ESG ETF”, y = “Standard ETF”)
##The theme function in ggplot2 allows you to personalize the visual aspects of your plot, such as background, axis ticks, and legend placement. With various themes, you can adjust the plot’s style to your liking or to fit specific design needs. In this example, the theme_excel() function from the ggthemes package is utilized to give the plot a resemblance to Microsoft Excel’s appearance.