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.


Description 2-3 paragraphs.

Describe the data set. Explain the variable you are graphing in your plots and the reason you are investigating with it. Discuss the reason/motivation you chose the variable to facet on, and what insight or trend you are attempting to investigate. Discuss any challenges you had in making the graphs and how you dealt with these challenges. Name at least one cognostic measure (this can include the cognostic you created or be different) the reader could investigate, and explain any insight they might gain from it.


grading: trelliscope plot[25 points], discussion[25 points]


Note: you can add a url directly to the text and it will be active in the html (and word document if you knit to that)

Example: https://www.google.com

If you want to be fancy and make your url active text, you can do this

library(ggplot2)
library(tidyverse)
## Warning: package 'tidyr' was built under R version 4.5.2
## Warning: package 'dplyr' was built under R version 4.5.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.1.5
## ✔ forcats   1.0.1     ✔ stringr   1.5.1
## ✔ lubridate 1.9.4     ✔ tibble    3.3.0
## ✔ purrr     1.1.0     ✔ 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
library(trelliscopejs)
## This package is no longer maintained. Please use the 'trelliscope' package instead (see https://github.com/trelliscope/).

Get Data

library(Lahman)
# This is a baseball package I found online that has MLB data files dating back to 1871
baseball_data <- Teams

Get cleaned data set for visual use

Team_Clean <- Teams %>%
  mutate(win_percent = W / (W + L)) %>% # create custom win % column to be used later
  group_by(franchID) %>%
  filter(n() >= 20) # only keep franchises with at least 20 years existence

Cognostic Measure

# I wanted to create a measure for each franchise that could be used to sort by in the visual
# my measure: percent of winning seasons, using my previously created win_percent > .500
Team_Clean <- Team_Clean %>%
  group_by(franchID) %>%
  mutate(
    pct_above_500 = round(mean(win_percent > 0.5) * 100, 1)) %>%
  ungroup()

Visual

Team_Clean %>%
  ggplot(aes(x = yearID, y = win_percent, color = teamID)) + # color shows different team names for each franchise
  geom_point() +
  facet_trelliscope(~ franchID,
                    name = "Franchise",
                    desc = "Franchises with at least 20 years existence since 1871",
                    nrow = 2, ncol = 2,
                    path = ".",
                    self_contained = TRUE)
## using data from the first layer

Description

# I found this data set online while searching for sports data sets. My dream is to work for a professional sports team some day, and this trellsicope package seemed like the perfect assignment to facet on franchises. The Lahman package has a handful of data sets, and the one I decided to use is called "Teams." This data set contains 3075 observations of 48 variables. Each row represents a single season for a MLB franchise dating all the way back to 1876. I decided to do a small amount of data cleaning to ensure my future scatterplots were well represented by filtering by franchises that had at least 20 seasons of data. I faceted on each Franchise to get 30 separate scatterplots to see how each compare with one another throughout their history's.
# I created two customs variables for this assignment. The first was a simple win percentage for each row that divided wins by total games. This variable would end up being the y variable for the scatterplots. I wanted to investigate winning trends throughout the years of franchises and needed to account for the fact that the total number of games in a season was not constant since 1876. The second measure I created did not change by year, rather was constant for each individual franchise. For each of the 30 franchises, I calculated the percentage of "winning" seasons, or percent of seasons that had a winning percentage over .500. For example, one of the best franchises of all time, the Los Angeles Dodgers, have a winning season percentage of 66%. The Colorado Rockies on the other hand, have had winning seasons just 38.1% of their existence. This measure can be toggled on by clicking it on the left most side under filters, and it will appear under each franchID. A user could investigate current teams, both good and bad, and see how their current success aligns with their historical success. At the same time, you are able to see which franchises typically remain around .500 and which had extremely good and awful seasons. I went with a 2 by 2 configuration so you could easily make comparisons between the franchises. I found that more than 4 on a page was not beneficial and any less did not use the space available effectively. 
# The most difficult part of this assignment was finding a data set. I first searched on data camp before resorting to google for sports data sets. Once I found the Lahamn package, it became very clear and obvious what I could represent with franchise data. I knew I wanted to explore historical winning trends among the 30 franchises and see if the prestigious teams of today were really that much better throughout history. I did not originally plan to show team names for each franchise, but by using the color = function in ggplot, I was also able to show each franchises team names and how the team performed under each name, which I think adds a lot to the visuals. You can easily see which franchises have never re branded and which have changed numerous times. This was a really fun assignment!