```{install.packages(“esquisse”)} install.packages(“tidyverse”)

library(esquisse) library(tidyverse)

library(ggplot2)

library(dplyr) 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)

ggplot(blackrock_esg_vs_non_esg_etf) + aes(x = esg_etf, y = standard_etf, colour = sector) + geom_point(shape = “bullet”, size = 1.5) + geom_smooth(span = 0.54) + scale_color_manual(values = c(Communication = “#F8766D”, Consumer Discretionary = “#DA8C15”, Consumer Staples = “#ACA000”, Energy = “#58B016”, Financials = “#00BB4C”, Health Care = “#00C19F”, Industrials = “#00BAD5”, Information Technology = “#3AA7F3”, Materials = “#918BFD”, Real Estate = “#E26EEF”, Utilities = “#FF61C3”)) + scale_x_continuous(trans = “log10”) + scale_y_continuous(trans = “log10”) + labs(title = “Shiyu Liao”, subtitle = “Good!!!”) + theme_classic() + theme(plot.title = element_text(size = 17L, face = “bold”, hjust = 0.5)) + facet_wrap(vars(sector))

##1.5.1.3 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

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 variables scale_color_manual( values = c(ESG ETF (ESGU) = “#15C179”, Standard ETF (IVV) = “#A6A59F”) ) + ##we add title here labs(title = “Shiyu Liao”, subtitle = “developing ESG”) + theme_bw() + ##theme for my title theme( 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.4 esquisser() blackrock_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 chart geom_col() + scale_fill_hue(direction = 1) + ##label part, I set up the title and the author’s name labs( title = “Company above 1 weight in esg market”, subtitle = “by Jack” ) + ##size, face of the title theme_minimal() + theme( plot.title = element_text(size = 20L, face = “bold”, hjust = 0.5) )

##Second one blackrock_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) )

##1.5.2.1

library(ggplot2) ggplot(blackrock_esg_vs_non_esg_etf) + aes(x = esg_etf, y = standard_etf, colour = sector, group = 1) +##set up the x, y, color geom_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 log scale_y_continuous(trans = “log10”) + theme_minimal()## theme of our title

##simple one we just basically set up the smooth and log 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”) + theme_minimal()

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 line scale_color_manual(values = “purple”) + scale_x_continuous(trans = “log10”) + scale_y_continuous(trans = “log10”) + theme_minimal() ##1.5.3

library(ggplot2) ggplot(blackrock_esg_vs_non_esg_etf) + aes(x = esg_etf, y = standard_etf, group = 1) + geom_point(shape = 21, size = 2, colour = “black”, fill = “white”) + geom_smooth(method = “lm”, formula = y ~ x, colour = “blue”, se = FALSE) + scale_x_continuous(trans = “log10”) +
scale_y_continuous(trans = “log10”) +
labs(title = “Relationship between ESG and Standard ETFs”, x = “ESG ETF”, y = “Standard ETF”) + theme_minimal() + theme(plot.title = element_text(face = “italic”, size = 18, hjust = 0.5), axis.title = element_text(face = “bold”, size = 12), axis.text = element_text(size = 10), legend.position = “bottom”) ##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.

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. ```