Data Analysis Boot Camp: Data Visualization

Author

Anran Fang

Problem 1: Recreate the chart above in Esquisse

  1. Import blackrock_esg_vs_non_esg_etf
library(readr)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
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 the four columns we will use in our anlaysis here
  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.

2.  Look at the screenshot above. Assign your variables to the correct aesthetics, and choose the correct chart type.

3.  Explore the menus at the bottom of the screen.

    -   Change the x and y axis to log10.

    -   Add a smoother (there is no 45 degree line option in esquisse).

    -   Add descriptive titles and labels. Put your name in the caption.

4.  Change the color palette to a different color palette of your choice.

5.  Copy and paste the code into your homework.
library(ggplot2)
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_brewer(palette = "Set3", direction = 1) +
  scale_x_continuous(trans = "log10") +
  scale_y_continuous(trans = "log10") +
  labs(
    x = "ESG ETF (ESGU)",
    y = "Standard ETF (IVV)",
    title = "We made this chart using Esquisse!",
    subtitle = "It's a great tool for learning ggplot2,
    even it has its limitations.",
    caption = "Anran Fang"
  ) +
  theme_minimal() +
  facet_wrap(vars(sector), ncol = 3L)
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Problem 2: Exploring the outliers

Some preparation work:

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ lubridate 1.9.3     ✔ tibble    3.2.1
✔ purrr     1.0.2     ✔ tidyr     1.3.1
── 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
blackrock_esg_vs_non_esg_etf_long <- blackrock_esg_vs_non_esg_etf %>%
  pivot_longer(cols = contains("etf"), names_to = "fund_type", values_to = "weight") %>% 
  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
  1. After running the code above, reload the Esquisse GUI. Import blackrock_esg_vs_non_esg_etf_long.quisse

  2. Go into the Data tab, and limit the weight variable to companies over a 1% weight.

  3. Choose a Point chart and assign variables to aesthetics

    • weight to the x and size aesthetics (double encoding).

    • company_name to y

    • fund_type to color

  4. Change the color for the ESG fund points to be green colored, and the non-esg fund to be grey.

  5. Add meaningful titles and labels. Put your name as the caption.

  6. Copy and paste the code to recreate the chart in your homework. Annotate the code using #s to explain what each part is doing.

  7. Write a short reflection (a few sentences) on how to interpret this chart, and what it tells you about the large outliers in the dataset.

blackrock_esg_vs_non_esg_etf_long %>%
 filter(weight >= 1L & weight <= 7L) %>% #Limit weight to companies over 1% weight
 ggplot() +
  aes( #Setting x and y and other aesthetics
    x = weight,
    y = company_name,
    colour = fund_type,
    size = weight
  ) +
  geom_point(shape = "circle") +
  scale_color_manual( #Assign green to ESG and grey to non-ESG
    values = c(`ESG ETF (ESGU)` = "#8EF86D",
    `Standard ETF (IVV)` = "#A69DA2")
  ) +
  labs( # Changing titles, captions, and some labels
    title = "ESG / Non-ESG Weights",
    caption = "Anran Fang",
    size = "Weight"
  ) +
  theme_minimal()

The output point chart displays all companies––both ESG and non-ESG–along with their weights. As most dots concentrate on the smaller weights sides, outliers are even more obvious, not only because they are on the right, but also because size of the dots reflects their weights. Two outliers are extreme, with respectively a rough 5.8% and 6.4% weight, yet these two companies are similarly included in both the ESG and non-ESG ETF. Therefore, we might come to a brief conclusion that weights allocation in the ETF fund does not entirely depends on the ESG property of the company.

Problem 3: Make your own charts with Esquisse

  1. Make 2 new charts that explore interesting questions in the data. Add meaningful titles and labels so viewers know what they are looking at.

  2. Copy and paste the code so you can recreate the charts in your homework. Annotate the code to explain what each section is doing.

  3. Short explanation of what each chart illuminates.

ggplot(blackrock_esg_vs_non_esg_etf_long) +   
  aes(x = fund_type,y = weight, colour = sector, group = fund_type) +#Two box plot grouped by fund type
  geom_boxplot() +   
  geom_jitter()+
  scale_y_continuous(trans = "log10") + #Use log scale for weights   
  labs(title = "Box Plot of Weights Allocation in ETF", caption = "Anran Fang") +   #Add a title and caption
  theme_minimal()+
  scale_color_hue(direction = 1)   #Show specific dot for each company in different sectors

The produced box charts shows the quartile weights that each fund allocates its companies. We can tell that ETF funds tend to include a lower number of companies, assigning each with roughly 0.2% weights on average, with 25-percentile at roughly 0.8% and 75-percentile at roughly 1.2%. In comparison, non-ESG ETF include more companies in its fund, with an average of less than 0.1%, though a much lower 25-percentile at only 0.02% yet a similar 75-percentile value. Besides, in each fund, we can see that companies in the IT sector is likely to be assigned the max weight and of almost equal weight in both funds. Same pattern is seen in companies in sectors of communication, consumer staples, and healthcare, each with consecutively little lower weight.

Problem 4: Understanding aes()

  1. recreate these three charts.

  2. Annotate your code.

  3. Below each one, write a short note explaining the ggplot2 grammar difference that makes it work

ggplot(blackrock_esg_vs_non_esg_etf) +
  aes(x = esg_etf, y = standard_etf, colour = sector) + #set x, y, and color   
  geom_point(shape = "circle", size = 1.55) + #draw the dot plot   
  geom_smooth(method = "lm", formula = y ~ poly(x, 3), color = "blue", span=0.75) + #add a grouped smooth line using designated cube polynomial   
  scale_x_continuous(trans = "log10") + #set x to log scale   
  scale_y_continuous(trans = "log10") +#set y to log scale   
  theme_gray()#set grey background 

# Group function here is essential to group all sectors and draw one trend line.

ggplot(blackrock_esg_vs_non_esg_etf) +
  aes(x = esg_etf, y = standard_etf, colour = sector) + #set x, y, and color   
  geom_point(shape = "circle", size = 1.55) + #draw the dot plot   
  geom_smooth(span=0.75) + #add a smooth line for each sector   
  scale_x_continuous(trans = "log10") + #set x to log scale   
  scale_y_continuous(trans = "log10") +#set y to log scale   
  theme_gray()#set grey background 
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'

#This simply deletes the group function in the first graph.

ggplot(blackrock_esg_vs_non_esg_etf) +
  aes(x = esg_etf, y = standard_etf) + #set x, y, and color   
  geom_point(shape = "circle", size = 1.55, color="purple", se=FALSE) + #draw the dot plot with no side notation   
  geom_smooth(method = "lm", formula = y ~ poly(x, 3), color = "yellow", span=0.75) + #add a grouped smooth line using designated cube polynomial   
  scale_x_continuous(trans = "log10") + #set x to log scale   
  scale_y_continuous(trans = "log10") +#set y to log scale   
  theme_gray()#set grey background 

#This graph changes the prviously color setting showing sectors into a set purple color; Color of trend line is also now set to yellow.

Problem 5: Make a new chart from the R Graph Galler

  1. Take a look at the R Graph Gallery.

  2. Choose a chart type that you think might shed light on our data.

  3. Copy the example and make sure you understand how it works.

  4. Use that to build a new chart for our data, and explain what the chart tells us about the data. As always, add informative titles and labels.

ggplot(blackrock_esg_vs_non_esg_etf_long, aes(x=sector, y=weight, fill=fund_type)) +
  geom_boxplot()+     
  scale_y_continuous(trans = "log10") + #Use log scale for weights     
  labs( #Add a title and caption     
    title = "Box Plot of Weights Allocation in ETF")

This is a grouped box plot showing the weights allocated to each sector, respectively for each fund type. The dots floating around the graph are outliers whose weights are way above the majority of its group. From the graph we can see the distribution of outliers, as well as their group.