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(readxl)
library(dplyr)
library(ggplot2)
library(trelliscopejs)
# Load and clean data
cars <- read_excel("C:/Users/dalla/OneDrive/Desktop/Classes/Data Visualization/cars2.xlsx")
cars_cleaned <- cars %>%
filter(!is.na(`Engine HP`), is.finite(`Engine HP`), !is.na(`Engine Cylinders`)) %>%
mutate(`Engine Cylinders` = as.numeric(as.character(`Engine Cylinders`)),
HP_per_Cylinder = `Engine HP` / `Engine Cylinders`)
# Calculate average HP per cylinder
avg_hp_per_cyl <- cars_cleaned %>%
group_by(`Engine Cylinders`) %>%
summarise(Avg_HP_per_Cyl = mean(HP_per_Cylinder, na.rm = TRUE)) %>%
ungroup()
# Merge with main data
cars2 <- left_join(cars_cleaned, avg_hp_per_cyl, by = "Engine Cylinders")
# Generate Trelliscope visualization
cars2 %>%
ggplot(aes(x = `Engine HP`, y = `highway MPG`, color = Year)) +
geom_point() +
facet_trelliscope(~ `Engine Cylinders`, nrow = 2, ncol = 2, name = "Cylinders",
desc = "Scatter Plot of Engine Horsepower vs. Highway MPG by Cylinder Count",
scales = "same",
path = ".",
self_contained = TRUE
) +
geom_text(aes(label = paste0("Avg HP/Cyl: ", round(Avg_HP_per_Cyl, 2))), x = Inf, y = Inf,
hjust = 1, vjust = 1, size = 5, color = "black", inherit.aes = FALSE) +
ylim(c(0, 55)) +
theme_minimal()
For this analysis, I selected a detailed automotive dataset containing over 11,000 entries, featuring variables such as cylinder count, production year, horsepower (HP), and highway miles per gallon (MPG). The focus was on the relationship between HP and MPG, considering how this efficiency metric varies with engine size, as categorized by cylinder count. The data was cleaned to ensure accuracy, with the creation of an ‘HP per cylinder’ measure to provide insight on each car’s power efficiency.
A faceted scatter plot using the trelliscope package allowed a look at the efficiency trends across different engine types, with color coding by year to trace the evolution of engine performance over time. Challenges like overlapping text and axis scaling were addressed by strategic data manipulation and visualization adjustments.
The primary cognostic measure, ‘HP per cylinder,’ was used to quantify the efficiency of each engine type. This metric provides a detailed perspective on how engine power correlates with fuel economy, offering insights crucial for car enthusiasts and buyers evaluating a vehicle’s performance alongside its ecological footprint.
Although the dataset and the trelliscope graphs are very informative,
they need to be analyzed further because of the lopsidedness of the
data. There are way more data points for engines with 4, 6, and 8
cylinders than the rest.