library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.5.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## âś” dplyr 1.1.4 âś” readr 2.1.5
## âś” forcats 1.0.1 âś” stringr 1.5.1
## âś” ggplot2 4.0.2 âś” tibble 3.3.0
## âś” lubridate 1.9.4 âś” tidyr 1.3.1
## âś” purrr 1.1.0
## ── 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(ggplot2)
library(trelliscopejs)
## This package is no longer maintained. Please use the 'trelliscope' package instead (see https://github.com/trelliscope/).
diamonds %>%
group_by(clarity) %>%
mutate(
avg_price_per_carat = mean(price / carat),
price_per_carat_cog = cog(avg_price_per_carat, desc = "Average price per carat (USD) for each clarity grade")
) %>%
ungroup() %>%
ggplot(aes(x = cut, y = price / carat)) +
geom_point(color = "steelblue", alpha = 0.2, size = 1.5) +
geom_hline(aes(yintercept = avg_price_per_carat), linetype = "dashed", color = "red") +
labs(
x = "Cut Quality",
y = "Price per Carat (USD)"
) +
theme_minimal() +
facet_trelliscope(
~ clarity,
nrow = 2,
ncol = 4,
name = "Diamond Price per Carat vs. Cut Quality by Clarity",
desc = "Does cut quality affect price per carat?",
path = ".", self_contained=TRUE
)
## using data from the first layer
#For this assignment, I used the built in diamonds dataset from ggplot2 package, which contains prices and physical characteristics for over 53,000 round cut diamonds. In my plots I graphed the price per carat of the diamonds (price / carat) on the y axis against cut quality (cut) on the x axis, with a dashed red horizontal line showing us the average price per carat for each clarity group(this is my cognostic measure). I chose this plot to investigate whether higher/better cut qualities (ranging from Fair to Ideal) result in a higher price per carat, and to see how individual diamonds spread above and below each group's average price per carat benchmark. I chose to facet on clarity (ranging from I1 to IF) because diamond clarity often strongly influences pricing and could otherwise mask the effect of cut quality if all the diamonds were combined into a single chart. Using facet_trelliscope() i separated by each clarity grade into its own panel so the viewer can step through the clarity levels and compare how the distribution of points across cut qualities shifts relative to the red dashed benchmark line.
#One challenge I encountered while building the graph was the heavy overplotting caused by plotting over 50,000 points across only five categorical cut levels, which initially made the points stack into solid vertical bars. Setting alpha = 0.2 to make the points semi transparent helped reveal where observations are most concentrated, while and making sure my tidyverse piping of group_by(clarity) %>% mutate(...) directly into ggplot() ensured my custom cognostic (avg_price_per_carat) worked cleanly for both the geom_hline() reference line and the trelliscope sidebar controls. In addition to my custom price_per_carat_cog measure, readers can investigate the automatically generated price_per_carat_max (or price_per_carat_median) cognostic in the trelliscope sort and filter menu. Sorting panels by price_per_carat_median alongside my custom mean cognostic lets people compare normal/average unit prices against high-end outliers across clarity grades—showing, for example, that upper clarity grades like VVS1 and IF reach much higher peak prices per carat even when lower clarity grades contain larger individual stones.
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.
knit the file to an html document
publish this to an RPubs page.
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