library(tidyverse)
library(png)ggplot Showcase
path <- "C:/Users/Justin Pons/Fall 2023/DA 6233 Data Visualization/DA-6233-2023/Homework 2"
setwd(path)d1 <- read_csv("tech_stocks_csv.zip", show_col_types = FALSE) |>
filter(sale > 0) |>
mutate(conm = stringr::str_to_title(conm),
datadate = lubridate::ymd(datadate))Question 1
d1_1 <- d1 |>
group_by(conm) |>
summarize(avg_sale = mean(sale), .groups = "drop") |>
arrange(desc(avg_sale))plot_1 <- d1_1 |> ggplot(aes(reorder(conm, avg_sale), avg_sale))
plot_1 <- plot_1 +
geom_bar(stat = "identity") +
labs(x = "Company", y = "Average Sale in $ Millions") +
scale_y_continuous(labels = scales::label_dollar(prefix = "$")) +
coord_flip() +
theme_minimal()
plot_1Question 2
plot_2 <- plot_1
plot_2 <- plot_2 +
geom_text(aes(label = round(avg_sale/1000)),hjust = -.2, size = 3)
plot_2Question 3
d1_3 <- d1 |> mutate(debt_ratio =
(dlc + replace_na(dltt, 0)) / at, rnd_int = xrd / sale) |>
select(debt_ratio, rnd_int)
plot_3 <- d1_3 |> ggplot(aes(x=debt_ratio,y=rnd_int))
plot_3 <- plot_3 +
geom_point(size = 3.5,fill = 'darkgray',shape=21) +
labs(x = 'Debt Ratio',y = "R&D to Sales Ratio") +
geom_smooth(method='lm',formula=y~x) +
theme_minimal()
plot_3Question 4
d1_4 <- d1 |>
filter(tic %in% c("AAPL", "TSLA", "META", "MSFT", "AMZN", "NVDA")) |>
mutate(pr_margin = oibdp / sale,
fyear = as.Date(paste(fyear, "12", "31", sep = "-")))plot_4 <- d1_4 |> ggplot(aes(x=fyear,y=pr_margin))
plot_4 <- plot_4 +
geom_line() +
geom_hline(yintercept=0,color="red") +
labs(x = "Fiscal Year", y = "Profit Margin") +
scale_y_continuous(labels = scales::percent) +
facet_wrap(~conm) +
theme_minimal() +
theme(strip.text.x = element_text(hjust = -0.005))
plot_4Question 5
d1_5 <- d1 |>
filter(conm == "Tesla Inc") |>
mutate(mkt_val = prcc_f * cshpri) |> # Create market value
select(conm, datadate, mkt_val, sale) |>
pivot_longer(cols = c(mkt_val, sale),
names_to = "fin_var",
values_to = "fin_value")plot_5 <- d1_5 |> ggplot(aes(x = datadate,y = fin_value, color = fin_var)) +
geom_line(linewidth = 1.25) +
labs(y = NULL,x = 'Date',color='Financial Variable') +
ggtitle("Tesla's Sales and Market Value (Million USD)") +
scale_y_continuous(labels = scales::label_dollar(prefix = "$")) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5, face = 'bold',size = 16),
legend.key = element_blank(),
legend.position="top",
legend.box="horizontal"
)
plot_5Question 6
d1_6 <- d1 |> filter(tic %in% c("NVDA","META"))
plot_6 <- d1_6 |> ggplot(aes(x=as.factor(fyear),y=oibdp,fill=conm)) +
geom_col() +
facet_wrap(~conm,ncol=1) +
labs(x = 'Fiscal Year',y = 'Profit in $ Millions',fill=NULL) +
scale_fill_manual(values = c("#5cc9f5", "#b131a2")) +
theme_minimal() +
theme(legend.position = "top",
strip.text.x = element_text(hjust = -0.005,size=12)
)
plot_6Question 7
d1_7 <- d1 |> filter(tic %in% c("META"))plot_7 <- d1_7 |> ggplot(aes(x=as.factor(fyear),y=oibdp,fill=conm)) +
labs(x = 'Fiscal Year',y = 'Profit in $ Millions') +
geom_col(show.legend=FALSE) +
scale_fill_manual(values = c("#5cc9f5")) +
theme_minimal() +
annotation_raster(readPNG("mark-zuckerberg-celebrity-mask.png"),
xmin = 1, xmax = 3,
ymin = 30000, ymax = 40000, interpolate = T)
plot_7