The sp500 data set has the 502 companies in the Standards & Poors (S&P 500) that are the largest 500 (502) publicly traded companies in the US. The data set has 11 variables, with the important ones being:
symbol: The 3 to 4 letter symbol used to ID the company on the stock market
company: The name of the company
sector: The sector of the economy of the company (Medical/Financial/Utility/Construction/etc…)
state: Which state the headquarters is located
market_cap: The total price of all the “at large” shares (as of May 23rd 2024) of the company. Used as a measure of the value of the entire company.
price: The price of an individual share (as of May 23rd 2024)
region: The region in the US (or not in US) of the HQ (Northeast/Midwest/South/West/Not US)
Create a bar graph of each sector. See what it should look like on Brightspace!
ggplot(
data = sp500,
mapping = aes(
# Ordering the bars from highest to lowest (top - bottom)
x = fct_infreq(sector) |> fct_rev()
)
) +
geom_bar(
fill = "steelblue"
) +
# Changing the x-axis label and adding a title
labs(
x = NULL,
title = "S&P 500 Company Sectors",
y = "Number of companies"
) +
# Having the bars touch the y-axis
scale_y_continuous(
expand = c(0, 0, 0.05, 0)
) +
coord_flip() +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5))
Create a bar chart of the number of companies in each sector by region. See what it should look like on Brightspace!
ggplot(
data = sp500,
mapping = aes(
# Ordering the bars from highest to lowest (top - bottom)
x = factor(region, levels = c("Northeast", "South", "Midwest", "West", "Not US")),
fill = fct_infreq(sector)
)
) +
geom_bar(
position = "dodge2"
) +
# Changing the x-axis label and adding a title
labs(
x = "US Region",
title = "S&P 500 Company Sectors",
y = "Number of companies",
fill = "Sector"
) +
# Having the bars touch the y-axis
scale_y_continuous(
expand = c(0, 0, 0.05, 0)
) +
theme_bw() +
theme(
plot.title = element_text(hjust = 0.5),
panel.grid.major.x = element_blank()
)
Create a bar chart of the percentage of regions by sector. See what it should look like on Brightspace! The groups on the x-axis are ordered by market cap (highest -> lowest)
ggplot(
data = sp500,
mapping = aes(
# Ordering the bars from highest to lowest (top - bottom)
x = fct_reorder(sector, market_cap, .desc = T),
fill = factor(region, levels = c("Northeast", "South", "Midwest", "West", "Not US"))
)
) +
geom_bar(
position = "fill"
) +
# Changing the x-axis label and adding a title
labs(
x = NULL,
title = "S&P 500 Company Region by Sectors",
y = NULL,
fill = NULL
) +
# Having the bars touch the y-axis
scale_y_continuous(
expand = c(0, 0, 0.05, 0),
labels = scales::label_percent()
) +
theme_classic() +
theme(
plot.title = element_text(hjust = 0.5),
legend.position = "top"
)
The code chunk below reads in a data set that has the daily closing price per share of three companies: Amazon, Tesla, and Nvidia from January 1st, 2020 to May 23rd, 2024.
The important columns are: company, date, and market_cap
Create an appropriate graph to show how the three companies’ market cap has changed across time. If you use color to represent the companies (hint, hint), the company color hexcodes are:
Amazon: “#ff9900”
Tesla: “#E31937”
Nvidia: “#76B900”
Display market cap in billions (1,000,000,000) of dollars (ie, 100 billion market cap = 100). Add any appropriate titles, labels, themes, etc… to make the graph look nice!
ggplot(
data = three_stocks,
mapping = aes(
x = date,
y = market_cap/1e9,
color = company
)
) +
geom_line() +
# Adding titles and labels
labs(
title = "Market Cap for Amazon, Nvidia, and Tesla",
subtitle = "January 1st, 2020 - May 23rd, 2024",
y = "Market Cap (In Billions)",
x = NULL,
color = NULL
) +
theme_bw() +
# Centering the title and subtitle and moving the legend
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
legend.position = "inside",
legend.position.inside = c(0.1, 0.85)
) +
# Changing the y-axis to dollars
scale_y_continuous(
labels = scales::label_dollar()
) +
# Changing the colors to match the company
scale_color_manual(
values = c(Amazon = "#ff9900", Tesla = "#E31937", Nvidia = "#76B900")
)