Problem 1

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.

library(ggplot2)
library(trelliscopejs)
## This package is no longer maintained. Please use the 'trelliscope' package instead (see https://github.com/trelliscope/).
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ lubridate 1.9.5     ✔ tibble    3.3.1
## ✔ purrr     1.2.2     ✔ tidyr     1.3.2
## ── 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
NHL <- read.csv("NHL Data.csv")

# Create a new data frame to use for plotting
NHL2 <- NHL %>%
  select(G, A, Pos) %>%
  group_by(Pos) %>%
  mutate(Correlation = cor(G, A))

ggplot(NHL2, aes(x=G, y=A)) +
  geom_point() + 
  geom_smooth(method="lm", se=FALSE) + 
  labs(title = "Goals vs. Assists",
       x="Goals",
       y="Assists") + 
  facet_trelliscope(~Pos, nrow=2,ncol=3, name="Goals vs. Assists by Position", path="trelliscope")
## `geom_smooth()` using formula = 'y ~ x'
## using data from the first layer
## 
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
NHL2_cor <- NHL %>%
  group_by(Pos) %>%
  summarize(Correlation = cor(G, A))

The data set I used was data for top 499 NHL players for the 2025-2026 season. The data set includes a multitude of variables that are both discrete and continuous. I created a new data set called NHL2 to shrink the amount of variables to only the ones I needed. These variables include G (Total Goals scored in the season), A (Total Assists in the season), and Pos (Position of the player).

I choose to do a scatter plot due to what I am trying to investigate. I am plotting the variables G and A and faceting on Pos. I am trying to see if there is any correlation between Goals scored and Assists and if it varies by position. When it comes to certain moments in the game, some players would rather take a shot even when passing would have a higher chance of giving the team the point. I think it would be interesting to see if players with higher assists also score higher goals and if certain positions give more assists than goals and vice-versa.

The big challenge I faced with the graph was the cognostic I had created. I initially tried to do it with the summarize in the first tidyverse but I was receiving an error. After checking the data camp notes and doing some research on the internet, I released my mistake of putting it before my graph. I copied and pasted it after my graph and edited it some and my plots came out good. Another issue I had was when trying to knit the file to HTML I kept getting an error. This was due to the facet code portion. I added a title and path like they have in the datacamp notes and that fixed it. A cognostic measure that you could investigate is the same as the one I created, which is the correlation between Goals and Assists. By investigating this, you can compare it to the graphs to see what or if there is a relationship between Goals and Assists per position.