R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

##Problem set blackrock_esg_vs_non_esg_etf

p <- ggplot(blackrock_esg_vs_non_esg_etf, aes(x = esg_etf, y = standard_etf)) + geom_point() + scale_x_log10() + scale_y_log10() + geom_smooth(method = “lm”, color = “skyblue”, se = FALSE) + labs(x = “ESG ETF Score (log10)”, y = “Standard ETF Score (log10)”, title = “Comparison of ESG ETF Scores vs. Standard ETF Scores”, caption = “Created by [ValeriaLee]”) + theme_minimal() p

blackrock_esg_vs_non_esg_etf |> ggplot(aes(x = esg_etf, y = standard_etf)) + geom_point() + scale_x_log10() + scale_y_log10() + geom_smooth(method = “lm”, color = “skyblue”, se = FALSE) + labs(x = “ESG ETF Score (log10)”, y = “Standard ETF Score (log10)”, title = “Comparison Between ESG and Standard ETF Scores”, caption = “Created by [ValeriaLee]”) + theme_minimal()

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_filtered <- filter(blackrock_esg_vs_non_esg_etf, weight > 0.01) ## filter the ones with weight>0.01 ggplot(blackrock_esg_vs_non_esg_etf_filtered, aes(x = weight, y = company_name, color = fund_type, size = weight)) + geom_point() + #create a point chart scale_color_manual(values = c(“ESG” = “green”, “Non-ESG” = “grey”)) + # Create colors based on the requirement labs(x = “Weight”, y = “Company Name”, color = “Fund Type”, size = “Weight”,#labelling correctly title = “Comparison Between ESG and Non-ESG Fund Weights”, caption = “Created by [ValeriaLee]”) + theme_minimal()

ggplot(blackrock_esg_vs_non_esg_etf_long, aes(x = date, y = weight, color = fund_type)) + geom_line() + labs(x = “Date”, y = “Weight”, color = “Fund Type”, title = “Trend of ESG and Non-ESG Fund Weights over Time”, caption = “Created by [ValeriaLee]”) + theme_minimal() ## the trending of ESG and Non-ESG funds Weights ##lollipop chart:to see detailed comparison betwwen ESG and Non ESG funds, i dont think its an efficient way but this shape is fun ggplot(blackrock_esg_vs_non_esg_etf_long, aes(x = weight, y = sector, color = fund_type)) + geom_segment(aes(xend = 0, yend = sector), size = 1) + geom_point(size = 3) + labs(x = “Weight”, y = “Sector”, color = “Fund Type”, title = “Sector-wise Comparison of ESG and Non-ESG Fund Weights”, caption = “Created by [ValeriaLee]”) + theme_minimal()

install.packages(“ggthemes”) library(ggthemes)

total_weights <- blackrock_esg_vs_non_esg_etf_long %>% group_by(sector, fund_type) %>% summarise(total_weight = sum(weight)) ggplot(total_weights, aes(x = sector, y = total_weight, fill = fund_type)) + geom_bar(stat = “identity”, position = “dodge”) + labs(x = “Sector”, y = “Total Weight”, fill = “Fund Type”, title = “Total Weights of ESG and Non-ESG Funds by Sector”, caption = “Created by [ValeriaLee]”) + theme_minimal() ## use the fivethirtyeight theme from the package from ggthemes