[1] 4
Homework 1
Quarto
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
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)
# use dplyr::glimpse() to get an overview of our data
blackrock_esg_vs_non_esg_etf |>
glimpse()
g <- blackrock_esg_vs_non_esg_etf |>
ggplot(aes(x = esg_etf, y = standard_etf)) +
geom_point()
g
g2 <- g +
scale_x_log10() +
scale_y_log10()
g2
log10(0)
g3 <- blackrock_esg_vs_non_esg_etf |>
# add sector as an aesthetic mapped to sector in aes()
ggplot(aes(x = esg_etf, y = standard_etf, color = sector)) +
# everything else is the same as above
geom_point() +
scale_x_log10() +
scale_y_log10()
g3
g4 <- g3 +
geom_abline()
g4
g5 <- g4 +
facet_wrap(~sector)
g5
blackrock_esg_vs_non_esg_etf |>
ggplot(aes(x = esg_etf, y = standard_etf, color = sector)) +
# make points translucent using alpha so we can better see overlap.
geom_point(alpha = .6) +
# make the x and y axes be equal using limits argument.
# format the axes as percent to make the units clear
scale_x_log10(limits = c(.01,10), labels = scales::label_percent(scale = 1)) +
scale_y_log10(limits = c(.01,10), labels = scales::label_percent(scale = 1)) +
geom_abline() +
facet_wrap(~sector, ncol = 4) +
# get rid of the legend. Because each facet is labeled, it just created clutter.
theme(legend.position = "none") +
# Give titles and subtitles that tell your viewer what they are looking at.
labs(title = "Large Cap American Equities ETFs: ESG vs. Non-ESG",
subtitle = "A comparison of the holdings of BlackRock iShares ESGU and IVV",
# Always label your axes.
x = "Weight in ESG ETF (ESGU)",
y = "Weight in Standard ETF (IVV)",
# Tell people where the data came from.
# You can also include the date of the data when relevant.
# You can put your name here, and let the job offers & promotions roll in.
caption = "Data: BlackRock iShares")
#1.5.1.2 Homework problem 1: Recreate the chart above in Esquisse
library(tidyverse)
tidyverse_update()
#esquisser(viewer = "browser")
#esquisser(viewer = "pane")
#options("esquisse.viewer" = "browser")
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)",
caption = "Nadia Palesa Mnguni"
) +
theme_minimal() +
facet_wrap(vars(sector))
# 1.5.1.3 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
library(tidyverse)
library(shiny)
tidyverse_update()
#esquisser(viewer = "browser")
#esquisser(viewer = "pane")
#options("esquisse.viewer" = "browser")
blackrock_esg_vs_non_esg_etf_long %>%
#I am filtering the fund types by weight
filter(weight >= 1L & weight <= 7L) %>%
ggplot() +
aes(
x = weight,
y = company_name,
colour = fund_type,
size = weight
) +
geom_point(shape = "circle") +
#making the cirlcles for the fund types the right colours
geom_smooth(span = 0.75) +
scale_color_manual(
values = c(`ESG ETF (ESGU)` = "#30D313",
`Standard ETF (IVV)` = "#A9AEB2")
) +
#labelling and adding caption
labs(
x = "Weight ",
y = "Company name ",
title = "Fund Type by Size",
caption = "Nadia Palesa Mnguni"
) +
theme_minimal()
##7 The chart effectively shows the distribution of investments across companies within two types of funds.
##The clustering on the left suggests most companies hold a smaller slice of the fund's portfolio,
##while the outliers with higher weights signal a substantial investment in those particular entities.
##These outliers could represent a strategic emphasis by the fund on those companies, believed to be pivotal within their portfolio strategy.
##However, this concentration also introduces potential risk; if these key companies falter, the fund's performance could be disproportionately affected.
##Investors looking at this chart should be mindful of these weightings as they reflect both the fund's confidence in these companies and the inherent risks of such investment concentrations.
#1.5.1.4 Homework problem 3: Make your own charts with esquisse
blackrock_esg_vs_non_esg_etf_long %>%
filter(weight >= 1L & weight <= 7L) %>%
ggplot() +
aes(x = sector, fill = fund_type, colour = company_name) +
geom_bar() +
#filling the colour by fund type
scale_fill_manual(
values = c(`ESG ETF (ESGU)` = "#30D313",
`Standard ETF (IVV)` = "#A9AEB2")
) +
scale_color_manual(
values = c(`ESG ETF (ESGU)` = "#30D313",
`Standard ETF (IVV)` = "#A9AEB2")
) +
#Adding labels
labs(
x = "Sector",
y = "Fund Type",
title = "Fund Type Share by Sector",
caption = "Nadia Palesa Mnguni"
) +
theme_minimal()
#chart explanation:This is a stacked bar chart illustrating the distribution of the two types of funds—ESG ETFs and Standard ETFs—across various sectors.
#The chart allows us to quickly understand the focus of investment funds and could potentially reflect the overall market sentiment towards ESG concerns within various economic sectors.
# Chart 2
blackrock_esg_vs_non_esg_etf %>%
#trying to remove the data that are clustered at 0 on either axis
filter(esg_etf >= 0.15 & esg_etf <= 7) %>%
filter(standard_etf >= 0.15 &
standard_etf <= 6.95) %>%
ggplot() +
#need the data to show the sector size and colour
aes(
x = standard_etf,
y = esg_etf,
colour = sector,
size = sector
) +
geom_point(shape = "circle") +
scale_color_hue(direction = 1) +
#formatting the axes as percent to make the units clear
scale_x_continuous(trans = "log10") +
scale_y_continuous(trans = "log10") +
#labelling
labs(
title = "ESG ETFs vs Standard ETFs by sector size",
caption = "Nadia Palesa Mnguni"
) +
theme_minimal()
#This is a scatterplot comparing the two ETFs by focusing on the sector and sector size.
#We see a weak positive relationship, with most sectors prioritizing standard etfs compared to esg etfs
#1.5.2.1 Homework problem 4: Understanding aes()
#Chart 1
blackrock_esg_vs_non_esg_etf %>%
ggplot(aes(x = esg_etf, y = standard_etf, color = sector)) +
geom_point() + # Add points to the plot
scale_x_log10() + # Set the x axis to a logarithmic scale
scale_y_log10() + # Set the y axis to a logarithmic scale
geom_smooth(method = 'loess', se = TRUE,colour="blue") + # Add a smoothed line using a log-linear model
labs(x = "esg_etf", y = "standard_etf", color = "Sector") + # Add labels
theme_minimal() + # Use a minimal theme
theme(legend.position = "right") # Position the legend on the right
#Chart 2
blackrock_esg_vs_non_esg_etf %>% ggplot(aes(x = esg_etf, y = standard_etf)) +
geom_point(aes(color = sector)) + # Add colored points based on sector
scale_x_log10() + # Set x-axis to logarithmic scale
scale_y_log10() + # Set y-axis to logarithmic scale
geom_smooth(aes(color = sector), # Add a smooth line for each sector
method = 'loess', se = TRUE) +
labs(x = "esg_etf",
y = "standard_etf",
color = "Sector") +
theme_minimal() # Use a minimal theme
#for chart 1 and 2 The ggplot2 grammar difference that makes the shaded area appear in the bottom chart is likely the addition of a parameter within geom_smooth(),
#such as fill = to add the shading, and setting method = and se = TRUE to specify the method of smoothing and to include the standard error in the confidence interval, respectively.
#chart 3
blackrock_esg_vs_non_esg_etf %>%
ggplot(aes(x = esg_etf, y = standard_etf)) +
geom_point(color = "purple") + # Plot points in purple
scale_x_log10() + # Logarithmic scale for x-axis
scale_y_log10() + # Logarithmic scale for y-axis
geom_smooth(method = "loess", se = TRUE, color = "yellow") + # Add regression line with confidence interval
labs(x = "esg_etf", y = "standard_etf") +
theme_minimal() # Use a minimal theme for the plot
#This chart's ggplot2 grammar is focused on demonstrating the overall trend between the two variables without breaking it down by sector, using color to highlight the trend line rather than the data points' categories.
#To recreate this chart, you would not include a color aesthetic in aes() for differentiating by sector,
#which is the main difference in ggplot2 grammar from the previous two plots.
#1.5.3.1 Resources for learning to make new charts
library(hrbrthemes)
# bar chart
blackrock_esg_vs_non_esg_etf%>%
ggplot(aes(fill=sector,x=esg_etf, y=sector)) +
geom_bar(position="dodge", stat="identity")
#this grouped bar chart shows us that the IT sector has the largest share of esg etfs while real states has the smallest.
blackrock_esg_vs_non_esg_etf%>%
ggplot(aes(fill=sector,x=standard_etf, y=sector)) +
geom_bar(position="stack", stat="identity")
#stacked barchart
blackrock_esg_vs_non_esg_etf_long%>%
ggplot(aes(fill=sector, y= weight, x=fund_type)) +
geom_bar(position="stack", stat="identity")
# this stacked bar char shows us that the alloaction of funds across sectors is largely similar between fund types
#1.5.3.2 Homework problem 6: make a new chart from the ggplot2 Extensions Gallery
install.packages("hrbrthemes") # NOTE: CRAN version is 0.8.0
import_roboto_condensed()
library(hrbrthemes)
library(gcookbook)
library(tidyverse)
# current verison
packageVersion("hrbrthemes")
## [1] '0.8.6'
ggplot(blackrock_esg_vs_non_esg_etf, aes(esg_etf, standard_etf)) +
geom_point(color = ft_cols$yellow) +
labs(x="ESG ETF", y="Standard ETF",
title="Scatterplot of esg and standard etfs",
subtitle="postive relative relationship is shown",
caption="Nadia Palesa Mnguni") +
theme_ft_rc()
warnings()
You can add options to executable code like this
The echo: false option disables the printing of code (only output is displayed).