For a data set of your choosing, make a faceted plot using the trelliscopejs package. You may make any type of plot; scatter plot, histogram, etc. but, as mentioned in the discussion below, you must explain why you chose this plot and what you are investigating about the variable you are graphing.
The trelliscope plot must include one cognostic measure of your own. Include a description of what it is and what information this measure gives.
# Declare libraries
library(ggplot2)
library(tidyverse)
library(trelliscopejs)
library(plotly)
# Import data
nsdq_symb <- readRDS("C:/Users/Admin/Documents/RMU/02) Data Visualization/Week 4/Homework/nsdq_symb.rds")
nsdq2016 <- readRDS("C:/Users/Admin/Documents/RMU/02) Data Visualization/Week 4/Homework/nsdq2016.rds")
# Data cleaning
# Join datasets together, verifying that nsdq_symb is unique symbol
length(unique(nsdq_symb$symbol))
## [1] 500
nsdq_symb_2 <- nsdq_symb %>%
ungroup() %>%
arrange(symbol) %>%
group_by(symbol)
nsdq2016_2 <- nsdq2016 %>%
ungroup() %>%
arrange(symbol, date) %>%
group_by(symbol)
stocks <- left_join(nsdq2016_2, nsdq_symb_2, by = "symbol")
# Create variable transformations and feature creation to feed into graphs that will be picked up as cognostics
stocks2 <- stocks %>%
group_by(symbol) %>%
arrange(date) %>%
mutate(
daily_return = (close - lag(close))/lag(close),
log_return = log(close/lag(close)),
ytd_return = close / first(close),
yearly_return = last(close)/first(close),
avg_daily_return = mean(daily_return, na.rm = TRUE)
)
# Create exploratory visualizations
stocks2 %>%
distinct(symbol, yearly_return) %>%
ggplot(aes(x = yearly_return)) +
geom_histogram()
stocks2 %>%
ggplot(aes(x = date, y = ytd_return, group = symbol)) +
geom_line()
# looks like there's some outliers
# filter to outliers
filter(stocks2, ytd_return > 5)
stocks2[stocks2$symbol == "TSG",]
# It's just one group, exclude them. Could be a data issue, as it looks like everything is shifted by a decimal point, but can't find external data to back it up.
stocks2 <- stocks2[stocks2$symbol != "TSG",]
# graph again without outliers, this looks like a good graph to facet by company in trelliscope, sector info could feed into cognostics
# The nature of a ytd return line has a normalized axis per stock, which would provide a nice trelliscope view
stocks2 %>%
ggplot(aes(x = date, y = ytd_return, group = symbol)) +
geom_line(aes(color = sector)) +
facet_wrap(~sector)
# We see that the lines show the returns, but let's add in a frequency measure of average trading volume
avg_volume <- stocks2 %>%
group_by(symbol) %>%
summarize(avg_volume = mean(volume, na.rm = TRUE))
stocks2 <- left_join(stocks2, avg_volume, by = "symbol")
stocks2$volume_scaled <- stocks2$volume/stocks2$avg_volume
# trelliscope
stocks2 %>%
ggplot(aes(x = date, y = ytd_return)) +
geom_line() +
labs(x = "Date",
y = "Cumulative Year to Date Value") +
theme(legend.position = "none") +
theme_bw()+
facet_trelliscope(~symbol,
name = "Nasdaq Return 2016",
desc = "Cumulative returns by stock for 2016",
nrow = 2, ncol = 3,
scales = c("same","same"),
path = ".",
self_contained = TRUE
)