# Load libraries
library(dslabs)
library(tidyverse)
library(highcharter) # Interactive charts in R
library(RColorBrewer) # Color palettes for categorical data
# Load the dataset
data("stars")DS Labs Assignment
# Bin B–V index into 8 groups and assign Dark2 colors
bv_bins <- cut(stars$bv, breaks = 8, labels = FALSE)
colors <- brewer.pal(8, "Dark2")[bv_bins]
# Create list of points with embedded tooltip text
# Source: Highcharter tooltip formatting — https://jkunst.com/highcharter/articles/tooltip.html
hc_data <- pmap(
list(stars$temp, stars$lum, stars$bv),
\(x, y, bv) list(
x = x,
y = y,
name = paste0(
"Temperature: ", round(x), " K<br>",
"Luminosity: ", format(round(y, 2), nsmall = 2), " × Sun<br>",
"Color Index (B–V): ", round(bv, 2)
)
)
)
# Build interactive HR diagram
# Source: Highcharter scatter plot example — https://jkunst.com/highcharter/articles/scatter.html
# Source: Axis reversal and log scale — https://jkunst.com/highcharter/articles/axis.html
highchart() |>
hc_chart(type = "scatter") |>
hc_title(text = "Interactive Hertzsprung–Russell Diagram of Stars") |>
hc_xAxis(title = list(text = "Temperature (K)"), reversed = TRUE) |>
hc_yAxis(title = list(text = "Luminosity (Sun = 1)"), type = "logarithmic") |>
hc_add_series(
data = hc_data,
colorByPoint = TRUE,
colors = colors,
marker = list(radius = 5),
showInLegend = FALSE
) |>
hc_tooltip(useHTML = TRUE, pointFormat = "{point.name}") |>
hc_add_theme(hc_theme_flat()) # Source: Highcharter themes — https://jkunst.com/highcharter/articles/themes.htmlSummary
In this project, I created an interactive Hertzsprung–Russell (HR) diagram using the stars dataset from the dslabs package in R. The diagram plots stellar surface temperature on the x-axis (reversed to match astronomical convention) and luminosity on the y-axis using a logarithmic scale. Each star is represented as a point, color-coded by its B–V (Blue minus Visual) color index using the Dark2 palette from RColorBrewer. The chart was built using the highcharter package, with tooltips that display each star’s temperature, luminosity, and color index. The result is a clean, interactive visualization that captures the diversity of stellar types and satisfies the assignment’s requirements for continuous variables and interactivity.
The overall shape of the HR diagram reflects fundamental stellar physics. A star’s luminosity depends strongly on its surface temperature and size. According to the Stefan–Boltzmann law, luminosity increases with the fourth power of temperature and is also proportional to the surface area of the star. This law holds for the blackbody radiation of all physical objects. This means that even small increases in temperature lead to large increases in energy output, especially for stars with large radii.
This relationship explains the diagonal band of stars running from the upper left to the lower right: the main sequence. These stars are in a stable phase of hydrogen fusion, and their position reflects a balance between temperature and radius. Hot, massive stars appear in the upper left, while cooler, smaller stars fall toward the lower right. Above the main sequence are giants and supergiants: cooler stars with enormous radii that are still highly luminous. Below the main sequence are white dwarfs: hot but tiny stars with low luminosity.
The HR diagram is a powerful tool for understanding stellar evolution, and this interactive version allows users to explore how temperature, luminosity, and color index relate across different types of stars.