---
title: "Qatar Cars"
author: "Ana Luisa Bodevan"
date: "2025-12-09"
categories: [scatteplot, misc]
image: "20251209.png"
execute:
warning: false
message: false
format:
html:
code-tools: true
code-fold: true
---
This week challenge dataset in on Qatar Cars. Check the [TidyTuesday](https://github.com/rfordatascience/tidytuesday/blob/main/data/2025/2025-12-09/readme.md) GitHub repo for the data.
## 1. SETUP
```{r}
#| label: load libraries and data
library(pacman)
pacman::p_load(
tidytuesdayR,
tidyverse,
dplyr,
janitor,
ggtext,
showtext,
scales,
glue,
skimr,
ggrepel,
ggbranding,
ggnewscale)
tuesdata <- tidytuesdayR::tt_load('2025-12-09')
qatarcars <- tuesdata$qatarcars |>
janitor::clean_names()
rm(tuesdata)
```
## 2. DATA ANALYSIS
```{r}
#| label: examine the data
skimr::skim(qatarcars)
```
## 3. DATA TIDYING
```{r}
#| label: tidy the data
# normalize prices
qatarcars <- qatarcars |>
mutate(
price_usd = price / 3.64,
price_eur = price / 4.15
)
# identify most expensive cars by engine type
most_expensive <- qatarcars |>
group_by(enginetype) |>
slice_max(order_by = price_usd, n = 1) |>
ungroup()
dollar_labels <- function(x) {
case_when(
x >= 1e6 ~ paste0("$", round(x / 1e6, 1), "M"),
x >= 1e3 ~ paste0("$", round(x / 1e3, 1), "k"),
TRUE ~ paste0("$", x)
)
}
```
## 4. PLOT
```{r}
#| label: aesthetics
# DARK MODE COLORS
background_dark <- "#111215" # near-black NYT dark
panel_dark <- "#18191c"
grid_dark <- "#2a2c31"
title_dark <- "#f2f2f2"
subtitle_dark <- "#d4d4d4"
text_dark <- "#c7c7c7"
caption_dark <- "#9a9a9a"
col_dark <- c(
"Electric" = "#6ccff6",
"Hybrid" = "#c7b8ea",
"Petrol" = "#8ce0b1"
)
col_dark_labels <- c(
"Electric" = "#9cdfff",
"Hybrid" = "#dfd4fb",
"Petrol" = "#baf2d3"
)
```
```{r}
#| label: fonts and text
font_add_google("Roboto Condensed", "robotoc")
font_add_google("Roboto", "roboto")
showtext_auto()
title_font <- "robotoc"
body_font <- "roboto"
title <- str_glue("Does Higher Power Drives Higher Prices?")
subtitle <- str_glue(
"Examining the horsepower-price relationship by engine type"
)
```
```{r}
#| label: final plot
ggplot() +
# ====================================================
# LAYER 1 — SCATTER
# ====================================================
geom_point(
data = qatarcars,
aes(x = price_usd, y = horsepower, color = enginetype),
alpha = 0.8,
size = 3
) +
scale_color_manual(values = col_dark, name = "Engine Type") +
# Independent color scale for labels
ggnewscale::new_scale_color() +
# ====================================================
# LAYER 2 — LABELS
# ====================================================
geom_text_repel(
data = most_expensive,
aes(
x = price_usd,
y = horsepower,
label = glue("{model}\n{dollar(price_usd, accuracy = 1)}"),
color = enginetype
),
size = 3.7,
fontface = "bold",
box.padding = 0.5,
point.padding = 0.3,
segment.color = caption_dark,
segment.size = 0.35,
min.segment.length = 0,
family = body_font,
seed = 1234,
bg.color = panel_dark,
bg.r = 0.15
) +
scale_color_manual(values = col_dark_labels, guide = "none") +
# ====================================================
# SCALES
# ====================================================
scale_x_log10(
labels = dollar_labels,
breaks = c(1e4, 2.5e4, 5e4, 1e5, 2.5e5, 5e5, 1e6, 3e6, 1e7)
) +
scale_y_continuous(
labels = label_comma(),
limits = c(0, 2050)
) +
# ====================================================
# LABELS
# ====================================================
labs(
title = title,
subtitle = subtitle,
x = "Price (USD)",
y = "Horsepower"
) +
# ====================================================
# DARK MODE THEME
# ====================================================
theme_minimal(base_family = body_font) +
theme(
plot.background = element_rect(fill = background_dark, color = NA),
panel.background = element_rect(fill = panel_dark, color = NA),
plot.title = element_text(
size = 22,
face = "bold",
family = title_font,
color = title_dark,
margin = margin(b = 6)
),
plot.subtitle = element_text(
size = 13,
color = subtitle_dark,
margin = margin(b = 15)
),
legend.position = "top",
legend.title = element_text(size = 10, face = "bold", color = text_dark),
legend.text = element_text(size = 9.5, color = text_dark),
axis.title = element_text(size = 11, face = "bold", color = text_dark),
axis.text = element_text(size = 9.5, color = text_dark),
panel.grid.minor = element_blank(),
panel.grid.major = element_line(color = grid_dark, linewidth = 0.2),
plot.margin = margin(t = 15, r = 25, b = 15, l = 25),
plot.caption = element_markdown(color = caption_dark)
) +
# ====================================================
# BRANDING — DARK MODE
# ====================================================
add_branding(
github = "anabodevan",
bluesky = "1141bode",
additional_text = "{qatarcars} | #TidyTuesday 2025 W48",
line_spacing = 1L,
icon_color = caption_dark,
text_color = caption_dark,
icon_size = "12pt",
text_size = "12pt",
text_family = "roboto",
caption_margin = margin(t = 20, b = 5, unit = "pt")
)
```