DATA 110 — Continuous Variables Assignment

library(tidyverse)  
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── 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(dslabs)
library(ggthemes)
library(viridis)
Loading required package: viridisLite
data(stars)
glimpse(stars)
Rows: 96
Columns: 4
$ star      <fct> Sun, SiriusA, Canopus, Arcturus, AlphaCentauriA, Vega, Capel…
$ magnitude <dbl> 4.8, 1.4, -3.1, -0.4, 4.3, 0.5, -0.6, -7.2, 2.6, -5.7, -2.4,…
$ temp      <int> 5840, 9620, 7400, 4590, 5840, 9900, 5150, 12140, 6580, 3200,…
$ type      <chr> "G", "A", "F", "K", "G", "A", "G", "B", "F", "M", "B", "B", …
stars2 <- stars |>
  mutate(type = factor(type, levels = c("O","B","A","F","G","K","M")))
ggplot(stars2, aes(x = temp, y = magnitude, color = type)) +
  geom_point(size = 3, alpha = 0.8) +
  scale_x_continuous(trans = "log10") +
  scale_x_reverse() +
  scale_y_reverse() +
  scale_color_viridis(discrete = TRUE, option = "plasma", name = "Spectral Type") +
  labs(
    x = "Surface Temperature (Kelvin, log scale, hottest on left)",
    y = "Apparent Magnitude (reversed: higher up = brighter star)",
    title = "The Hertzsprung-Russell Diagram: Star Temperature vs. Brightness",
    caption = "Source: dslabs package, 'stars' dataset"
  ) +
  theme_economist() +
  theme(
    legend.position = "right",
    plot.title = element_text(size = 13, face = "bold")
  )
Scale for x is already present.
Adding another scale for x, which will replace the existing scale.
Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).

library(highcharter)
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
Highcharts (www.highcharts.com) is a Highsoft software product which is
not free for commercial and Governmental use

Attaching package: 'highcharter'
The following object is masked _by_ '.GlobalEnv':

    stars
The following object is masked from 'package:dslabs':

    stars
library(RColorBrewer)
star_cols <- brewer.pal(7, "Dark2")

highchart() |>
  hc_add_series(
    data = stars2,
    type = "scatter",
    hcaes(x = temp, y = magnitude, group = type)
  ) |>
  hc_colors(star_cols) |>
  hc_xAxis(
    title = list(text = "Surface Temperature (Kelvin)"),
    reversed = TRUE,
    type = "logarithmic"
  ) |>
  hc_yAxis(
    title = list(text = "Apparent Magnitude (brighter stars are lower numbers)"),
    reversed = TRUE
  ) |>
  hc_title(text = "Interactive Hertzsprung-Russell Diagram") |>
  hc_subtitle(text = "Hover over a point to see exact temperature and magnitude") |>
  hc_caption(text = "Source: dslabs package, 'stars' dataset") |>
  hc_tooltip(
    pointFormat = "Temp: {point.x} K<br>Magnitude: {point.y}"
  ) |>
  hc_legend(align = "right", verticalAlign = "top", layout = "vertical", title = list(text = "Spectral Type"))

The stars data set from the dslabs package was used in this visualization and it has physical attributes of 96 well known stars such as surface temperature, magnitude and spectral classification. I made a Hertzsprung-Russell plot of temperature against magnitude and reversed the axes while making temperature logarithmic according to the astronomical practice since smaller magnitudes imply brighter stars. I used color to represent spectral types with viridis color palette which is different from the default and changed the default theme of ggplot to theme_economist() and included another version of the plot which is interactive using highcharter package. The most fascinating finding about the stars is that they do not form a random distribution but rather form a diagonal line called the main sequence where stars similar to our Sun spend much of their lifetime. There are few cooler stars found higher than the main sequence line and they are red giants due to the fact that they have expanded and become brighter in later stages of their lives.